Implement a Bounded Object Pool to Reduce Allocation Churn
Last updated: November 29, 2026
Quick Overview
A high-throughput service allocates large buffers per request, causing garbage collection pauses. Implement a thread-safe bounded object pool with acquire, release, a context manager, reset on return, and a timeout when the pool is exhausted.
Microsoft
November 29, 20260
0
0 solved
A high-throughput service allocates large buffers per request, causing garbage collection pauses. Implement a thread-safe bounded object pool with acquire, release, a context manager, reset on return, and a timeout when the pool is exhausted.
Performance-focused Microsoft teams (for example .NET runtime, Bing serving, or Azure storage front ends) care about allocation rates and GC pressure, which is why ArrayPool and ObjectPool exist in .NET. The interviewer checks thread safety, building objects outside the lock, resetting state so data never leaks between requests, and handling broken objects.
What the Interviewer Expects
- Bounds total objects and blocks or times out when exhausted
- Builds new objects outside the lock while still reserving a slot
- Resets objects on release so no request sees another's data
- Drops objects that fail reset and frees their slot
- Offers a context manager so release cannot be forgotten
- Explains when pooling helps and when it hurts
Key Topics to Cover
How to Approach This
- Apply SOLID principles. Single Responsibility makes code testable, Open/Closed makes it extensible.
- Choose data structures based on access patterns, not familiarity.
- Prefer immutable data and message passing over shared mutable state for concurrency.
- Design APIs with RESTful conventions, versioning, meaningful errors, and pagination from day one.
Possible Follow-up Questions
- How would you measure whether the pool actually reduced GC time?
- What goes wrong if a caller keeps using an object after releasing it?
- How would you shrink the pool after a traffic spike?
- Why might a per-thread cache beat a shared pool?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice System Design ProblemsSample Answer
Requirements
ObjectPool(factory, reset, max_size).acquire(timeout)returns an idle object, or creates one if fewer thanmax_sizeexist, or waits; raisesTimeoutErrorif none frees in time.- `release(...
High-Level Design
A Condition guards an idle list and a created counter. Acquire waits on "idle object available or room to create." To avoid holding the lock during a slow constructor, the thread **reserves a slot...