Data, ORMs and caching · mid
Database connection pooling
Opening a database connection is slow and each one costs the database memory, so a server keeps a small pool of open connections and lends them to queries, and you size the pool for the database's limit across all instances, which is the main problem with serverless functions.
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
- A Postgres connection is a separate server process (several MB). max_connections defaults to 100, shared by every app instance, job worker and admin tool.
- The pool (pg.Pool, the ORM's pool): acquire a connection, run the query, release it. When all are busy, requests wait in a queue; set a wait timeout.
- Sizing: total = pool size x instances (x serverless concurrency). 20 pods x 20 connections = 400 > 100: errors. Bigger pools do not mean faster: the DB has limited cores.
- A transaction must run on one connection: take a client from the pool, BEGIN ... COMMIT, then release it. Forgetting to release leaks connections until the pool is stuck.
- Serverless (Lambda, Vercel functions) can open thousands of connections during a spike: use a pooler (PgBouncer, RDS Proxy, Supabase/Neon poolers), or an HTTP-based driver.
- Create one pool or ORM client per process at startup, not per request.
The red pen
Common traps: mistake → fix
Mistake: A bigger pool is always faster
Fix: past the DB's capacity, more connections add contention and memory pressure.
Mistake: Open a new connection per request, it is simpler
Fix: the connect handshake is slow and you hit max_connections under load.
Easy to hard
Questions interviewers ask, easy to hard
- What is a connection pool and why do we need one?
- How do you choose the pool size?
- Why do serverless functions and relational databases struggle together? How do you fix it?
- The API hangs and the logs say 'timeout acquiring connection'. What do you check?
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.