Resource Pool Design
Difficulty: MEDIUMCategory: LLDFrequency: 1Asked On 2022-04-29
Problem Statement
Designing a Resource Pool Manager that efficiently manages a limited set of reusable resources (e.g., database connections, worker threads, network sockets). The pool should allow multiple clients to borrow resources, use them, and return them, while ensuring thread safety, fairness, and scalability.
Functional Requirements
1.Resource Borrowing
•Provide an API acquire() to borrow a resource.
•If a resource is available, allocate it immediately.
•If no resource is available, either block until one is released or fail with an exception (based on configuration).
2.Resource Returning
•Provide an API release(resource) to return a resource back to the pool.
•Ensure the resource is reset/cleaned before reuse.
3.Pool Initialization
•Support configurable pool size (min, max resources).
•Pre-initialize a certain number of resources.
4.Resource Validation & Eviction
•Detect and remove unhealthy/stale resources.
•Optionally create new resources if demand spikes (up to max capacity).
5.Concurrency
•Handle multiple threads borrowing/returning simultaneously without data corruption.
•Avoid deadlocks.
6.Timeouts & Blocking Behavior
•Support blocking acquire(timeout) with configurable wait time.
•Fail fast if no resources are available and blocking is disabled.
Non-Functional Requirements
•Thread-Safety → Concurrent clients should not corrupt pool state.
•Performance → Minimize lock contention.
•Scalability → Should handle hundreds/thousands of requests efficiently.
•Extensibility → Support different resource types (DB connections, threads, sockets).
Design Hints (OOP + Patterns)
•Core Classes/Interfaces:
•Resource (abstract, can be DBConnection, Socket, Worker, etc.).
•ResourcePool → manages resources.
•ResourceFactory → responsible for creating new resources.
•Concurrency Handling:
•Use BlockingQueue (producer-consumer model).
•Or use locks/condition variables for custom implementation.
•Design Patterns:
•Factory Pattern → to create new resources.
•Object Pool Pattern → core design principle.
•Singleton → single instance of pool manager.
✅ Example:
"Design and implement a thread-safe Resource Pool system in Java/C++ (like a DB connection pool). It should allow multiple clients to acquire/release resources, handle blocking/non-blocking acquire, and support pool resizing and validation of resources."