---
title: "How to Handle Database Connection Pooling"
description: "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."
url: "https://swe.quest/articles/how-to-handle-database-connection-pooling-1d65f2947c3645f4"
canonical: "https://blog.railway.com/p/database-connection-pooling"
source: "Railway"
published: "2024-12-13"
difficulty: "medium"
kind: "How-to"
reading_minutes: 13
tags: ["postgres","javascript","performance","databases"]
---

# How to Handle Database Connection Pooling

A post on [Railway](https://swe.quest/sources/railway.md), published 2024-12-13, 13 min read, How-to. Original: [https://blog.railway.com/p/database-connection-pooling](https://blog.railway.com/p/database-connection-pooling)

## Summary

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.

Written by swe.quest, not by the author.

**Difficulty:** Medium: assumes a working developer; shows how it is built or done

## Topics

- [PostgreSQL](https://swe.quest/tags/postgres.md) (Data & databases)
- [JavaScript](https://swe.quest/tags/javascript.md) (Languages)
- [Performance engineering](https://swe.quest/tags/performance.md) (Reliability & operations)
- [Databases](https://swe.quest/tags/databases.md) (Data & databases)

## Prerequisites

- Node.js and Express basics ([easy posts on JavaScript](https://swe.quest/tags/javascript.md?difficulty=easy))
- PostgreSQL basics ([easy posts on PostgreSQL](https://swe.quest/tags/postgres.md?difficulty=easy))
- async/await in JavaScript ([easy posts on Concurrency](https://swe.quest/tags/concurrency.md?difficulty=easy))

## Keywords

PostgreSQL, node-postgres, Prometheus, prom-client, Express, Railway

## Related posts

- [Supavisor: Scaling Postgres to 1 Million Connections](https://swe.quest/articles/supavisor-scaling-postgres-to-1-million-connections-8585e4b9712f0334.md): Supabase · 2023-08-11 · Medium · 7 min read · PostgreSQL, Elixir & Erlang, Scaling, Multi-tenancy, Performance engineering
  Supavisor is a multi-tenant Postgres connection pooler written in Elixir that Supabase built to replace single-threaded PgBouncer.
- [Using Neon’s Scale to Zero with Long-Running Applications](https://swe.quest/articles/using-neon-s-scale-to-zero-with-long-running-applications-bbe64dd8466d86d8.md): Neon · 2024-01-24 · Medium · 7 min read · PostgreSQL, Serverless, Resilience, JavaScript, Performance engineering
  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.
- [PgBouncer is now available in Supabase](https://swe.quest/articles/pgbouncer-is-now-available-in-supabase-7611d240679e4c4c.md): Supabase · 2021-04-02 · Medium · 4 min read · PostgreSQL, Serverless, Scaling
  Supabase 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.
- [Battling database performance](https://swe.quest/articles/battling-database-performance-5c5eb431f69f9020.md): incident.io · 2023-04-20 · Medium · 10 min read · PostgreSQL, Performance engineering, Observability, Go, Incidents & postmortems
  Intermittent 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.
- [How to Minimise the Impact of Database Latency](https://swe.quest/articles/how-to-minimise-the-impact-of-database-latency-bc4c9682c5942467.md): Neon · 2024-06-07 · Medium · 9 min read · PostgreSQL, Performance engineering, SQL, Serverless
  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.

Browse: [Latest posts](https://swe.quest/index.md) · [Topics](https://swe.quest/tags.md) · [Sources](https://swe.quest/sources.md) · [Topic map](https://swe.quest/map.md) · [Site guide (llms.txt)](https://swe.quest/llms.txt)
