Patterns

Cache-aside

Keep hot reads in memory and let the app fill the cache on a miss, so the database serves each popular row once instead of once per request.

Cache-aside puts a fast store beside the database, and the application, not the store, decides what goes in it.

Use it when#

  • Reads outnumber writes, and the database CPU climbs with traffic.

  • A small set of rows draws most of the reads, like a product page on launch day.

  • A slightly stale answer is acceptable for a few seconds.

How it works#

On a read, the app asks the cache first. A hit returns at once. A miss reads the database, writes the answer into the cache with a TTL, and returns it. On a write, the app updates the database and deletes the cached key, so the next read refills it.

1. gethit2. miss: read3. set with TTLAppCacheDatabase

If 90% of reads hit, the database sees a tenth of the traffic. A rise from 90% to 99% cuts it tenfold again, so the last few points of hit rate are worth the most.

What it costs#

Data can be stale for as long as the TTL, and a deleted key can race with a slow reader that writes the old value back. A cold cache sends every read to the database at once, and a popular key that expires under load sends many readers to rebuild it together, which is a cache stampede.

In Quorum#

Place a Cache in front of the Database and research Caching. The Cache's default strategy is cache-aside, so the Design shows it with a TTL and a size.

Drag to pan. Hold Ctrl or Cmd and scroll to zoom.

At 1.50k Users the Simulation solves this Design to Apdex 1.00, p99 211 ms, 0.0% errors and $1.28/s Burn.
WhereSettingValueWhy
App ServerInstance sizesmallSmall gives the app two vCPUs, enough to run code while the data tier does its own work.
CacheCache strategyasideThe app fills the Cache on a miss and writes straight to the Database, so the Cache holds only what someone has read.
CacheTTL (seconds)60An entry lives for a minute, which keeps most hot reads as hits and bounds how stale an answer can get.
CacheInstance sizesmallMemory decides how much of the hot set fits. A Cache too small for it misses even on popular rows.
DatabaseInstance sizesmallSmall gives the Database two vCPUs, so the figures below reflect the Cache and not a starved Database.

Research this Design needs: Caching.

The Cache stampede incident shows the cold-cache problem, and Stampede protection is the fix. The glossary defines cache-aside.