Exercises — Batch vs real-time inference
Before we start, one tiny reminder of the quantities the whole page leans on, so no symbol is used before it's earned:
The figure below plots exactly these two cost curves so you can see the crossover before computing it — the lavender batch curve shooting up as , the flat coral real-time line , and the mint break-even line where they meet. Its horizontal axis is utilization and its vertical axis is cost per useful prediction. Keep it in mind through all of Level 2 and 3.

Notice in the figure how the lavender curve rockets toward the top-left: that visual explosion is the edge case above. And everything to the right of the mint line (buttered region) is where batch wins — the exact inequality you'll derive in Exercise 2.2.
Level 1 — Recognition
Exercise 1.1
For each system, name the mode (batch or real-time) and the single clue that settles it: (a) A bank scores every account for churn risk every Sunday night and writes the score to a table. (b) A voice assistant transcribes the words you just spoke. (c) A photo app tags faces the instant you upload a new photo.
Recall Solution
(a) Batch — "every Sunday night" + "writes to a table" = scheduled, stored, looked up later. (b) Real-time — the input (your speech) arrives exactly when the answer is needed; slack . (c) Real-time — the photo you just uploaded didn't exist to precompute; on-demand.
Exercise 1.2
True or false, with one line of justification each: (a) Real-time inference optimises throughput above all else. (b) Staleness is a risk unique to batch inference. (c) "Batch size = 1" turns a batch pipeline into a real-time system.
Recall Solution
(a) False — real-time optimises latency (time for one request). Throughput (predictions/sec) is what batch chases. (b) True — batch computes early and consumes late, so the world can change in between. Real-time computes at consumption, so no staleness gap. (c) False — the model math becomes batch-size-1, but the system (live server, autoscaling, p99 SLA, cold starts) is entirely new. You choose an architecture, not a batch size.
Level 2 — Application
Exercise 2.1
A model costs (compute units) per prediction. The per-request overhead is . You run the batch job over candidates but only get used. Compute the batch cost per useful prediction, the real-time cost per useful prediction, and say which mode is cheaper.
Recall Solution
Batch: total spend ; useful outputs ; so , which equals (the cancels, as promised). Real-time: . Since , real-time is cheaper. Half of what you precompute is thrown away, doubling the effective cost — the small overhead of is a bargain by comparison.
Exercise 2.2
Same , . Find the exact utilization at which batch and real-time cost the same, then state the rule for when batch wins.
Recall Solution
Set costs equal: . WHY isolate ? We want the utilization threshold — the value of that makes the two modes tie — so we solve for alone. Two domain facts make this legal: (compute plus overhead is always a positive price), so we may divide by it; and (you use some predictions, else batch is the infinity case), so we may multiply both sides by without flipping or dividing by zero. Then . Batch wins when (you use more than 80% of what you precompute). Below that, wasted compute makes batch dearer than paying the overhead on demand.
Exercise 2.3
An input arrives at and the answer is needed by the same day. State the slack in hours, and whether precomputing (batch) is possible. Then say what happens in the boundary case .
Recall Solution
hours , so precomputation is possible — there is a window to do the work early. But possible wise: if the underlying data can change within those 6 hours (price drop, new purchase), the precomputed answer goes stale. Positive slack unlocks batch; low staleness cost justifies it. Boundary : input and deadline coincide, so there is no earlier moment to precompute in — batch is impossible, and real-time is forced regardless of any cost analysis (this is exactly the fraud case in Level 5). (And if , the deadline is already past when the input arrives — no mode can help; the problem itself must be re-scoped.)
Level 3 — Analysis
Exercise 3.1
A model is cheap: , overhead (heavy network + serialization). Find the break-even utilization. What does the large ratio tell you about which mode this workload leans toward?
Recall Solution
. Batch wins whenever — a very low bar. Here is large: each on-demand call carries heavy overhead, so real-time is expensive per request. This workload leans batch: even at modest utilization, precomputing beats paying the fat overhead repeatedly. (Contrast Exercise 2.2 where tiny overhead pushed break-even up to 0.8, favouring real-time.)
Exercise 3.2
You run real-time with dynamic batching: the server waits up to ms to collect concurrent requests, then processes them together. A request arrives ms into a collection window. Its latency budget is ms and the forward pass takes ms. (a) What is this request's worst-case added wait from batching? (b) Does it fit the budget assuming ms network overhead?
Recall Solution
(a) The window closes ms after this request arrives (worst case it arrived just as the window opened, giving the full ms; here it's ms of remaining wait). (b) Total = batching wait forward pass network ms ms. Fits, with ms of headroom. Dynamic batching buys throughput by spending a little of that headroom — see Dynamic Batching.
Exercise 3.3
Two designs serve the same feature. Design A: real-time, , , . Design B: batch, , , plus a fixed storage cost per useful prediction to hold results. Which is cheaper per useful prediction?
Recall Solution
A (real-time): . B (batch): , so batch (B) is cheaper. High utilization () keeps waste low and the storage add-on doesn't erase the win. Storage is batch's often-forgotten cost — see Caching Strategies.
Level 4 — Synthesis
Exercise 4.1
Design the inference architecture for a search engine. Documents number in the billions and change slowly; each user query is unique and must return in ms. Split the pipeline into a batch part and a real-time part, and justify each split by what changes slowly vs what depends on the live request.
Recall Solution
Batch part (nightly / on doc update): compute an embedding (a vector representation) for every document. Documents are stable and enormous in number — high slack, high reuse — so precompute and store the vectors in a Feature Store / vector index. Real-time part (per query): embed this query, do a nearest-neighbour lookup against the precomputed index, then a light re-rank. This depends on the live request (the query text didn't exist until now) and must fit the ms budget. Justification: split by rate of change. The expensive, stable half is batched; the cheap, query-specific half is served live. This is the hybrid pattern from the parent's Worked Example 3.
Exercise 4.2
Your real-time endpoint sees a daily traffic spike from to requests/sec at 9 a.m. Each pod (server replica) handles req/sec. Describe the scaling plan, and explain the cold-start risk and one mitigation.
Recall Solution
Replica count needed: at peak, pods; off-peak, pod. Use Autoscaling to scale between 1 and 7 based on load. Cold-start risk: a newly spun-up pod must load model weights into memory before serving — during that gap it cannot answer, so requests queue or fail, spiking p99 latency exactly when traffic surges. Mitigation: keep a warm pool (a couple of pre-loaded idle pods) or pre-scale on a schedule ahead of the known 9 a.m. spike, so pods are ready before the wave hits. See Latency and Throughput.
Level 5 — Mastery
Exercise 5.1
A fraud model runs real-time at , , . A colleague proposes a "cost-saving" batch job that precomputes a risk score per card each morning (, storage per useful prediction). (a) Compare pure costs. (b) Give the decisive reason batch fails here that has nothing to do with cost.
Recall Solution
(a) Real-time: . Batch: . Batch is marginally cheaper () — tempting! (b) But cost is irrelevant here. A fraud decision depends on this swipe's device, location, amount, and time — a feature combination that did not exist until the swipe happened. A morning-computed "risk score per card" cannot see the live transaction, so it answers the wrong question. Slack is ; staleness is unacceptable. Real-time is forced regardless of the penny it saves.
Exercise 5.2
Build a single decision procedure (a short flowchart in words) that takes any new prediction task and outputs batch, real-time, or hybrid. Then run it on: recommending articles that stay relevant for a day, with , , .
Recall Solution
Procedure (in order — the first "yes" that forces a mode wins):
- Is the deadline even reachable — is ? No () → task infeasible; re-scope the deadline or find an earlier signal.
- Does the input exist before the deadline with room to precompute — is ? No () → real-time (forced).
- Is staleness over the slack window unacceptable? Yes → real-time (forced).
- Can the pipeline be split into a slow-changing part and a live-request part? Yes → hybrid.
- Otherwise compare cost: batch if , else real-time.
Run on the article task: ✓ and (recs valid for a day) ✓; staleness over a day is acceptable ✓ (not forced to real-time); not obviously splittable into slow vs live parts → fall through to the cost step. Break-even . Actual , so batch wins. Precompute the day's recommendations and cache them (see Caching Strategies).
Exercise 5.3
A hybrid search system precomputes doc embeddings in batch every 24 h. Freshly published documents therefore wait up to a day to become searchable. Propose a fix that keeps the batch efficiency but bounds worst-case staleness to under 1 hour, and name the drift concept at play.
Recall Solution
Fix: add an incremental / mini-batch stream — a small job running every, say, 15 minutes that embeds only newly published documents and upserts them into the same index. The heavy nightly batch still handles the bulk (efficient), while the mini-batch bounds worst-case staleness to min h. Concept: this is managing Data Drift and Staleness — the gap between when a prediction (here, an embedding) is computed and when it's consumed. Bounding that gap bounds staleness cost.
Recall One-line ladder recap
L1 spot the mode from clues → L2 plug the cost formulas → L3 include hidden storage/overhead → L4 design & scale the system → L5 let forcing constraints trump cost, and bound staleness in hybrids.
Connections
- Parent: Batch vs real-time inference
- Model Serving, Feature Store, Latency and Throughput, Autoscaling, Data Drift and Staleness, Dynamic Batching, Caching Strategies