Design an Offline Batch Inference Service
Understanding the Problem
📦 What is offline batch inference? Offline batch inference runs a model over a large dataset where nobody is waiting on any single answer: classify 50 million support tickets, embed a document corpus, summarize every product review, or score last night's transactions. The user hands over a dataset, a model and usually a small script, and gets a results file back hours later. Latency per record doesn't matter; throughput, cost and finishing reliably do.
You'll hear it phrased several ways: "design a batch API for an LLM provider" (upload a JSONL file of prompts, get results within 24 hours), "design a platform for teams to run models over big datasets," or "design a Spark-like system for GPU inference." They're the same problem. The provider version has external customers and files; the internal version adds user scripts and data in tables. We'll design for the general case: a job is a dataset, a model and an optional user script.
Functional Requirements
Core Requirements
- Users should be able to submit a batch job: an input dataset, a model, an optional pre/post-processing script, and an output location.
- The system should run the job across a shared GPU pool and write the results.
- Users should be able to see a job's status and progress (records done, failed, ETA) and cancel it.
- Users should get a clear report of which records failed and why, and be able to re-run just those.
Below the line (out of scope)
- Online, low-latency serving (see /ml-system-design/llm-inference).
- Training or fine-tuning models.
- Building the input datasets; we read whatever the user points us at.
Non-Functional Requirements
Ask about the workload. For our design: about 1,000 jobs a day, from a few thousand records to 50 million, adding up to roughly 200 million records a day. Most jobs run an LLM (an 8B model for classification and extraction, sometimes a 70B model); some run embedding or vision models. The promise to users is a completion window of 24 hours, and most jobs finish much faster.
Core Requirements
- The system should finish each job within its completion window (24 hours by default) and give an honest ETA.
- The system should never lose completed work: a crash, a preempted GPU or a bad record must not force a job to start over.
- The system should keep GPUs busy, above 70-80% utilization during a job, since GPU time is the cost.
- The system should share GPUs fairly across teams and jobs, and let urgent work go first.
- The system should contain failures in user code: one bad script or record shouldn't take down a worker, and a broken script should fail fast, not burn a day of GPU time.
Below the line (out of scope)
- Exact-once semantics across external side effects the user's script performs (calling other APIs). We guarantee our output files, not their side effects.
- Cross-region data residency rules beyond "run in the region the data lives in."
Let's size a big job, because it sets the shard size and the fleet. Take 50 million documents, 800 input tokens and 100 output tokens each, on an 8B model:
Prefill: 50M × 800 tokens = 4 × 10^10 tokens
× 2 FLOPs × 8B params = 6.4 × 10^20 FLOPs
at ~400 TFLOP/s effective per H100 → 1.6M GPU-seconds ≈ 440 GPU-hours
Decode: 50M × 100 tokens = 5 × 10^9 tokens
at ~10,000 tokens/s per GPU (large batches, no latency SLO) ≈ 140 GPU-hours
Total ≈ 600 GPU-hours → about 6 hours on 100 GPUs
Two things fall out. First, prefill dominates for this kind of job (long inputs, short outputs), so the job is compute-bound and big batches are exactly what we want. Second, 600 GPU-hours must be cut into pieces. If we split into shards of 10,000 records, we get 5,000 shards of about 7 GPU-minutes each: short enough that losing one costs little, long enough that per-shard overhead is noise.
The key difference from online serving: there's no latency SLO per request, so we can run much larger batches and push GPUs to their throughput limit. That's why providers can offer batch APIs at a large discount. Say this early; it justifies building a separate system instead of just calling the online endpoint in a loop.