ML System Design

Batch vs Real-Time Inference

Choosing between scoring predictions one at a time in real time versus processing millions of predictions in bulk overnight.

🟡 intermediate5 min readservingmust-know
Batch vs Real-Time Inference represents the fundamental serving architecture choice in production ML systems. Batch inference precomputes predictions for all users or items offline (e.g. nightly Spark jobs) and stores results for fast lookup. Real-time inference computes predictions on demand when a request arrives, using the freshest features. Many production systems use a hybrid approach combining both.

The Two Approaches

When your model needs to make predictions, you have two fundamental options:

┌──────────────────────────┬──────────────────────────┐
│ BATCH INFERENCE          │ REAL-TIME INFERENCE       │
├──────────────────────────┼──────────────────────────┤
│ Run predictions for ALL  │ Run prediction for ONE   │
│ users/items at once,     │ user/item when the       │
│ store results in a table.│ request arrives.          │
│ Scheduled (nightly/hourly│ On-demand, per request.  │
│ Spark/Airflow job).      │ API endpoint.            │
└──────────────────────────┴──────────────────────────┘

Batch Inference

Compute predictions for every user (or every item) in a single large job, then store the results in a database or cache for fast lookup.

Example: Every night at 2 AM, run a Spark job that computes the top 100 recommended items for each of your 10 million users. Store the results in Redis. When a user opens the app, look up their precomputed recommendations instantly.

Advantages:

Disadvantages:

Real-Time Inference

Run the model when a request arrives, using the freshest available features.

Example: When a user opens the app, fetch their latest features from the Feature Store (including what they clicked 30 seconds ago), run the model, and return personalized results.

Advantages:

Disadvantages:

The Hybrid Approach

Most production systems combine both:

  1. Batch: Nightly, precompute a broad set of candidate items for each user and store them.
  2. Real-Time: When the user opens the app, retrieve the precomputed candidates and re-score them with a lightweight real-time model that incorporates the latest session features.

This gives you the computational efficiency of batch (heavy candidate generation runs offline) with the freshness of real-time (final ranking uses live signals).

Decision Framework

FactorChoose BatchChoose Real-Time
Feature freshnessFeatures change slowly (daily)Features change every second
Latency requirementNo strict latency (email, reports)Strict latency (<100ms)
User basePredictable, known usersUnknown users, new visitors
Model complexityCan use heavy modelsMust use fast models
Cost priorityPredictable scheduled costPay-per-request cost

Say this out loud

Batch inference precomputes predictions for all users in advance and stores them for fast lookup. Real-time inference computes predictions on demand using the freshest features. Most production systems use a hybrid approach where batch handles heavy candidate generation and real-time handles lightweight re-scoring with live session signals.

Followups to expect

  1. What is near-real-time inference? Processing predictions in micro-batches every few minutes (not nightly but not per-request), using stream processing like Flink. Good for features that change hourly.
  2. How do you handle cold-start users in batch inference? Fall back to a default set of popular items or demographic-based recommendations since no precomputed personalization exists for unknown users.

Check yourself

Question 1 of 3

When is batch inference the better choice over real-time inference?

More in ML System Design

See all →
A Framework for Any ML Design Round5 minFraming a Business Problem as ML5 minOnline vs Offline Evaluation5 min