What it is
Cache aside (also called lazy loading) is the most common way to use a cache such as Redis or Memcached in front of a database. The application owns the logic: it looks for a value in the cache; on a hit it uses it; on a miss it reads the database, returns the value and writes it into the cache so the next read is a hit. The cache itself knows nothing about the database.
Why it exists
Many workloads read far more than they write, and they read the same popular items again and again: product pages, user profiles, configuration. Serving each of those reads from the database costs a connection, CPU and disk work every time. An in-memory cache answers in well under a millisecond and is cheap to scale, so taking repeated reads off the database lowers latency and protects its capacity for the work only it can do.
How it works
Read path.
- Compute the cache key (e.g.
product:42). GETit from the cache. If found, return it.- Otherwise query the database,
SETthe key with a TTL, and return the value.
Write path. Write to the database, then delete the cache key so the next read reloads the fresh value. Deleting is safer than updating the cache: two concurrent writers updating both stores can leave them in different orders, while a delete just forces a reload.
Hit ratio. The fraction of reads served by the cache depends on how skewed the access pattern is, how much fits in the cache and how long entries live. With Zipf-like popularity, a cache holding 10% of the keys can serve well over half the reads, because the hottest keys stay resident and the least recently used ones are evicted (LRU).
Expiry. A TTL makes every entry expire after a fixed time, which bounds how stale it can get even if an invalidation is missed. Each expiry turns the next read into a miss, so the TTL is a direct trade between freshness and database load.
When to use it
- Read-heavy data with popular items, where slightly stale values are acceptable for seconds or minutes.
- When you want the cache to be optional: if it fails, the application can still read the database.
- When only some data is worth caching: cache aside caches exactly what is read.
When not to
- Write-heavy data, or data read once (logs, events): you pay for cache writes that are never read.
- Data that must always be current (balances, stock reservations) unless you can invalidate reliably and tolerate the race windows.
- When the database comfortably handles the load; a cache adds a component and a consistency problem.
Trade-offs
| Choice | You gain | You pay |
|---|---|---|
| Short TTL | Fresher data | Lower hit ratio, more database load |
| Long TTL | High hit ratio | Staler data if an invalidation is missed |
| Invalidate on write | Fresh reads after writes | Code on every write path; race windows remain |
| Bigger cache | More keys stay resident | Memory cost; no help if the TTL is what limits hits |
| Caching computed results | Saves expensive work | Harder to know when to invalidate |
Alternatives
- Read-through: the cache loads missing values from the database itself; the application only talks to the cache.
- Write-through: writes go through the cache to the database, keeping the cache warm and current, at the cost of slower writes and caching data that may never be read.
- Write-behind: writes go to the cache and are flushed to the database later; fast, but risks losing writes.
- Read replicas or a materialised view: scale reads without a separate consistency mechanism.
- HTTP/CDN caching: cache whole responses closer to users.
Failure modes
- Stale reads: a write that fails to invalidate, or a race where a slow reader puts an old value back after a writer deleted it, serves outdated data until the TTL expires.
- Cache stampede (thundering herd): when a hot key expires, many concurrent requests miss at once and all query the database for the same value. Mitigate with request coalescing (one request rebuilds, others wait or get the old value), early probabilistic refresh, and TTL jitter so many keys do not expire together.
- Cold cache: after a restart, deploy or failover the cache is empty and the database briefly sees nearly all reads. If it was sized only for misses, it saturates.
- Cache outage: the fallback to the database is correct but sends the full read load there. Size the database for it, shed load, or limit concurrency to the database while the cache recovers.
- Hot keys: one key read so often that a single cache node saturates; replicate it or keep a small local in-process cache.
In production
Treat the cache as part of capacity planning: know what the database must handle if the hit ratio drops. Set a TTL on every key, add random jitter, and delete keys on writes. Protect hot keys against stampedes. Monitor hit ratio, evictions, cache latency and database load together; a falling hit ratio is an early warning. Warm critical keys after deploys if a cold start is dangerous.
Common misconceptions
- "A cache makes the database unnecessary for reads." Every miss, expiry and cache outage still reads the database.
- "A bigger cache always raises the hit ratio." Not if entries expire before they are evicted; then the TTL is the limit.
- "Updating the cache on write is the same as deleting the key." Updates can race and leave stale values; deletes force a fresh read.
- "The fallback to the database makes cache failures harmless." Only if the database can carry the full load.
Interview angle
Expect to justify the cache with numbers (read/write ratio, hit ratio, database capacity) and to describe the read and write paths precisely. Interviewers then probe invalidation and staleness, TTL choice, stampede protection, what happens when the cache cluster fails or restarts, and hot keys. Saying "the database must survive a cold cache, or we shed load until it warms" shows you think of the cache as capacity, not decoration.
Model assumptions
The simulation is a simplified model, not a benchmark. Users read 10,000 products with Zipf(1) popularity; only reads are modelled. The Product API has 32 workers and holds one while it waits for the cache and the database. The database has 8 connections and a 30 ms mean read (lognormal, cv 0.4), about 260 reads per second; waiting more than 250 ms for a connection fails the query. The cache is an LRU of 1,000 keys answering in about 1 ms; it is filled after each database read. A failed cache is treated as a miss. A crashed cache restarts empty, as an in-memory cache usually does. The hit-ratio tile counts every lookup since the simulation started, including warm-up. Numbers show behaviour and orders of magnitude only.