Connection Pooling
Thousands of app instances, a hundred database connections.
Every service instance opens its own pool of database connections. Scale the app tier out and the database runs out of connections long before it runs out of CPU. A pooler multiplexes many clients over a small server pool.
Concepts
- max_connections
- PostgreSQL defaults to 100; every connection costs memory and scheduling.
- Transaction pooling
- PgBouncer lends a server connection per transaction, so 40 connections can serve thousands of clients.
- Session pooling
- Pins a server connection per client — it does not fix connection exhaustion.
- Autoscaling side effect
- Every new instance brings its pool: scaling the app can take down the database.
Lab
Problems
- YouTube: Viral Video — Medium — A video goes viral: 4× traffic and an exploding transcoding backlog.
- Uber: Rush Hour — Hard — A poison message stalls ride matching, then rush hour doubles traffic.
- Payment System — Hard — A third-party fraud API slows down and drags the whole payment flow with it.
- Dropbox: Monday 9 a.m. — Hard — Monday morning: sync traffic ×2.5, uploads ×2.5 — and at T+90s the metadata primary dies. Every request counts, so the system must be ready before nine.
- Booking.com: Holiday Search Surge — Hard — Holiday deals go live: searches ×5. The search index, the search tier’s threads and the availability database’s connections all run out — each for a different reason.
- Autoscaling Ate the Database — Easy — A promo ×2.5 makes the API autoscale — and every new instance opens 20 Postgres connections. Raising max_connections makes Postgres slower, not faster.
- Three Hundred Seats at MySQL — Easy — A traffic spike autoscales orders-api past MySQL’s 300 connections. A proxy fixes the count — but with its default pool it caps throughput below the load.
- Reshard Under Load — Easy — A write-heavy spike needs three times the primaries. Vitess can split shards online — and vtgate’s pool becomes the next bottleneck.
- Healthcare.gov: Launch Day — Hard — Six times the expected visitors on day one: plan browsing that was never cached, a synchronous identity vendor, a data hub that waits on three agencies, and an autoscaling tier with a fixed database.
Codelays runs in your browser: turn on JavaScript to use it.