← Lessons

quiz vs the machine

Silver1120

Databases

Cache Aside vs Write Through vs Write Back

Three caching strategies that trade freshness, latency, and durability.

5 min read · intro · beat Silver to climb

Why Caching Strategy Matters

A cache speeds reads, but how it stays in sync with the database changes correctness and risk. Three common patterns handle writes differently.

Cache Aside

The application checks the cache first. On a miss it loads from the database, then stores the value in the cache for next time. Writes go straight to the database and the cache entry is deleted or left to expire. This is simple and the cache only holds data that is actually requested.

Write Through

Every write goes to the cache, which then synchronously writes to the database. The cache stays fresh and reads are fast, but each write pays the cost of touching both stores.

Write Back

Writes land in the cache and return immediately. The cache flushes to the database later in batches. This gives very fast writes but risks data loss if the cache fails before flushing.

Key idea

Cache aside is simple and lazy, write through keeps the cache fresh at write cost, and write back is fastest but can lose data that has not yet flushed.

Check yourself

Answer to earn rating on the learn ladder.

1. What is the main risk of a write back cache?

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

3. Which strategy keeps the cache fresh by writing both stores on every write?