🎓 A1Learner
🔍

Home / Blog / System Design / Scalability

Caching Strategies: Cache-Aside, Read-Through, and More

System Design · Scalability · 6 min read

A cache is a fast copy of data; cache-aside, read-through, write-through, and write-behind differ in who fills it and when writes reach the database.

What a cache is, and what it costs

A cache is a fast, temporary copy of data that is otherwise slower or costlier to fetch. The original store is usually a database. The database is the source of truth, which means its copy is the one that counts when copies disagree.

A cache hit happens when the requested data is already in the cache. A cache miss happens when it is not, and the system must fetch it from the database. Caching trades freshness for speed. A cached copy can be older than the database copy, and that gap is called staleness.

Caching helps most when the same data is read many times and changes slowly. A shop page, a feed, and a public profile are examples. Caching helps least when every read is different, or when the data must be exactly current at the moment it is read, such as a payment balance.

Cache-aside: the application fills the cache

Cache-aside is also called lazy loading. Lazy here means data is loaded into the cache only when it is first requested, not in advance. The application owns the caching logic. The cache itself is a simple store that knows nothing about the database.

On a read, the application checks the cache first. On a hit, it returns the cached copy. On a miss, it reads the database, stores the result in the cache, and returns it. Only data that is actually requested gets cached, so memory is not spent on data nobody reads.

On a write, the usual cache-aside step is to update the database and then delete the cached entry. Deleting is safer than overwriting the cached copy. The next read then fetches the fresh value from the database. A time to live, or TTL, adds a safety net: every entry expires after a set time, so even a missed delete cannot leave stale data forever.

Read-through: the cache fills itself

Read-through moves the loading logic into the cache layer. The application only talks to the cache. On a miss, the cache layer loads the value from the database itself, stores it, and returns it.

The behaviour a reader sees is close to cache-aside. The difference is where the work lives. With read-through, every part of the application follows the same loading rules, because the rules sit in one shared layer rather than being repeated in each place that reads data.

Read-through needs a cache layer that supports loading from the database. It also puts the cache on the read path in a stronger sense: if the cache layer is down, reads may fail rather than merely slow down. Cache-aside degrades more gently, because the application can still read the database directly when the cache is unavailable.

Write-through and write-behind: two ways to handle writes

Write-through writes to the cache and the database together, as one step, before the write is confirmed. A later read therefore finds a fresh cached copy. That freshness is useful for data read right after it is changed, such as a profile name or a setting.

The cost is slower writes, because every write waits for the database as well as the cache. Memory can also be spent on data that is written but never read again. Write-through still needs every writer to use the same path. A writer that updates the database directly, bypassing the cache, leaves a stale copy behind.

Write-behind, also called write-back, reverses the order. The write goes to the cache at once, and the database is updated later, in batches. Writes feel fast, and repeated writes to the same entry can be combined into one database write. The risk is data loss: if the cache fails before the delayed write reaches the database, the acknowledged change is gone. Write-behind fits counters and analytics events, where losing a small amount of recent data is tolerable. It does not fit payments or orders.

A related pattern is write-around. Writes go straight to the database and skip the cache. The cache is filled later, on a read miss. That fits data written often but read rarely, where filling the cache on every write would waste memory.

How to choose between them

Start with cache-aside for reads, and delete the cached entry on writes, with a TTL as a safety net. That combination covers most read-heavy data, such as pages, feeds, and product details. It is simple, and it keeps the cache optional rather than essential.

Add read-through when the same loading logic is repeated in many places and a shared cache layer can hold it once. Add write-through for the narrow set of records where a reader must see a fresh value right after a change, and where every writer can be forced through the same path.

Use write-behind only for high-volume writes where a small loss is acceptable. For data that must never be stale, such as stock at checkout, skip the cache at the decisive moment and read the database directly. The cache can still serve the display copy, while the decision uses the source of truth.

What goes wrong, and how to soften it

A cache stampede happens when a popular entry expires and many requests miss at once, flooding the database together. One softening step is to let only one request rebuild the entry while the others wait or receive the older copy. Another is to stagger expiry times, so entries do not all expire in the same moment.

A second failure is the stale write race. A reader can miss the cache, read an old database value, and store it in the cache after a writer has already updated the database and deleted the entry. The old value then sits in the cache until its TTL expires. Short TTLs for fast-changing data limit the damage.

A third failure is a full cache outage. With cache-aside, the system slows but keeps working, because reads fall back to the database. With read-through and write-through, the cache layer is part of the path, so an outage can block reads or writes. Planning for that difference matters as much as picking a pattern: a cache that the database cannot survive without is a dependency, not just an optimisation.

Questions readers ask

What is the difference between cache-aside and read-through?

In cache-aside, the application checks the cache and loads a miss from the database itself. In read-through, the cache layer loads the miss. The reader sees similar behaviour, but the loading logic lives in a different place.

Is write-through always safer than write-behind?

Write-through is safer for freshness, because the database is updated before the write is confirmed. Write-behind is faster, but it can lose acknowledged writes if the cache fails before flushing them to the database.

Why delete a cache entry on write instead of updating it?

Deleting avoids leaving an older value in the cache when two writers act at once. The next read reloads the fresh value from the database, so the cache cannot quietly keep the wrong copy.

When should data not be cached at all?

Skip caching when every read is unique, when the database lookup is already cheap, or when the value must be exactly current at decision time. In that last case, read the source of truth directly.