5.3.7 · HinglishMLOps & Deployment

Batch vs real-time inference

1,730 words8 min readRead in English

5.3.7 · AI-ML › MLOps & Deployment


WHAT — hum choose kya kar rahe hain?


WHY — yeh distinction exist kyun karta hai? (first principles)

Har prediction ke do clocks hote hain:

  1. Jab input available hoti hai (arrival time ).
  2. Jab prediction actually chahiye hoti hai (deadline ).

Slack define karo:

  • Agar slack bada hai (tumhe input answer ki zaroorat se pehle pata hai) → batch mein precompute karo. Pehle se karna wasteful nahi hai... jab tak answer stale na ho jaaye.
  • Agar slack tiny hai (input exactly usi waqt aata hai jab answer chahiye) → real-time serve karna zaroori hai; precompute karne ka koi pehle ka moment hai hi nahi.

HOW — cost & throughput ke baare mein reason karna (derive karo)

Maano:

  • = din bhar mein chahiye predictions ki sankhya,
  • = compute cost per prediction (forward pass),
  • = fraction of predictions jo actually consumer use karta hai.

Batch cost per useful prediction, agar tum saare candidates ke liye predict karo lekin sirf use ho:

Yeh step kyun? Tum pay karte ho sab compute karne ke liye, lekin sirf predictions kaam karti hain, toh cost useful waale pe spread hoti hai. Agar chhota hai (tum unhe precompute karte ho jo kabhi log in hi nahi karte), toh batch wasteful hai.

Real-time cost per useful prediction — tum sirf tab compute karte ho jab maanga jaaye, toh : jahan hai per-request overhead (network, serialization, warm server idle rakhna).

Break-even: batch tab jeetta hai jab

Yeh kyun matter karta hai: kam overhead (chhota ) threshold ko 1 ke paas push karta hai → real-time sasta hai → lean real-time. High utilization near 1 mahenge models ke saath → batch precompute karo aur cache karo.

Figure — Batch vs real-time inference

Throughput vs Latency (yeh trade off karte hain)

Batch systems inputs group karte hain vectorized/GPU parallelism use karne ke liye, throughput maximize karte hain lekin batch fill hone ka wait latency add hoti hai. Real-time dynamic batching use kar sakta hai (concurrent requests collect karne ke liye kuch ms wait karo) as a compromise — yeh beech ka raasta hai.


Staleness — batch ka hidden cost


Flashcards

Batch vs real-time decide karne ke liye sabse best single quantity kya hai?
Slack : deadline se pehle input kitna pata hai. Zero slack real-time force karta hai.
Batch inference define karo.
Ek schedule pe accumulated bade set pe predict karna, results baad mein lookup ke liye store karna (offline).
Real-time inference define karo.
Request arrival pe tight latency budget ke andar synchronously prediction compute karna (online/on-demand).
"Real-time = batch with batch size 1" galat kyun hai?
Model math identical hai lekin real-time ko live serving infra chahiye: autoscaling, load balancing, p99 SLAs, cold-start handling.
Agar sirf fraction use ho toh batch cost per useful prediction?
— total compute ko actually consumed predictions pe divide karo.
Cost mein batch real-time ko kab beat karta hai?
Jab utilization ho, yaani utilization compute-to-compute-plus-overhead ratio se zyada ho.
Latency vs throughput?
Latency = ek request ka time; throughput = predictions per second. Real-time latency optimize karta hai, batch throughput optimize karta hai.
Dynamic batching kya hai?
Real-time servers thodi der (kuch ms) wait karte hain concurrent requests group karne ke liye, thodi latency ke badle mein kaafi zyada throughput milta hai.
Batch ki fundamental weakness?
Staleness — pehle compute ki gayi predictions tab tak outdated ho sakti hain jab tak use hon.
Sab kuch precompute kyun nahi kar sakte?
Input (feature combination) live event hone tak exist hi nahi karta (jaise yeh exact transaction), aur input space effectively infinite ho sakta hai.

Recall Feynman: 12-saal ke bachche ko explain karo

Sandwich banana socho. Batch = tum subah 100 sandwiches banate ho aur fridge mein rakh dete ho, toh lunch fast hota hai — lekin sandwich soggy (stale!) ho sakti hai aur kuch kabhi khayi hi nahi jaatein (waste!). Real-time = jab bhi koi order kare tab sandwich banate ho — hamesha fresh, kuch waste nahi, lekin insaan ko wait karna padta hai aur tumhe ek cook poora din ready rakhna padta hai. Agar pata ho sab kya chahte hain pehle se aur sab khayenge → pehle bana lo (batch). Agar har order ek surprise ho jo andaaza nahi laga sakte → turat banao (real-time).


Connections

  • Model Serving — real-time endpoints host karne ki infra.
  • Feature Storereal-time models ko precomputed batch features serve karta hai (dono worlds ko bridge karta hai).
  • Latency and Throughput — yeh note jis trade-off ko formalize karta hai.
  • Autoscaling — real-time traffic spikes kaise absorb karta hai.
  • Data Drift and Staleness — batch predictions kyun decay hoti hain.
  • Dynamic Batching — dono extremes ke beech ka compromise.
  • Caching Strategies — batch results essentially ek warm cache hain.

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