Design a Request Batcher
📦 What is a request batcher? It is a small layer between many callers and a backend that prefers bulk requests. Each caller asks for one thing and waits. The batcher collects the waiting requests, sends them to the backend as one call, splits the answer back up, and wakes every caller with its own piece.
Batching shows up wherever a round trip costs more than the work inside it: database drivers that group inserts, message producers that send records in bundles, data loaders that turn many "get user 7" lookups into one "get users 3, 7, 12". In an interview it is a compact concurrency problem. There is no big class model to draw. Instead, the whole grade rests on three things: waking the right caller with the right result, flushing on two different triggers without a race between them, and shutting down without losing anyone who is still waiting.
Requirements
"Our pricing service is slow per call, but it accepts up to 100 product ids at once. Lots of threads ask it for one price each. Build something that batches those calls."
The prompt names the goal (fewer calls) but none of the rules. Each question below settles one of them.
Clarifying Questions
You: What does the backend look like to us?
Interviewer: One function. You pass it a list of values, it returns a list of results of the same length, in the same order. results[i] is the answer for values[i].
You: When do we send a batch? Only when it is full?
Interviewer: When it is full, yes. But traffic is uneven, and at night a batch may never fill. Nobody should wait more than a set time, so also send whatever is waiting once the oldest request has waited that long.
You: Does that time window start when the first request of a batch arrives, or is it a fixed tick every few milliseconds?
Interviewer: From the oldest request that is still waiting. That way the delay we add to any one request is bounded.
You: Should a caller block until its answer is ready, or get a handle back right away?
Interviewer: Block, and return the result. Keep the caller's code simple. We can talk about an async version later.
You: What happens if the backend call fails?
Interviewer: Every caller in that batch gets -1. No retries for now.
You: How do we shut it down?
Interviewer: A close() call. After it, new requests are turned away with -1, but every request that was already waiting must still get its answer.
You: Can two batches be at the backend at the same time?
Interviewer: No, one call at a time is fine for now.
The third answer is the one people skip. A fixed tick ("flush every 10 ms") is simpler to code, but a request that arrives just after a tick still waits a full period, and the tick fires even when nothing is waiting. Measuring from the oldest request gives a clean promise: no request waits longer than the window before its batch is sent.
💡 Interview tip: In a concurrency problem, ask what the caller sees: does it block, get a future, or pass a callback? That one answer decides the shape of the public API and which waiting tool you need.
Final Requirements
- The batcher is built with a maximum batch size, a maximum wait in milliseconds, and a handler that maps a list of values to a list of results of the same length.
submit(value)blocks the calling thread and returns the result for that value.- As soon as the maximum batch size of requests is waiting, they are sent as one batch.
- A smaller batch is sent once its oldest request has waited the maximum wait.
- A batch never holds more than the maximum size or fewer than one request, and requests are batched in the order they arrived.
- Every caller gets the result for its own value. If the handler fails, every caller in that batch gets
-1. pending()reports how many requests are waiting to be sent.close()turns away new requests with-1, flushes everything still waiting, and returns once all of those callers have their results and the background work has stopped.
Out of scope
- Retries, timeouts on the backend call, and partial failures (we come back to some of these in Extensibility).
- Merging duplicate values inside a batch.
- More than one batch at the backend at a time.
- Batching across machines. This is one process with threads.