How to design an LLM inference batching service
The first sections are free. The deep dives, level expectations and quiz are part of Pro.
On this page
- The problem
- Clarifying questions
- The requirements menu
- Non-functional requirements
- Estimates
- Core entities
- The API
- The high-level design
- What breaks first
- The deep dives
- Entity fields
- Batching and latency
- Routing answers back
- Failures without lost work
- Variants
- What each level is expected to show
- The 30-second answer
- Quiz
The problem
An inference batching service takes one request at a time from callers who wait on an open connection, packs those requests into batches of up to 100 inputs so a GPU runs them together, and hands each output back to the caller it belongs to. The answer turns on two decisions: when a batch leaves for the GPU (full, or on a short deadline so a lone request at night isn't stranded) and how a finished batch's outputs find the front ends still holding the callers. The model stays a black box; the work is a queue, a batcher, a lease on each batch and a return path keyed by request id.
Clarifying questions
| Ask | Why it matters | A typical answer |
|---|---|---|
Do callers wait on the connection for the answer, or do they get a job id and poll? | It decides whether the design needs a route from the GPU's outputs back to a held connection, which is the hardest part of this question, or a results store that callers read later. | Synchronous: the caller sends |
How many inputs fit in one batch, and how long does a batch take on the GPU? | Batch size divided by batch time is one GPU's throughput, and the fleet size and the latency figures follow from it. | Up to 100 inputs per batch. In one version of the question each batch takes about 100 ms on the GPU regardless of how full it is. |
What request rate should I design for, and how even is it? | A steady rate sizes the fleet; bursts and quiet hours decide the batching policy, since a batch that waits to fill at night strands the requests in it. | About 100,000 requests per second at peak in the token-service version. Traffic comes in bursts by day and trickles in at night. |
Is each request one fixed forward pass, or does the model generate tokens one step at a time? | A fixed pass lets you batch whole requests. Token generation finishes requests at different steps, which changes how the batch is managed on the worker. | Treat the model as a black box that runs one batch in one call. If you know continuous batching, say where it would change the worker, and move on. |
What latency does a caller accept, and do we care about the median or the slow tail? | The tail decides how long a batch may wait for more inputs and how fast a dead node's requests must be recovered. | No fixed number is given; p99 matters more than the median because each caller is waiting. Propose a budget and defend it. |
Is it safe to run the same input twice? | Retries after a node death or a caller timeout mean an input may run again. If a second run is harmless, recovery gets much simpler. | Yes. Inference has no side effects here, so a duplicate run wastes GPU time and nothing else. Callers reuse their request id on a retry. |
Do some callers deserve faster service than others? | Priority lanes change the queue from one line into several and affect how batches are filled. | Not in the first version. Say how you'd add it if asked; this question's closing twist asks for it. |
Non-functional requirements
| Requirement | What it forces |
|---|---|
Up to 100 inputs per batch on one GPU | The batcher closes a batch at no more than 100 inputs, so one GPU's throughput is capped at batch size over batch time, and the fleet size comes from that ceiling. |
In one variant, each batch takes a fixed 100 ms on the GPU | A half-empty batch costs the same GPU time as a full one, so a full batch is the cheapest way to serve a request, and the batcher's wait is a deliberate trade of latency for GPU use. |
No accepted request is lost when a node dies | Requests live in a replicated queue, not in a batcher's memory, and a GPU worker runs its batch under a lease so a dead worker's requests go back to the queue. |
Tail latency (p99) matters more than the median | The batch deadline is short, a lost lease is detected in a few batch times rather than minutes, and a caller wait has its own deadline so the slow tail becomes an error rather than a hang. |
Estimates
| Quantity | Working | Result | What it sizes |
|---|---|---|---|
Throughput of one GPU and the fleet size | 100 inputs per batch ÷ 100 ms per batch = 1,000 inputs a second per GPU; 100,000 requests per second ÷ 1,000 = 100 GPUs at full batches; if traffic grows ten times, 1,000,000 a second ÷ 1,000 = 1,000 GPUs | Roughly 1,000 requests a second from each GPU, which means roughly 100 GPUs at peak with no headroom, and roughly 1,000 if traffic grows ten times | The GPU fleet. Measure the batch time on the real model rather than assuming it; partial batches at night lower the effective rate. |
Batches a second | 100,000 requests per second ÷ 100 per batch = 1,000 batches a second across the fleet; 1,000 ÷ 100 GPUs = 10 batches a second per GPU; one second ÷ 1,000 batches = 1 ms per batch for a single batcher | About 1,000 batches a second in total, 10 per GPU; a single batcher forming them all would see a new batch due about every 1 ms, a budget to measure the batching cost against | Whether one batcher can keep up, and why the design shards batchers by GPU group. |
Batch deadline and a lone request's latency | Pick a deadline of a tenth of the batch time: 100 ms ÷ 10 = 10 ms; a lone request waits 10 ms, then 100 ms on the GPU, so about 110 ms | A 10 ms deadline adds at most 10 ms to whichever request opened the batch, and a lone request at night sees about 110 ms | The deadline setting. It is a choice, not a measurement: a longer deadline fills batches better and lengthens the tail. |
Requests in flight and queue memory | 100,000 a second × 100 ms on the GPU = 10,000 requests on GPUs at any moment; assuming about 1 KB per request, 10,000 × 1 KB = 10 MB; a one-second backlog is 100,000 × 1 KB = 100 MB | About 10,000 requests on GPUs at once, about 10 MB; each second of backlog adds about 100 MB | The queue. Even a backlog fits in memory on a few nodes; durability, not size, is why the queue is replicated. |
Backlog from a burst of ten times the traffic | 1,000,000 arrivals a second minus 100,000 drained a second = 900,000 excess per second of burst; 900,000 ÷ 100,000 drained a second = 9 seconds to clear each second of burst | Each second of a ten-times burst adds 900,000 requests that take about 9 seconds to drain | Why the front ends must push back: a burst a few seconds long can push queued requests past a caller's timeout. |
Lease length and the delay a dead worker adds | A lease of three batch times: 3 × 100 ms = 300 ms; a dead worker's requests wait up to 300 ms for the lease to expire, then about 100 ms to run again: done at about 400 ms where a normal run finishes at about 100 ms | With a 300 ms lease, the 100 requests on a dead worker arrive up to about 300 ms later than they would have | The heartbeat and lease interval, and the tail latency a node death adds. |
Open connections held by the front ends | Assume a round trip of about 200 ms per request: 10 ms of batch wait, 100 ms on the GPU, and the rest allowed for the queue and network hops; 100,000 a second × 200 ms = 20,000 connections open at once | About 20,000 connections open across the front ends at peak | How many front ends and how much memory per held connection; a deeper queue raises this in proportion. |
Load on a shared results store that front ends poll | 100,000 writes a second from the workers; if each of 20,000 waiting connections is polled once per 10 ms, 20,000 × 100 polls a second = 2,000,000 reads a second | About 100,000 writes and about 2,000,000 reads a second against one store | Why polling a shared store for results loses to pushing each output to the front end that holds the caller. |
Core entities
| Entity | What it holds |
|---|---|
Request | One input from one caller, with what the system needs to route its output home. |
Batch | Up to 100 requests sent to one GPU worker as a unit, with the order that maps outputs back to requests. |
GPU worker | One process on one GPU that runs a batch and reports that it is alive. |
The API
| Endpoint | Notes |
|---|---|
| The front end assigns or accepts a request id, checks the caller's rate limit, stamps a deadline, and parks the connection. It answers with the output, with an error when the deadline passes, or with |
| The internal call from batcher to GPU worker. Outputs come back positionally, so the batch's request id list is the map from slot to caller. In this design, a failure for a single input comes back as an error in that slot, so the rest of the batch still returns. |
| Used by the batcher to stop sending to a sick worker and by the lease table as a second signal beside heartbeats. Report the current batch and lease so an operator can see what a dead worker was holding. |
The high-level design
Step 1 of 5
Front ends, a batcher and GPU workers
Start with the straight line. Stateless front ends take POST /v1/infer, give each request an id, and hold the connection. A batcher collects requests and closes a batch once it holds 100 inputs or once its oldest member has waited out a deadline, whichever comes first, so a lone request at night still leaves. GPU workers run a batch and return outputs in input order; the batcher maps each slot back to its request id and answers the front end. At 1,000 requests a second per GPU, 100,000 a second needs about 100 GPUs, so the batcher is already fanning out to a fleet.
What breaks first
The deep dives
Batching and latency. Requests arrive one at a time, and each caller is waiting. When does a batch leave for the GPU, what does that choice cost in latency and GPU time, and how many GPUs does the peak need?
Routing answers back. A batch of 100 finishes on a GPU. Each output has to reach a caller held open by one of many front ends. How does an output find the right front end, what happens when that front end dies, and what does a retried batch do to the caller?
Failures without lost work. A GPU node dies with a batch of 100 on it while requests keep arriving at full speed. How does each of those requests still get an answer, what happens to an input that fails each time it runs, and what do the front ends do when the queue grows faster than the GPUs can empty it?
Concepts this guide uses
Sources
- Aced: Anthropic system design interview guide
- NVIDIA Triton Inference Server: Dynamic batching and concurrent model execution
- Anyscale: How continuous batching raises LLM inference throughput while reducing median latency
- Amazon SQS developer guide: Visibility timeout
- Amazon SQS developer guide: Using dead-letter queues
- Apache Kafka wiki: Kafka replication design
- Stripe API reference: Idempotent requests
- MDN: 429 Too Many Requests
- The Morning Paper: The Tail at Scale (a summary of Dean and Barroso)