Skip to content

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
Asked at
Practice this question

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

AskWhy it mattersA 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 POST /v1/infer and the connection stays open until the output is ready. You can propose an async variant, but the main design holds the caller.

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.

The requirements menu

RequirementScopeWhy

Accept a synchronous request and hold the caller until its answer is ready

Core

The shape of the whole design: something must keep the connection open while the work travels through a queue, a batch and a GPU, then find its way back.

Group requests into batches of up to 100 inputs for one GPU

Core

The reason the service exists. Batching is what makes a GPU worth its cost, and the batch size cap sets throughput per GPU.

Return each output to the caller that sent the input

Core

A batch returns outputs in input order, so the design has to remember which request sat in which slot and which front end holds that caller.

Rate limit callers and retry failed batches

Core

Rate limits keep one caller from filling the queue; retries keep a transient GPU error from becoming a caller error.

Lose no queued work when a node dies

Core

A caller who waited is owed an answer or an explicit error, so an accepted request has to survive the death of a batcher or a GPU worker.

Return 429 with a Retry-After hint when queue depth crosses its limit

Optional

Worth taking: it's a few lines at the front end and it is the answer to the burst question. Without it, a burst makes callers wait past their own timeouts and then retry, which deepens the queue.

An asynchronous job id for callers that can poll

Optional

Useful for long inputs and bulk jobs, and simpler for failover because no connection has to survive. Leave it as a variant unless asked, since the question is about the synchronous path.

Priority lanes for callers who pay for speed

Optional

A natural extension and this question's closing twist. Take it if asked; it means separate queues or a weighted pick when a batch is filled.

Deduplicate identical inputs inside a batch

Optional

Tempting when the same prompt arrives again and again, but it complicates the output mapping and saves GPU slots only when duplicates land in the same batch. Mention it and leave it.

Store each request and its output permanently

Out of scope

The queue holds work in flight and a result is read once by its waiting caller. A record of everything is an audit or analytics feature, which is a separate pipeline.

Several models or model versions on the same fleet

Out of scope

Real in production, but it adds a routing dimension that doesn't change the batching answer. Say that a batch holds inputs for one model and stop there.

The model, its kernels and the GPU internals

Out of scope

The model is a black box that takes a batch and returns outputs. Time spent on kernels is time not spent on the queue and the return path.

Training

Out of scope

A different workload with different scheduling. Name it and move on.

Streaming partial outputs

Out of scope

Streaming tokens changes the reply path into a stream per caller. Say how the reply channel would carry chunks, then park it.

Non-functional requirements

RequirementWhat 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

QuantityWorkingResultWhat 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

EntityWhat 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

EndpointNotes

POST /v1/infer { input } → { output }, held open until done

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 429 when the queue is past its bound. Let the caller pass its own id so a retry after a timeout can be matched to the first attempt.

Worker: run(batch) → outputs in input order

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.

GET /v1/health on each GPU worker

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

inferbatchrequest, idoutputs by idCallersFront endsBatcherGPU workers

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.

POST /v1/infer429 when fullrequest, idbatchpoison inputoutputsreply channelheartbeatexpiredCallersFront endsRate limit,queue boundReplicated queueBatcher: 100 ordeadlineGPU workersDead lettersResult routerLease table
The whole design

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?

A concept that helps here: GPU batching and schedulingA concept that helps here: Stating a trade-offA concept that helps here: Back-of-the-envelope estimation

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?

A concept that helps here: Routing messages between stateful serversA concept that helps here: Idempotency keysA concept that helps here: Delivery semantics: at most, at least, exactly once

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?

A concept that helps here: Distributed locks and leasesA concept that helps here: Retries and dead-letter queuesA concept that helps here: Backpressure and load shedding

Concepts this guide uses

Sources

  1. Aced: Anthropic system design interview guide
  2. NVIDIA Triton Inference Server: Dynamic batching and concurrent model execution
  3. Anyscale: How continuous batching raises LLM inference throughput while reducing median latency
  4. Amazon SQS developer guide: Visibility timeout
  5. Amazon SQS developer guide: Using dead-letter queues
  6. Apache Kafka wiki: Kafka replication design
  7. Stripe API reference: Idempotent requests
  8. MDN: 429 Too Many Requests
  9. The Morning Paper: The Tail at Scale (a summary of Dean and Barroso)
Sign in to report a mistake