Catalog My learning Blog
Scalability Patterns: Load Balancing, Caching, Sharding · Phase 2 · Lesson 6 · 10 min read

Cache-Aside vs Read-Through Caching

On this page
  1. Learning Objective
  2. Concept Explanation
  3. Worked Example: The Stale Set Race and Three Ways to Close It
  4. Real-World Examples
  5. Review Questions & Answers

Learning Objective

Describe the exact sequence of operations in cache-aside and read-through caching, identify the race conditions each one permits, and explain why cache-aside remains the dominant pattern despite being the one that pushes the most correctness burden onto application code.

Concept Explanation

Cache-aside, also called lazy loading, puts the application in charge. On a read, the application asks the cache; on a hit it returns the value, and on a miss it queries the system of record, writes the result into the cache, and returns it. Nothing is in the cache until someone asks for it, which means the cache naturally contains only data that is actually being read, and a cache outage degrades the system to a slow but functioning one rather than a broken one. Those two properties, working-set-shaped contents and graceful degradation, are the reason cache-aside is by a wide margin the most deployed caching pattern in production systems.

The cost is that every consumer of that data must implement the pattern identically, and the pattern has sharp edges. The most important one is the concurrent read-write race. Consider a reader that misses the cache and fetches version 1 from the database, and then, before that reader writes version 1 into the cache, a writer updates the database to version 2 and deletes the cache key. The reader now completes and writes its stale version 1 into an empty cache, where it will sit until its TTL expires. Nothing detected an error, no operation was performed out of order from any single actor perspective, and the cache now holds a value that no longer exists in the database. This is not a rare theoretical interleaving; at high read concurrency against frequently updated keys it happens constantly.

The mitigations are worth knowing precisely because they are what separates a caching implementation that works at scale from one that merely works in testing. A short TTL bounds the damage window without eliminating it. Versioned or compare-and-set writes let the reader refuse to overwrite a value written more recently than its own read, which requires the cache to support atomic conditional writes. Leases, the mechanism Facebook described for memcache, have the cache hand out a token on a miss and reject a subsequent set whose token has been invalidated by an intervening delete, which closes the race properly. And the simplest structural mitigation is to delete on write rather than update on write, which makes the next read repopulate from the authoritative source instead of trying to keep two copies in agreement.

Read-through moves the miss handling into the cache layer itself. The application only ever talks to the cache, and the cache is configured with a loader function that fetches from the backing store on a miss, populates itself, and returns the value. This eliminates duplicated logic across callers, guarantees the pattern is implemented consistently, and gives the cache provider a natural place to add per-key locking so that concurrent misses for the same key trigger exactly one load rather than a hundred. Local in-process caches commonly offer this, and it is a first-class feature of several distributed cache products. The tradeoffs are that the cache is now a hard dependency rather than an optimization, since if it is down the application cannot reach the data at all through this path, the read model is coupled to whatever loader interface the cache supports, and the first request for any key still pays the full miss latency.

A practical detail that bites both patterns is negative caching. If a key does not exist in the backing store, a naive implementation caches nothing, so every request for that key becomes a database query. An attacker, or simply a buggy client in a retry loop, can then pass straight through the cache to the database by requesting identifiers that do not exist, which is known as cache penetration. The fix is to cache the absence explicitly, storing a small sentinel value with a short TTL, or to front the lookup with a Bloom filter that can cheaply answer that a key is definitely not present. Failing to handle this is a common reason a system with an excellent cache hit rate on paper still shows heavy database load in practice.

DimensionCache-AsideRead-Through
Who handles a missThe application, explicitly, at every call siteThe cache layer, via a configured loader
Cache unavailableDegrades to direct database reads; system still worksRead path is broken unless an explicit bypass exists
Consistency of implementationDepends on every caller doing it the same wayEnforced in one place by construction
Concurrent miss on one keyN callers each query the database unless coalescing is addedNatural place for per-key load coalescing
What ends up cachedExactly the working set that was actually requestedThe same, plus whatever the loader chooses to prefetch
CouplingLow; the cache is an optimization the application can skipHigher; the data access path runs through the cache abstraction

Worked Example: The Stale Set Race and Three Ways to Close It

The interleaving below is the single most important thing to understand about cache-aside, because it produces a wrong value with no error anywhere and it survives code review easily. The three fixes that follow differ in how much they require from the cache itself.

SQL
# --- THE RACE ---
# Reader R and Writer W act on key "user:42", currently absent from cache.

t0  R: GET user:42            -> MISS
t1  R: SELECT ... WHERE id=42 -> returns {name: "Ana", v: 1}
t2  W: UPDATE users SET name="Ana Lopez" WHERE id=42   # DB now v2
t3  W: DEL user:42            -> key was already absent, no-op
t4  R: SET user:42 {name: "Ana", v: 1}   # stale value now cached
# Result: cache says "Ana", database says "Ana Lopez", until TTL expires.
# No component did anything out of order. No error was raised.

# --- FIX 1: bounded damage via TTL (weakest, always worth doing) ---
SET user:42 {...} EX 60      # wrong for at most 60s instead of forever

# --- FIX 2: version-guarded set (needs atomic compare-and-set) ---
read(key):
    v = cache.get(key)
    if v: return v
    row = db.select(key)                  # row carries a monotonic version
    cache.set_if_newer(key, row, row.version)   # refuses to clobber v2 with v1

# --- FIX 3: lease / token (needs cache-side support; Facebook's approach) ---
read(key):
    v, lease = cache.get_with_lease(key)
    if v: return v
    row = db.select(key)
    cache.set_with_lease(key, row, lease)   # rejected if a DEL invalidated
    return row                              # the lease in the meantime

# --- NEGATIVE CACHING: close the penetration path ---
read(key):
    v = cache.get(key)
    if v is NOT_FOUND_SENTINEL: return None      # absence was cached
    if v: return v
    row = db.select(key)
    if row is None:
        cache.set(key, NOT_FOUND_SENTINEL, ttl=30)   # short TTL on purpose
        return None
    cache.set(key, row, ttl=300)
    return row
Two read paths side by side, cache-aside with the application performing the miss fetch and populate, and read-through with the cache invoking a loader, annotated with the concurrent reader and writer interleaving that leaves a stale value in the cache and the lease token that rejects it.
Two read paths side by side, cache-aside with the application performing the miss fetch and populate, and read-through with the cache invoking a loader, annotated with the concurrent reader and writer interleaving that leaves a stale value in the cache and the lease token that rejects it.

Real-World Examples

  • Facebook described the lease mechanism in its NSDI 2013 paper "Scaling Memcache at Facebook", introduced specifically because the cache-aside stale-set race and the concurrent-miss stampede were both causing real problems at its read volume. A lease is a token returned on a miss that the client must present when setting the value, and any delete of that key invalidates outstanding leases, so a client holding a token issued before an invalidation is refused and cannot write a stale value into the cache.
  • AWS documents cache-aside as the default recommended pattern for ElastiCache and states the two reasons plainly: the cache contains only data that has actually been requested, and a node failure is survivable because the application can still read from the database. The same documentation notes the corresponding penalties, a three-step read on every miss and the possibility of stale data, which is a fair summary of why the pattern dominates despite pushing correctness work into application code.
Common PitfallCache-aside looks like four lines of obvious code and contains a race that silently caches a stale value forever. A reader that misses, fetches, and then writes its result can be overtaken by a writer that updated the database and invalidated the key in between, and nothing will detect it. Always bound the exposure with a TTL, prefer deleting the key on write over updating it, and use versioned or lease-guarded sets for keys where staleness actually matters.

Review Questions & Answers

A reader misses the cache and reads version 1 from the database. Before it writes that value back, a writer updates the database to version 2 and deletes the cache key, which does nothing because the key is not there yet. The reader then completes its populate and stores version 1, so the cache now holds a value older than the database with no error raised anywhere. A TTL does not prevent any step of that sequence from occurring; it only guarantees that the wrong value will eventually be discarded and reloaded. It converts an unbounded correctness problem into a bounded one, which is genuinely valuable and often sufficient, but for data where even a minute of staleness is unacceptable you need a mechanism that can actually reject the late write, such as a version-guarded conditional set or a lease token invalidated by the delete.

With cache-aside the application holds a reference to both the cache and the database, so if the cache is unreachable the code path that handles a miss is the same code path that handles a cache outage: query the database directly. With read-through the application only knows about the cache, and the database access lives inside the cache loader, so if the cache layer itself is down there is no path to the data at all unless a bypass was deliberately built. That is acceptable when the backing store could not survive full uncached traffic anyway, in which case falling through to it during a cache outage would just move the outage rather than prevent it, and it is acceptable for in-process local caches where the cache cannot fail independently of the application. It is a poor trade when the database can comfortably serve full read volume at degraded latency, because then cache-aside gives you a real fallback for free.

Cache penetration is repeated requests for keys that do not exist in the backing store. Because there is nothing to cache, every such request misses and goes to the database, and since the miss is legitimate the cache never learns anything from it. A hit rate metric hides this because it is usually computed over requests that had something to return, or because the penetrating traffic is a small fraction of total requests while being a large fraction of database load, so the ratio still looks healthy while the database is being hammered. The fixes are to cache the negative result explicitly with a short TTL, so a repeated request for a nonexistent identifier is answered from the cache, and, where the key space is large or adversarial, to front the lookup with a Bloom filter that can answer definitively that a key is absent without touching the database at all.

Updating in place means two systems must agree, and the order in which concurrent updates reach each system is not guaranteed to be the same. Two writers updating the same record can apply to the database in one order and to the cache in the opposite order, leaving the cache permanently holding the older value with no mechanism to notice. Deleting instead of updating makes the cache hold either the correct value or nothing, and nothing is always safe because the next read reconstructs from the authoritative source. It also avoids caching values that nobody subsequently reads, keeping the cache shaped like the actual working set. The residual risk, which the lesson covers, is the reader that repopulates with a value it read before the delete, which is why the delete strategy is often combined with versioned sets or leases rather than being relied on alone.