Fill the cache before users arrive, so the first visitors are not slow.
Cache warming means filling a cache with the data you expect to need before users ask for it. The first visitors then get fast answers too.
A cache learns what is popular only after users ask for it. Until then it is empty. That empty time is the cold start. During a cold start the hit ratio is near zero, pages are slow, and the database carries all the traffic.
| Approach | How it works | Good and bad |
|---|---|---|
| Preload a list | A script reads a list of known hot keys (or top products) and loads them. | Simple and fast. The list can be out of date. |
| Replay recent traffic | Take the most-read keys from recent logs or analytics, and read them through the cache. | Matches real behaviour. Needs good logs. |
| Copy from another cache | Dump from an existing cache or replica, then load it into the new one. | Very complete. Can be large, and the data may be a little old. |
| Lazy, throttled warming | Load the hottest keys a little at a time, while real traffic flows. | Gentle on the database. Takes longer. |
| Scheduled refresh | A job rebuilds hot items on a timer, so they never go cold. | Keeps data fresh. Costs steady background work. |
| Shift traffic slowly | Send 1 percent, then 10 percent, then all traffic to a new node. | Lets the cache fill by itself. Needs a smart load balancer. |
Replay recent real traffic (or the most-read keys from the logs) into the new cache. The best match, but you need those logs.
A simplified model with a simulated clock and expected values (no random numbers). The red dashed line is what the database can handle (120 queries per second). Do not warm everything: warm the hot keys, and warm slowly enough that the database is not hurt.
| Term | Meaning in easy words |
|---|---|
| Cold cache | An empty or nearly empty cache with a very low hit ratio |
| Warm cache | A cache that already holds the popular items |
| Cache warming / priming | Filling a cache before users ask |
| Hit ratio | The share of reads served from the cache |
| Hot key | A key that gets a lot of reads |
| Cold start | The slow period right after a cache or server starts |
| Throttling | Limiting the speed of a job so it does not overload others |
| Pre-fetching | Loading something before it is requested |
| Canary / gradual rollout | Sending a small share of traffic to a new node first |
| TTL jitter | A random extra time added to expiry to spread it out |
| Choice | We gain | We lose |
|---|---|---|
| Warm many items | Higher hit ratio sooner | More memory, more database reads |
| Warm few items | Cheap and gentle | Some users still hit a cold cache |
| Warm fast | Ready sooner | Risk to the database |
| Warm slowly | Safe for the database | Longer cold period |
| Do not warm | Simplest | Slow starts and big spikes |