CodePark/ questions / Connections & pooling
START PRACTICINGPRACTICE
Backend question · Databases & SQL › Connections & pooling

Why do backend apps use a connection pool with PostgreSQL instead of opening a connection per request?

Opening a PostgreSQL connection is slow and expensive, so apps reuse a few open connections instead. Opening one needs:

  • a TCP handshake
  • often a TLS handshake
  • authentication
  • a new backend process, because PostgreSQL starts a separate process for each connection.

Doing all that on every request adds latency.

Each connection also uses memory on the server. The max_connections setting (100 by default) limits how many can exist. Many connections running at once compete for CPU and locks. So past a certain point, throughput often drops.

A connection pool keeps a fixed set of open connections. It lends one to each query or transaction, then takes it back. This gives three benefits:

  • Requests reuse connections that are already open.
  • The number of connections stays limited.
  • Extra requests wait in the pool's queue instead of overloading the database.

In Node, pg.Pool, TypeORM and Prisma all use a pool by default.

  • Opening a connection is slow: TCP, TLS, authentication and a new process.
  • Each connection uses a process and memory, and max_connections sets a limit.
  • Too many active connections reduce throughput.
  • A pool reuses open connections and queues extra work.