Skip to content
learn
  1. Learn
  2. High Level System Design
  3. Caching

System Design HLD-05.02

Caching patterns

Cache-aside, read-through, write-through, write-behind and write-around: who talks to the database, and what each costs in consistency and durability.

Intermediate 14 min Last reviewed

The big idea

Putting a cache next to a database raises two questions that every design must answer. On a read, who fetches from the database when the cache misses: the application or the cache itself? On a write, which gets updated, in what order, and when do we tell the user it worked? The common answers have names, and each one trades speed, simplicity, consistency and durability differently.

Most systems use the simplest one, cache-aside, for reads, and delete the cached copy on writes. But write-heavy systems, systems that cannot afford stale reads and systems that cannot afford to lose a write each have a better-fitting pattern, and knowing the five lets you pick deliberately instead of by habit.

Think of a shop with a front shelf (the cache) and a stockroom (the database). Who restocks the shelf when it is empty: the customer, or a shop assistant? When new stock arrives, does it go to the shelf, the stockroom, or both?

How it works

Step 1: Reading

Cache-aside (lazy loading): the application checks the cache; on a miss it reads the database and puts the result in the cache itself.

Cache-aside read on a miss The application asks the cache for user 42 and misses. It queries the database, receives the row, and stores it in the cache with a time to live. Application Cache Database GET user:42 miss SELECT * FROM users WHERE id = 42 row SET user:42 (TTL 10 min) Figure 1. Cache-aside: the application owns the logic. On a miss it reads the database and fills the cache, so only data that is actually requested gets cached.

Read-through moves that logic into the cache layer: the application only talks to the cache, and the cache loads missing data from the database by itself. The application code is simpler and every service gets the same loading behavior, at the cost of a cache that must know how to talk to your database.

Step 2: Writing

  • Write and invalidate (usual with cache-aside): update the database, then delete the cached key. The next read misses and loads the fresh value. Deleting is safer than updating the cache, because a delete cannot leave a wrong value behind when two writes race.
  • Write-through: write to the cache, which synchronously writes to the database before acknowledging. Cache and database always agree, and reads right after writes are hits, but every write pays both latencies.
  • Write-behind (write-back): write to the cache and acknowledge immediately; the cache flushes to the database later, often in batches. Writes are very fast and the database sees far fewer of them, but anything not yet flushed is lost if the cache crashes.
  • Write-around: write only to the database, skipping the cache. Good when written data is rarely read back soon (logs, bulk imports), but any cached copy of the key is stale until it expires unless you also delete it.

Try it

This interactive needs JavaScript. The diagrams and text above cover the same idea.

Try this:

  1. Cache-aside: Read (miss, four steps), Read again (one-step hit), Write (database then delete), Read (miss again, fresh value).
  2. Write-through: Write, then Read: an immediate hit with the new value, and the two copies always agree.
  3. Write-behind: Write twice, then press Crash cache before Flush. The latest write never reached the database.
  4. Write-around: Read to fill the cache, then Write. The cache keeps serving the old value: a stale read.

Going deeper

The order of operations matters

With cache-aside, "delete the cache, then write the database" has a gap: a reader can miss, read the old database value and put it back in the cache before the write lands. "Write the database, then delete the cache" is much safer, though not perfect, which is why Cache invalidation and consistency covers leases and versions. Always set a TTL as a safety net so any stale entry eventually disappears.

Cache warming

A pure lazy cache starts empty after a deploy or restart and sends a burst of misses to the database. For critical, predictable keys, systems pre-load the cache (warming) or bring new cache nodes in gradually. The extreme case of a cold or expiring hot key is the stampede in Thundering herd, cache stampede and hot keys .

Trade-offs

OptionWins whenLoses when
Cache-aside + delete on write Most read-heavy applications: simple, caches only what is read, cache failure is not fatal A miss right after every write; small race windows
Read-through Many services sharing one loading policy; simpler application code Flexibility: the cache layer must know your data source
Write-through Data read right after it is written, and strict cache-database agreement Write latency, and caching data that may never be read
Write-behind Write-heavy workloads such as counters and metrics that tolerate some loss Durability: unflushed writes vanish on a crash
Write-around Write-once, read-rarely data like logs and bulk loads Freshness of any cached copy, unless it is also deleted

Common mistakes

  • Updating the cache on write instead of deleting it, so racing writers leave the older value cached.
  • Using write-behind for money or orders, where a lost write is unacceptable.
  • No TTL on cache entries, so any bug that leaves stale data leaves it forever.
  • Treating the cache as the source of truth. It can be emptied at any time; the database must hold everything.

Check yourself

  1. In cache-aside, who loads data from the database on a miss?

    Show answer

    B. The application The application checks the cache, reads the database on a miss and populates the cache.

  2. Which pattern acknowledges writes fastest but can lose data if the cache crashes?

    Show answer

    B. Write-behind Write-behind acknowledges once the cache has the value and writes to the database later.

  3. Why delete the cached key on write instead of updating it?

    Show answer

    B. Racing writes can leave the older value in the cache; a delete forces a fresh load If two updates race, the cache can end with the stale one; deleting avoids writing a possibly wrong value.

  4. Write-around writes only to the database. What happens to an existing cached copy?

    Show answer

    B. It stays stale until it expires or is deleted The cache is skipped, so the old copy remains until its TTL ends unless you delete it.

Recap

  • Cache-aside: the application loads misses and fills the cache; the most common pattern.
  • Read-through: the cache layer loads misses itself.
  • Write-through keeps cache and database in sync at the cost of write latency.
  • Write-behind is fastest for writes but can lose unflushed data.
  • On writes, prefer updating the database then deleting the cached key, with TTLs as a safety net.

Get new lessons by email

Get new posts on backend and AI internals. No spam, unsubscribe anytime.

Your email is only used to send new posts. Privacy

Next up