Question presented to candidate: "Every incoming request opens a brand-new database connection, runs one query, and closes it. Under moderate load, response times get noticeably worse even though the queries themselves are simple. What is actually slow here?"
What a strong answer should cover:
- Establishing a database connection has a real, non-trivial cost — a network handshake, authentication, sometimes TLS negotiation — distinct from and often larger than the cost of the actual query itself. Opening a fresh connection per request pays this cost repeatedly, on every single request, rather than once.
- 📌 Verified, not assumed: a real, measured comparison — 10 sequential queries, each with a simulated 20ms connection cost plus a 5ms query cost — took 374ms without pooling (a fresh connection every time) versus 231ms with a pool of 5 reused connections — a genuine, measured improvement, reported honestly rather than inflated into an idealized clean multiple.
- A connection pool maintains a fixed set of already-established connections, handing one out (
acquire) to serve a query and returning it (release) to the pool afterward for the next request to reuse — the expensive connection-establishment cost is paid once per pooled connection, not once per query. - The pool's size is a real, tunable trade-off: too small, and requests queue waiting for a connection to free up under load (a real, measurable bottleneck); too large, and the database itself may be overwhelmed by more simultaneous connections than it can efficiently handle — the right size depends on the database's own connection limits and the application's actual concurrency needs, not an arbitrary default.
- A precise answer names that most database drivers/ORMs already provide connection pooling built in (
pg'sPool, Mongoose's default connection management, Prisma's connection pool) — the common mistake is bypassing that built-in pooling by manually creating a fresh client connection per request, exactly the anti-pattern in the prompt's scenario, rather than configuring and reusing the pool the driver already offers. - A precise answer also connects this to serverless/Lambda environments (covered in its own dedicated question) — connection pooling behaves genuinely differently there, since a traditional in-process pool does not persist reliably across separate function invocations the way it does in a long-running server process.
Clarifying questions expected:
- "Is the connection actually being manually created per request, or is the existing driver's built-in pool simply misconfigured/unused?" — often the real, fixable root cause.
- "Is this running in a traditional long-running server, or a serverless/Lambda environment?" — connection pooling behaves genuinely differently in each, covered in its own dedicated question.
Code / implementation expected: Yes — the real, measured before/after timing (374ms vs. 231ms for the identical 10 queries) is the concrete, convincing proof of the actual cost being avoided, not a description of "pooling is faster."