Sitemap
Dev Genius

Coding, Tutorials, News, UX, UI and much more related to development

Member-only story

Your Go API Doesn’t Have a Performance Problem. Your Database Does.

--

Your Go code is fast. Your queries are holding everything back.

Press enter or click to view image in full size
Photo by Artur Shamsutdinov on Unsplash

When an endpoint is slow, we reach for pprof, shave allocations and swap the JSON library. The benchmark improves. The p99 doesn't.

In a typical CRUD application or ledger service, a request spends microseconds in Go and milliseconds, sometimes seconds, waiting on the database. A CPU profile won’t show that wait, because a blocked goroutine burns no CPU.

So measure the database first. With the pg_stat_statements extension enabled (PostgreSQL 13+ column names):

SELECT calls, total_exec_time, mean_exec_time, rows, query
FROM pg_stat_statements
ORDER BY total_exec_time DESC
LIMIT 10;

Sort by total time. A 2 ms query called a million times costs more than a 3 s report run twice.

Here are five places to look, using a ledger service built on PostgreSQL and pgx/v5.

1. Connection pooling

A bigger pool isn’t a faster pool. Every PostgreSQL connection is a backend process, and past what your CPU and disks can handle, extra connections only add contention.

pgxpool defaults MaxConns to the greater of 4 or the CPU count. Set it deliberately:

--

--

Yaninyz witty
Yaninyz witty

Written by Yaninyz witty

Writing about golang, next js, typescript, devops, aws

Dev Genius
Dev Genius

Published in Dev Genius

Coding, Tutorials, News, UX, UI and much more related to development