Sub topic 12
Hybrid patterns: precompute with live fallback
This chapter assumes you know basic machine learning vocabulary, can read Python, and understand HTTP APIs and scheduled jobs. It also assumes you have seen batch and real-time prediction from earlier chapters.
Why combine the two
Batch prediction is cheap and fast to read, but it only covers records that existed when the job ran. Real-time prediction covers every record, but you pay for compute on every request and you must keep latency low.
A hybrid pattern uses both. A scheduled job precomputes scores for known records and stores them. At request time, the service looks up the stored score. If none exists, it computes a live prediction instead.
This pattern fits cases where most requests hit records you already know. A churn score for existing customers is a good example. New customers, who sign up after the nightly job, are the gap the fallback fills.
How the flow works
The lookup is the fast path. The live prediction is the slow path, used only on a miss.
A minimal implementation
The function below takes the store and the two live steps as arguments. It returns the score and a label saying where the score came from.
def get_score(customer_id, precomputed, live_predict, features_for):
score = precomputed.get(customer_id)
if score is not None:
return score, "batch"
return live_predict(features_for(customer_id)), "realtime"Returning the source label matters. It lets you log how often the fallback runs and compare the two paths later.
Here precomputed can be a dictionary loaded from the batch output, or a key-value store such as Redis. The batch job from the earlier chapter writes scores_2026-11-28.parquet. A loader step can read that file and fill the store.
Design decisions
- Cache key. Use a stable identifier such as
customer_id. Add the model version if you may run more than one model. - Staleness limit. A precomputed score gets older every hour. Store a timestamp with each score. If it is older than your limit, treat it as a miss and score live.
- Fallback capacity. Size the live service for the miss rate, not for total traffic. Measure the miss rate before you choose.
- Same features. The live path must build features the same way the batch job did. Otherwise the two paths return different scores for the same customer, which is training-serving skew.
Common errors
- Silent fallback storms. If a batch job fails, the store empties or goes stale, and every request misses. The live service then receives all traffic. Alert on the fallback rate, and set a rate limit or a default response.
- Mixed model versions. After a model update, old batch scores come from the old model while live scores come from the new one. Version the scores and refresh the store when you deploy.
- Hiding failures. If the live path errors out, decide in advance what the caller gets: an older score, a default value, or an error. Do not leave it undefined.
- Unchecked consistency. Nobody notices when the two paths drift apart. Score a sample of customers on both paths and compare the results regularly.
Exercises
Exercise
A nightly job scores all customers. A product page shows each customer's churn score. About 3% of page views are for customers who signed up after the job ran. Which part of the hybrid system handles them, and what should you size the live service for?
Show answer
The live fallback handles them, because no precomputed score exists for those customers. Size the live service for roughly the miss rate (about 3% of traffic, plus extra for stale-score misses and a failed batch run), not for total traffic.
Exercise
Your batch job fails for two days. Describe two safeguards that keep the system working.
Show answer
Store a timestamp with each score and treat scores older than a set limit as misses, so users do not see very old values. Also alert on a rising fallback rate and cap or rate-limit the live path (or return a defined default) so the fallback service is not overwhelmed.