Design a Connection Pool System

Difficulty: MEDIUMCategory: LLDFrequency: 1Asked On 2024-12-14

Problem Statement

Design a connection pool system that manages a pool of reusable connections (e.g., database connections, API clients, or socket connections) to optimize resource usage and improve application performance.


The system should allow clients to borrow and return connections efficiently, handle concurrent requests safely, and support pool configuration and monitoring.


Functional Requirements (FR)


1.Connection Borrowing & Returning
•Clients can request (borrow) a connection from the pool.
•Clients return the connection after use.
•Validate that returned connections are healthy before putting them back in the pool.
2.Pool Management
•Configure maximum and minimum pool size.
•Maintain idle connections within the pool.
•Reuse idle connections to reduce overhead of creating new connections.
•Optionally create connections on demand if the pool is not full.
3.Connection Health Management
•Detect and remove broken/expired connections.
•Optionally test connection health before handing it out.
4.Timeouts & Waiting
•Clients wait if no connection is available and pool is at maximum size.
•Configure maximum wait time before throwing an exception.
5.Monitoring & Metrics
•Track metrics: active connections, idle connections, total connections, wait count, failures.


Non-Functional Requirements (NFR)


•Thread Safety – Support concurrent borrowing and returning of connections.
•High Performance – Low latency for acquiring connections.
•Scalability – Efficiently handle high number of concurrent client requests.
•Resource Efficiency – Minimize creation/destruction of expensive connections.
•Reliability – Prevent leaks and ensure all borrowed connections are returned.


Core Classes & Attributes


Class Key Attributes / Methods

Connection connectionId, status (idle, active, broken), lastUsedTime

ConnectionPool maxPoolSize, minPoolSize, idleConnections, activeConnections, borrowConnection(), returnConnection(), validateConnection()

ConnectionFactory createConnection(), destroyConnection(), validateConnection()

Metrics / Monitor activeCount, idleCount, waitCount, failureCount


APIs / Operations


•borrowConnection(timeout) → Returns an available connection or waits until timeout.
•returnConnection(connection) → Returns a connection to the pool.
•invalidateConnection(connection) → Removes a broken connection from the pool.
•getPoolMetrics() → Returns current pool metrics (active, idle, wait count).


Expectations in LLD


•Class Diagram – Show Connection, ConnectionPool, ConnectionFactory, and optional Metrics classes.
•Service Logic –
•How connections are borrowed, validated, and returned.
•Handling concurrent access using synchronization / locks / semaphores.
•Design Patterns –
•Singleton for a single global pool instance.
•Factory for creating connections.
•Proxy or Decorator for wrapping connections to monitor usage.
•Concurrency Considerations –
•Thread-safe queues for idle/active connections.
•Wait/notify or semaphore for managing blocked threads.
•Trade-offs –
•Pool size vs memory usage.
•Validation overhead vs performance.
•Eager creation vs lazy creation of connections.


✅ This LLD problem tests:


•OOP and class design skills
•Thread safety and concurrency handling
•Resource management and monitoring
•Ability to optimize performance under high load


Solution

Comments