cloudpatterns.appClassroomKatasWhiteboardBlog
Z2 · Compute & Execution

Connection pools

Connection pools

Silver

Opening a database connection is slow and costly, so a service keeps a pool of open connections and lends one to each request for as long as its database work takes. The pool caps how many requests talk to the database at once — connections in use ≈ requests per second × how long each holds one — so a slower database can drain the pool at unchanged traffic.

Use when
Any service that talks to a database, or another server, over connections that are slow to open · Many app instances or short-lived functions that would otherwise flood the database with connections — with a shared pooler or proxy
Avoid when
As a fix for slow queries — a bigger pool only lets more of them pile onto the database
Trade-offs
Reused connections save setup time and database memory, but requests wait when all of them are busy · Too small a pool makes requests wait; too large a pool swamps the database with competing work · Every instance's pool adds up at the database
Requires
Queueing & Little's law, Databases: the system of record
3 objectives · 8 misconceptions
  • explain Explain why a service reuses a pool of open connections instead of opening one per request, and what each open connection costs the database.
  • predict Predict when a pool runs out — connections in use ≈ requests per second × hold time — including when a slower database drains it at unchanged traffic, and what callers see then.
  • choose Choose a pool size that covers the peak without swamping the database, with every instance's pool together under the database's connection limit.
  • Not true: Opening a fresh database connection for every request costs next to nothing.
  • Not true: A connection pool only runs out when traffic grows.
  • Not true: When requests wait for a connection, a bigger pool is the fix.
  • Not true: A pool size that is right for one instance stays right however many instances run.
  • Not true: A database accepts as many connections as its clients open.
  • Not true: Idle connections sitting in a pool cost the database nothing.
  • Not true: When every connection is in use, a new request fails at once.
  • Not true: A pool that runs out opens extra connections beyond its maximum.

Neighborhood

On linesMachine Line

Progress is saved on this device