All the basics

What is a connection pool?

Open a few database connections once, then lend them to requests and take them back, instead of opening one each time.

A rack of shared bikes at a station: you borrow one when you need it and return it when done, so nobody buys a new bike per trip.

Press Play to watch it run · 4 steps

An app talks to a database, either opening a new connection per request or borrowing from a pool.

Borrowing database connectionsAn app talks to a database, either opening a new connection per request or borrowing from a pool.borrow10 opennew each timeRequestsYour appConnection poolDatabase

Tap any box to see what it is and what it talks to.

  • Client
  • Service
  • Queue
  • Database
Open in playground
Text description
  1. Many requests arrive at once.
  2. Each request opens its own new connection. Setup is slow, often tens of milliseconds.
  3. Each connection uses memory on the database, so too many hurt it.
  4. At its limit, the database refuses new connections. Customers see errors.
  • Opening a connection is slow, so open a few and reuse them.
  • The database can only handle so many connections.
  • When all are busy, requests wait, so set timeouts.

Reach for it when

In almost every backend that talks to a database: opening a connection can take tens of milliseconds, so reuse a few.

The catch

Ten connections shared by twenty requests means ten wait in line, and one slow query holding a connection makes everyone queue.

Say it in an interview“I would use a connection pool sized to what the database can handle, with a wait timeout, and keep queries short so connections return quickly.”