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
Software Engineering Fundamentals
Software Engineer
Amazon
November 29, 2026
Software Engineer
Technical Screen
Software Engineering Fundamentals
Medium

0

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
connection pooling
condition variables
timeouts
resource management
Little's law
How to Approach This
  1. Apply SOLID principles. Single Responsibility makes code testable, Open/Closed makes it extensible.
  2. Choose data structures based on access patterns, not familiarity.
  3. Prefer immutable data and message passing over shared mutable state for concurrency.
  4. 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 Problems
Sample Answer
Requirements
  • acquire(timeout) returns a connection or raises PoolTimeout.
  • At most max_size connections 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...


Submit Your Answer
Markdown supported