Build a Bounded Database Connection Pool With Acquire Timeouts
Last updated: November 29, 2026
Quick Overview
Implement a thread-safe pool that lazily creates up to N database connections, reuses idle ones, discards broken ones, and makes callers wait with a deadline when the pool is exhausted.
Amazon
November 29, 20260
0
0 solved
Implement a thread-safe pool that lazily creates up to N database connections, reuses idle ones, discards broken ones, and makes callers wait with a deadline when the pool is exhausted.
Order and payment services at Amazon talk to Aurora and RDS through connection pools, and pool exhaustion is a common cause of brownouts. The interviewer checks condition-variable use, deadline arithmetic, and whether you keep slow work such as opening a connection outside the lock.
What the Interviewer Expects
- Caps total connections, not just idle ones
- Uses a condition variable with a deadline-based wait loop
- Creates connections outside the lock while reserving a slot
- Validates idle connections and replaces broken ones
- Offers a context manager so release always happens
Key Topics to Cover
How to Approach This
- Apply SOLID principles. Single Responsibility makes code testable, Open/Closed makes it extensible.
- Choose data structures based on access patterns, not familiarity.
- Prefer immutable data and message passing over shared mutable state for concurrency.
- Design APIs with RESTful conventions, versioning, meaningful errors, and pagination from day one.
Possible Follow-up Questions
- How would you size the pool for a service with known latency and request rate?
- How would you evict connections idle for too long?
- What metrics would you emit to catch pool exhaustion early?
- Why can a too-large pool hurt the database?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice System Design ProblemsSample Answer
Requirements
acquire(timeout)returns a connection or raisesPoolTimeout.- At most
max_sizeconnections exist at any time. - Broken connections are not returned to the idle list.
- Opening a connection, w...
High-Level Design
Keep an idle stack and a created counter under one Condition. An acquirer first pops idle connections and validates them; if none, and created < max, it reserves a slot by incrementing the count...