Cache-Aside
Check a fast cache first, load from the database only on a miss, and store the result in the cache for next time.
Description
With cache-aside, the application manages the cache; the database knows nothing about it. To read, it looks in the cache. On a hit it returns immediately. On a miss it reads from the database, writes the result into the cache with an expiry time (TTL), and returns it.
On writes, the application updates the database and then deletes the cached entry, so the next read loads fresh data. Only data that's actually requested ends up in the cache, and a cache outage slows things down but doesn't break them.
System design
Cache-Aside system design diagram
- Web app (client).
- Product API (service).
- Redis cache (cache): TTL 5 min.
- Products DB (database).
- Step 1: Web app → Product API: GET /products/42.
- Step 2: Product API → Redis cache: get product:42.
- Step 3: Product API → Products DB: on a miss: read row.
- Step 4: Product API → Redis cache: set with TTL.
- Step 5: Product API → Web app: product.
- A user opens a product page.
- The API looks in the cache first. On a hit it can answer right away.
- On a miss it reads the product from the database.
- It stores the product in the cache with a 5-minute expiry, so the next requests are hits. When the product is edited, the API deletes this key.
- The user gets the product, usually in a millisecond or two from the cache.
- AWS
- Amazon ElastiCache (Redis/Valkey or Memcached); DynamoDB Accelerator (DAX) for DynamoDB.
- Azure
- Azure Cache for Redis.
- Google Cloud
- Memorystore for Redis or Memcached.
When to use it
Use it when
- Reads far outnumber writes, and the same items are read again and again: product pages, profiles, configuration.
- The database is slow or expensive for these reads, or close to its limits.
- Slightly stale data, for seconds or minutes, is acceptable.
Avoid it when
- Data must always be exactly current, such as account balances or stock levels during checkout.
- Every request reads different data, so the hit rate would be low.
- The data set is small and fits comfortably in the application's own memory. An in-process cache is simpler.
Pitfalls
- Stale data after writes. Invalidate (delete) the key on every write path, and keep TTLs short enough to bound the damage.
- Cache stampede: a popular key expires and hundreds of requests hit the database at once. Use request coalescing, a short lock, or refresh before expiry.
- Caching errors or empty results by accident, or forgetting to cache them deliberately so missing items hammer the database.
- Treating the cache as the source of truth. It can be emptied at any time.
Alternatives
Read-through / write-through cache
Choose it when you want the cache layer itself to load and write the database, so application code stays simpler.
Materialized View
Choose it when reads need data that is expensive to compute or join, not just fetch.
CDN caching
Choose it when the response is the same for many users and can be cached at the edge.
CQRS
Choose it when reads need a different model and storage altogether, not just a faster copy.