Concurrency Client Wrapper
Difficulty: MEDIUMCategory: LLDFrequency: 1Asked On 2022-03-17
Problem Statement
Design and implement a thread-safe client wrapper that provides concurrent access to an underlying service/client.
Design and implement a Concurrency Client Wrapper that provides a configurable, thread-safe, concurrent access layer over an underlying client (e.g., REST, DB, File I/O). It should support synchronous/asynchronous calls, limit max concurrency, handle retries/failures gracefully, and ensure proper resource management under heavy load.
The wrapper should manage multiple threads making requests to the underlying client, ensuring thread safety, controlled concurrency, and proper error handling.
🔶 Requirements
1.<span></span>Concurrency Control
2.<span></span>The wrapper should allow multiple threads to make requests concurrently.
3.<span></span>The maximum number of concurrent requests should be configurable.
4.<span></span>If the limit is reached, additional requests should be queued until capacity is available.
5.<span></span>Thread Safety
6.<span></span>All shared resources (like the underlying client instance, connection pool, response buffers) must be thread-safe.
7.<span></span>No race conditions, deadlocks, or resource leaks should occur.
8.<span></span>Request Handling
9.<span></span>Support both synchronous (blocking) and asynchronous (non-blocking with callback/future) calls.
10.<span></span>Provide a way to cancel pending requests.
11.<span></span>Retry mechanism for failed requests (configurable).
12.<span></span>Error Handling
13.<span></span>If the underlying client throws exceptions, they should be propagated gracefully.
14.<span></span>Properly handle timeouts and retries.
15.<span></span>Resource Management
16.<span></span>Maintain a connection/session pool if required.
17.<span></span>Allow graceful shutdown, ensuring pending requests complete or are safely terminated.
18.<span></span>Monitoring & Logging
19.<span></span>Log request start, completion, failures, and retries.
20.<span></span>Provide metrics: active requests, queued requests, request latency, failure rate.
🔶 Example Use Cases
1.<span></span>A Database Client Wrapper that supports concurrent queries with a maximum of N active connections.
2.<span></span>A REST API Client Wrapper that ensures only M requests are sent at a time to avoid overwhelming the server.
3.<span></span>A File I/O Client Wrapper that allows parallel reads/writes but ensures consistency of shared data.
🔶 Constraints
1.<span></span>Maximum concurrency should be configurable (max_concurrent_requests).
2.<span></span>Queue size for pending requests should also be configurable (max_queue_size).
3.<span></span>Support graceful degradation when queue is full (reject new requests or block until space is available).
4.<span></span>Must work in a multithreaded environment with at least 100 concurrent threads.
🔶 Expectations
Your design should:
1.<span></span>Define the class structure for the client wrapper.
2.<span></span>Clearly specify the synchronization mechanisms (locks, semaphores, thread pools, etc.).
3.<span></span>Allow easy extension (e.g., swapping underlying client or changing concurrency control policy).
4.<span></span>Provide a driver program to demonstrate multiple threads making requests concurrently.
5.<span></span>(Optional) Write unit tests to validate correctness under race conditions.