Caching strategies
How cache-aside, read-through, write-through, write-back and write-around read and write data, their consistency trade-offs and when to use each.
5 min readUpdated
A cache keeps a copy of frequently used data in fast storage, usually memory (a local cache such as Caffeine, or a shared one such as Redis or Hazelcast), in front of a slower system of record such as a database. A caching strategy decides two things: who loads data into the cache when it is missing, and what happens to the cache and the database when data changes. The answers determine how fresh the data is, how fast reads and writes are, and what can be lost when the cache fails.
The diagram shows the five common strategies: two for reading and three for writing. A system combines one read strategy with one write strategy.

Read strategies
Cache-aside
The application manages the cache itself. The cache and the database do not know about each other.
- Read: the application asks the cache (1). On a miss (2), it reads from the database (3, 4) and stores the result in the cache (5), usually with a time-to-live (TTL).
- Write: the application writes to the database and then deletes the cache entry, so that the next read loads the new value. Deleting is safer than updating the entry, because two concurrent updates can otherwise leave the older value in the cache.
// readProduct product = cache.get(id);if (product == null) { product = db.findProduct(id); cache.put(id, product, Duration.ofMinutes(10));}
// writedb.updateProduct(product);cache.delete(product.id());Consistency: the cache can serve stale data between a database change and the deletion of the entry, or until the TTL expires if something else changed the database.
Use it as the general-purpose default, especially for read-heavy data. Only data that is actually read gets cached, and if the cache is down the application still works, only slower.
Read-through
The cache sits between the application and the database. The application only talks to the cache.
- Read: the application asks the cache (1). On a miss (2), the cache itself loads the data from the database through a configured loader (3, 4), stores it (5) and returns it.
- Write: read-through says nothing about writes; it is combined with one of the write strategies below.
Consistency: the same as cache-aside: entries are stale until they expire or are invalidated.
Use it when the cache library or product supports loaders (for example JCache CacheLoader, Caffeine LoadingCache, Ehcache, Hazelcast MapLoader). The loading logic lives in one place instead of in every caller. As with cache-aside, the first request for each key is slow; warm the cache at startup if that matters.
Write strategies
Write-through
- Write: the application writes to the cache (1), and the cache writes to the database immediately and synchronously (2). The write is confirmed only after both have succeeded.
- Read: usually read-through; recently written data is already in the cache.
Consistency: the cache and the database stay in step for every write that goes through the cache.
Trade-offs: each write pays the latency of both stores. Every written item is cached, including data that is never read; a TTL keeps that in check.
Use it when data is read soon after it is written and stale reads are not acceptable, for example user profiles or settings.
Write-back (write-behind)
- Write: the application writes to the cache (1) and gets an immediate confirmation. The cache writes the changes to the database later (2), asynchronously, often in batches or after a delay, and several updates to the same key can be merged into one.
- Read: from the cache, which holds the latest values.
Consistency: the database lags behind the cache. Anything that reads the database directly (reports, other services) sees old data until the next flush.
Trade-offs: writes are fast and the database receives far fewer, larger writes. If the cache fails before it has flushed, the pending changes are lost, unless the cache is replicated or persists them.
Use it for write-heavy data where losing the last few seconds is acceptable, or where the cache is replicated: counters, view statistics, session or activity data, bursts of sensor readings.
Write-around
- Write: the application writes directly to the database (1) and bypasses the cache.
- Read: cache-aside or read-through. If the data is in the cache it is served from there (2); otherwise it is read from the database (3.1) and the cache is updated (3.2).
Consistency: an entry that was cached before the write stays stale until it expires. Delete the entry when you write, or use short TTLs.
Trade-offs: the cache only holds data that has been read, so it is not filled with data that is written once and rarely read. The first read after a write is always a miss.
Use it for data that is written much more often than it is read back soon, such as logs, audit records, bulk imports or large files.
Compare the strategies
| Strategy | Type | Who reads and writes the database | Read after a write | Main risk | Good fit |
|---|---|---|---|---|---|
| Cache-aside | Read | Application | Miss, then loaded | Stale data until invalidation or TTL | General purpose, read-heavy |
| Read-through | Read | Cache, through a loader | Miss, then loaded | Stale data until invalidation or TTL; slow first read | Read-heavy, cache with loader support |
| Write-through | Write | Cache, synchronously | Hit | Slower writes; caches data never read | Data read soon after it is written |
| Write-back | Write | Cache, asynchronously | Hit | Data loss if the cache fails before flushing | Write-heavy, loss-tolerant data |
| Write-around | Write | Application, directly | Miss | Stale entries if not invalidated | Data written often, read rarely |
Common combinations are cache-aside with deletion on write (the most widely used), read-through with write-through (the cache fully owns data access), and read-through with write-back (the fastest writes, with the risk of loss).