5.3.7MLOps & Deployment

Batch vs real-time inference

1,921 words9 min readdifficulty · medium

WHAT are we even choosing between?


WHY does the distinction exist? (first principles)

Every prediction has two clocks:

  1. When the input becomes available (arrival time tat_a).
  2. When the prediction is actually needed (deadline tdt_d).

Define the slack: slack=tdta\text{slack} = t_d - t_a

  • If slack is large (you know the input long before you need the answer) → precompute in batch. Nothing is wasted by doing it early... unless the answer goes stale.
  • If slack is tiny (input arrives the instant you need the answer) → you must serve real-time; there's no earlier moment to precompute.

HOW to reason about cost & throughput (derive it)

Let:

  • NN = number of predictions needed per day,
  • cc = compute cost per prediction (forward pass),
  • uu = fraction of predictions actually used by a consumer.

Batch cost per useful prediction, if you predict for all NN candidates but only uNuN get used: Cbatch=NcuN=cuC_{\text{batch}} = \frac{N \cdot c}{u \cdot N} = \frac{c}{u}

Why this step? You pay NcNc to compute everything, but only uNuN predictions do work, so cost is spread over the useful ones. If uu is small (you precompute for users who never log in), batch is wasteful.

Real-time cost per useful prediction — you only compute when asked, so u1u \approx 1: Crt=c+oC_{\text{rt}} = c + o where oo is the per-request overhead (network, serialization, keeping a warm server idle).

Break-even: batch wins when cu<c+o    1u<1+oc    u>cc+o\frac{c}{u} < c + o \iff \frac{1}{u} < 1 + \frac{o}{c} \iff u > \frac{c}{c+o}

Why this matters: low overhead oo (small oc\frac{o}{c}) pushes the threshold near 1 → real-time is cheap → lean real-time. High utilization near 1 with expensive models → batch precompute and cache.

Figure — Batch vs real-time inference

Throughput vs Latency (they trade off)

Batch systems group inputs to use vectorized/GPU parallelism, maximizing throughput but adding wait-to-fill-a-batch latency. Real-time can use dynamic batching (wait a few ms to collect concurrent requests) as a compromise — the middle ground.


Staleness — the hidden cost of batch


Flashcards

What single quantity best decides batch vs real-time?
The slack tdtat_d - t_a: how long before the deadline the input is known. Zero slack forces real-time.
Define batch inference.
Predicting on a large accumulated set on a schedule, storing results for later lookup (offline).
Define real-time inference.
Computing a prediction synchronously on request arrival within a tight latency budget (online/on-demand).
Why is "real-time = batch with batch size 1" wrong?
The model math is identical but real-time requires live serving infra: autoscaling, load balancing, p99 SLAs, cold-start handling.
Batch cost per useful prediction if only fraction uu are used?
c/uc/u — total compute NcNc divided over the uNuN predictions actually consumed.
When does batch beat real-time on cost?
When utilization u>cc+ou > \dfrac{c}{c+o}, i.e. utilization exceeds the ratio of compute to compute-plus-overhead.
Latency vs throughput?
Latency = time for one request; throughput = predictions per second. Real-time optimizes latency, batch optimizes throughput.
What is dynamic batching?
Real-time servers briefly wait (few ms) to group concurrent requests, trading a little latency for much higher throughput.
Batch's fundamental weakness?
Staleness — predictions computed early may be outdated by the time they're used.
Why can't you always precompute?
The input (feature combination) may not exist until the live event happens (e.g. this exact transaction), and the input space can be effectively infinite.

Recall Feynman: explain to a 12-year-old

Imagine making sandwiches. Batch = you make 100 sandwiches early in the morning and put them in the fridge, so lunchtime is fast — but a sandwich might get soggy (stale!) and some never get eaten (wasted). Real-time = you make each sandwich the moment someone orders it — always fresh, nothing wasted, but the person has to wait and you need a cook standing by all day. If you know exactly what everyone wants ahead of time and they'll all eat → make them early (batch). If every order is a surprise you can't guess → make on the spot (real-time).


Connections

  • Model Serving — the infra that hosts real-time endpoints.
  • Feature Store — serves precomputed batch features to real-time models (bridges both worlds).
  • Latency and Throughput — the trade-off this note formalizes.
  • Autoscaling — how real-time absorbs traffic spikes.
  • Data Drift and Staleness — why batch predictions decay.
  • Dynamic Batching — the compromise between the two extremes.
  • Caching Strategies — batch results are essentially a warm cache.

Concept Map

large slack

tiny slack

predicts many on schedule

risk

synchronous per request

served via

cost per useful pred

cost per useful pred

small u makes

caused by

forces

adds

Slack = t_d - t_a

Batch inference

Real-time inference

Stored results looked up later

Staleness

Tight latency budget

API endpoint

C_batch = c / u

C_rt = c + o

Wasteful precompute

Input space explodes

Live server, autoscaling, cold-start

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Dekho, inference ka matlab hai trained model se prediction lena. Sawaal sirf ek hai: prediction abhi ek-ek karke chahiye (real-time) ya baad me, saath me bahut saare chahiye (batch)? Iska decide karne ka sabse simple tarika hai slack — yaani input aane ka time aur answer chahiye hone ka time, dono ke beech ka gap. Agar gap bada hai (jaise Netflix recommendations — raat ko bana lo, subah dikha do) to batch me pehle se compute karke store kar lo. Agar gap zero hai (jaise card swipe pe fraud check — transaction abhi hui, jawab abhi chahiye) to real-time hi karna padega, kyunki jo input abhi tak exist hi nahi karta usko pehle se compute kaise karoge?

Cost ka funda bhi seedha hai. Batch me tum sabke liye predict kar dete ho, par sab log use nahi karte — agar sirf fraction uu log use karte hain to real cost c/uc/u ho jaata hai (wasted compute). Real-time me sirf jab request aati hai tabhi compute hota hai, par har request pe thoda overhead oo lagta hai (server chalu rakhna, network waghera). Break-even tab hota hai jab u>c/(c+o)u > c/(c+o) — matlab agar utilization high hai to batch sasta, warna real-time sasta.

Ek important cheez: staleness. Batch prediction raat ko bani, shaam ko use hui — beech me duniya badal gayi to answer purana pad jaata hai. Ye batch ki weakness hai. Real-time hamesha fresh hota hai par server hamesha ready rakhna padta hai — ye uski cost hai. Aur real production me mostly hybrid hi hota hai: heavy aur slow-changing part (jaise document embeddings) batch me, aur query-specific fresh part (jaise is query ka matching) real-time me. Yaad rakho: BATCH = Bulk, Ahead of time, Cached, but stale; REAL = Right now, Each request, Alive server, Low slack.

Go deeper — visual, from zero

Test yourself — MLOps & Deployment