Implement a Database Connection Pool
Database Connection Pool Implementation
Implement a simple database connection pool that allows callers to safely acquire and release database connections.
You will be provided with a mock database client, such as one exposing a db.query(connection, sql) function or a similar simulated query API, to interact with connections.
Requirements
- The pool must maintain a fixed maximum number of database connections.
- The
acquire()method should behave as follows:- If an available connection exists in the pool, return it immediately.
- If no connection is available but the pool has not yet reached its maximum size, create a new database connection, add it to the pool, and return it.
- If the pool is at its maximum capacity and all connections are currently busy,
acquire()must block and wait until a connection is released and becomes available.
- The
release(connection)method must return the specified connection to the pool. If there are any threads waiting for a connection,release()should wake up one of them. - The implementation must prevent common issues such as invalid releases (e.g., releasing a connection not acquired from the pool), double releases of the same connection, and connection leaks (e.g., connections not being returned to the pool).
- The entire connection pool logic must be safe under concurrent requests from multiple threads or processes.
Discussion Points
Be prepared to discuss edge cases including:
- Handling of query failures.
- Timeouts that may occur while a caller is waiting to acquire a connection.
- Strategies for graceful shutdown of the connection pool.
AI interview prep
Core dilemma
Efficiently and safely managing a finite, shared resource (database connections) under high concurrency, balancing responsiveness for new requests with strict resource limits and preventing deadlocks or resource exhaustion.
3 tradeoffs and 3 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Implement a Database Connection Pool
Database Connection Pool Implementation
Implement a simple database connection pool that allows callers to safely acquire and release database connections.
You will be provided with a mock database client, such as one exposing a db.query(connection, sql) function or a similar simulated query API, to interact with connections.
Requirements
- The pool must maintain a fixed maximum number of database connections.
- The
acquire()method should behave as follows:- If an available connection exists in the pool, return it immediately.
- If no connection is available but the pool has not yet reached its maximum size, create a new database connection, add it to the pool, and return it.
- If the pool is at its maximum capacity and all connections are currently busy,
acquire()must block and wait until a connection is released and becomes available.
- The
release(connection)method must return the specified connection to the pool. If there are any threads waiting for a connection,release()should wake up one of them. - The implementation must prevent common issues such as invalid releases (e.g., releasing a connection not acquired from the pool), double releases of the same connection, and connection leaks (e.g., connections not being returned to the pool).
- The entire connection pool logic must be safe under concurrent requests from multiple threads or processes.
Discussion Points
Be prepared to discuss edge cases including:
- Handling of query failures.
- Timeouts that may occur while a caller is waiting to acquire a connection.
- Strategies for graceful shutdown of the connection pool.