Bounded Blocking Queue That Batches Inference Requests for a GPU Worker
Last updated: November 29, 2026
Quick Overview
Implement a thread-safe bounded queue where request handlers enqueue prompts and a GPU worker pulls batches of up to max_batch items, waiting at most max_wait seconds to fill a batch once the first item arrives. Producers must be able to time out when the queue is full, and close() must wake everyone up.
OpenAI
November 29, 20260
0
0 solved
Implement a thread-safe bounded queue where request handlers enqueue prompts and a GPU worker pulls batches of up to max_batch items, waiting at most max_wait seconds to fill a batch once the first item arrives. Producers must be able to time out when the queue is full, and close() must wake everyone up.
Inference serving at OpenAI depends on dynamic batching: GPUs are far more efficient on a batch of requests, but users notice added latency. The interviewer is checking whether you can use locks and condition variables correctly (no lost wakeups, no busy waiting) and reason about the latency versus throughput knob.
What the Interviewer Expects
- Uses one lock with condition variables rather than sleeping in a loop
- Re-checks predicates in a while loop to handle spurious wakeups
- Separates the wait for the first item from the bounded wait to fill the batch
- Gives producers a timeout so the API can shed load with a 429 instead of hanging
- Handles shutdown so blocked producers and the worker both exit cleanly
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 pick max_batch and max_wait, and should max_wait adapt to load?
- How would you batch by sequence length so one long prompt does not pad the whole batch?
- What changes if there are several GPU workers pulling from the same queue?
- How would you rewrite this with asyncio for a single-threaded server?
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
put(item, timeout)blocks while the queue holdscapacityitems; returns False on timeout so the handler can return 429.get_batch(max_batch, max_wait)blocks until at least one item exists, ...
High-Level Design
One mutex protects a deque. Two condition variables share that mutex: not_full (producers wait on it) and not_empty (the worker waits on it). Every state change notifies the side that might now ma...