Scaling and changing databases · mid
Connection pooling and 'too many clients'
Each database connection costs memory. When many pods each hold a big pool, the total passes max_connections. A pooler such as PgBouncer shares a few server connections among many clients.
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceThe notes
What you must know
- The maths: pods times pool size per pod (for example 60 times 20 is 1,200) can be far above max_connections, and most connections sit idle.
- Each PostgreSQL connection is a process with its own memory, and every sort can use work_mem. Thousands of connections hurt even when idle.
- Immediate steps: cap the autoscaler, reduce the pool per pod, end idle sessions, and keep a few connections for admins.
- PgBouncer in transaction mode gives a server connection only for the length of one transaction. A few dozen server connections can serve thousands of clients. Other options: RDS Proxy, Supavisor, PgCat.
- What breaks in transaction mode: session state such as SET (use SET LOCAL), session advisory locks, LISTEN, temporary tables and WITH HOLD cursors. Check your PgBouncer version for prepared statement support.
- Size the server pool as a small multiple of the database CPU cores. Keep application pools small. Watch cl_waiting.
- MySQL uses a thread per connection and ProxySQL is a common pooler.
- Prevent it: connection maths in deploy reviews, an alert at 70% of max_connections, load tests that include autoscaling.
The red pen
Common traps: mistake → fix
Mistake: Raise max_connections to thousands
Fix: more connections use more memory and slow everything.
Mistake: A pool of 50 per pod is safe
Fix: multiply by the number of pods.
Mistake: Transaction pooling keeps session settings
Fix: a SET may apply to another client's next transaction.
Easy to hard
Questions interviewers ask, easy to hard
- Why did the database refuse connections when pods went from 10 to 60?
- What does PgBouncer do?
- What breaks in transaction pooling mode?
- How do you size pools?
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceYour first interview is free
Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.