When can you cache a machine learning model's predictions to save compute, and when is caching a bad idea?
ML system design, Serving
Level L1 Beginner. Length Short. Nature Th Theoretical.
What's being asked
Whether you understand the trade-off between saving inference cost with stored answers and serving stale or mismatched predictions.
Key idea
Caching works when many requests share identical inputs and answers stay valid for a while, such as popular search queries or nightly batch recommendations. It fails when inputs are unique or depend on fast-changing context, so every cache needs a sensible TTL (time to live) to limit staleness.
Steps
- Say when caching works well: if a small set of popular inputs repeats often (like frequent search queries or trending items) or if predictions are precomputed offline for each user, storing results in a fast cache cuts latency and GPU (graphics processing unit) or CPU (central processing unit) cost.
- Say when caching is a bad idea: if every request has unique features (such as a user's live cart plus current time) or if the prediction must react immediately to fresh context (like fraud checks on a live payment), the cache hit rate will be near zero or the answer will be wrong.
- Explain staleness and TTL (time to live): a cached prediction ages as user behaviour or prices change, so you attach a TTL after which the entry expires and is recomputed.
- Mention cache-key design and invalidation: the cache key must include the model version and every feature that affects the output, otherwise users receive predictions from an old model or the wrong context. Larger serving architectures build on this by pairing prediction caches with feature stores and approximate nearest-neighbour indexes.
Common mistakes
- Caching by user ID alone when the prediction also depends on the current item or query, causing wrong outputs to be served.
- Forgetting to include the model version in the cache key, so old predictions keep being served after a new model deploys.
- Setting no TTL at all, which leaves stale recommendations or risk scores in the cache indefinitely.
How long your answer should be
About 2 minutes; when it helps, when it hurts, and TTL.
Read this
Previous in ML system design: What do latency, throughput, p50 and p90 mean? Given 10 request latencies of milliseconds, what are the p50 and p90, and why do production systems promise p99 rather than the average?
Next in ML system design: Why do we sometimes train one model on several GPUs at once, and what is the simplest way to share the work between them?