How to Handle Database Connection Pooling
Connection pooling keeps a set of reusable database connections open, so requests check one out and return it instead of opening and closing a connection per query. A walkthrough builds a Node.js Express app with node-postgres, comparing per-query Client connections against a Pool (max 20, connection and idle timeouts) over 100 concurrent queries. Query latency is measured with prom-client histograms. In this local setup the pooled queries finished roughly 3x faster in total, and more individual queries completed within the low-millisecond buckets.
Related posts
- Supavisor: Scaling Postgres to 1 Million Connections
Supabase mediumAdeptSupavisor is a multi-tenant Postgres connection pooler written in Elixir that Supabase built to replace single-threaded PgBouncer. In benchmarks one 16-core instance held 250,000 client connections over a 400-connection database pool, and a 64-core instance held 500,000. Two 64-core nodes, one connected to the database and one relaying through it, held 1,003,200 connections at 20,000 QPS. Median query latency was under 2ms at 5,000 QPS, but running the pooler as a separate cluster adds about 2ms per query compared with co-located PgBouncer, in exchange for not consuming the database server's CPU and RAM.
- Using Neon’s Scale to Zero with Long-Running Applications
Neon mediumAdept
Neon's scale to zero suspends a Postgres compute after an idle window (only when no WAL senders are active and autovacuum isn't running), which severs connections held by long-running servers. A naive node-postgres Client crashes on the resulting unhandled error event. A client-side pg.Pool with an error handler tolerates the dropped connections and reconnects on demand, waking the database. In Apache Bench tests the pool beat per-request connections by more than 10x (about 46ms vs 678ms average latency, 370 vs 26 req/s), with the same pattern shown in a Fastify, Drizzle and HTMX example app.
- PgBouncer is now available in Supabase
Supabase mediumAdeptSupabase now bundles PgBouncer, co-located on the same server as each new project's Postgres, so serverless functions that open a connection per request don't overwhelm the database. The pooler keeps a fixed set of connections open, currently a pool size of 15, and queues clients when all are busy rather than raising Postgres's connection limit. Three pool modes are available: session (full feature support), transaction (recommended for serverless, but no session features like prepared statements) and statement (no multi-statement transactions, suited to autocommit). Pooling applies to new projects only.
- Battling database performance
incident.io mediumAdeptIntermittent API timeouts at incident.io over two weeks traced back to requests blocking on Go's database/sql client-side connection pool, with no single slow query or pool to blame. The team shipped 24 changes along the way. These included materialised views, new indexes, query rewrites, a one-second lock timeout on transactions, and moving Slack webhook handling onto PubSub. They also built per-operation tracking of connection-hold time using an ngrok/sqlmw middleware and context counters that avoid double-counting inside transactions. The root cause turned out to be an unnecessary transaction wrapped around every Slack modal submission. Many short transactions added together had exhausted the pools, and removing it ended the timeouts.
- How to Minimise the Impact of Database Latency
Neon mediumAdept
Database round-trip latency compounds with every sequential query, so co-locating the backend with the database and cutting round trips matters more than raw query speed. Neon's open-source Regional Latency Dashboard gives P50/P99 latencies between hosting providers and Neon regions, which can be multiplied by query count to estimate N+1 costs. The fixes are JOINs or bounded IN queries instead of N+1 loops, Django's select_related/prefetch_related, batching queries in one transaction (45% faster in a simple benchmark), Postgres RETURNING and ON CONFLICT clauses, connection reuse (8-9x lower response times in a Django sample), and application-level connection pools (10x+ throughput).