Cache-aside

  • Keep database write authority and protect cached reads from a delayed stale fill.
100%
Application replicasAuthoritative databaseRedis · rebuildable read modelChange deliveryRedis caches a projection of database stateClient / applicationDiscovery / coordinationStored dataload on missupdate itemsame transactionlookup / guarded filldeliver versionadvance floorReads a cached catalog item. On a miss, loads the committed payload and its database version.Catalog readerLoad on cache missApplies the business mutation and its durable invalidation event in one database transaction. Reads that must authorize a write use the database contract.Catalog writerA monotonically increasing item version identifies the committed value, including deletion. The worked fixture changes item 17 from version 7 to version 8.Item + versionThe change record shares the mutation transaction. A relay can retry after a crash, and duplicate or reordered delivery is expected.Committed change eventsThe value can expire. A separate per-item floor records the newest processed version and guards against delayed publication of an older value.Value + TTLVersion floorProcesses committed events, advances each item’s version floor monotonically and removes obsolete cache values atomically.Invalidation workerload on missupdate itemsame transactionlookup / guarded filldeliver versionadvance floor

Select a component to trace its direct connections. Drag to pan · Ctrl + wheel or pinch to zoom.

  • The database owns each item and its version. Updating item 17 to version 8 also records a change event in the same transaction.
  • The worker records 8 as the oldest version Redis may accept. A reader that fetched version 7 before the update can no longer refill the cache with that old copy.
  • Until that event arrives, cached reads can still be stale. A caller that must see its own write can read the database or require at least version 8.
  • The database owns the value. A slow read can reach Redis after the writer’s invalidation and restore stale data with a fresh TTL.
  • The version floor protects fills after its invalidation arrives. Reads during delivery lag still need an explicit freshness policy.
Cache-aside

Cache-aside

Redis Open Source 7.2 architecture

Exercise the stale-fill race

Moment Database Redis action
Reader misses Reads item version 7 Holds the old payload while computing the response.
Writer commits Stores version 8 and its change event A relay later advances the version floor to 8 and evicts the old value.
Slow reader resumes Still holds 7 A blind SET can give version 7 another 30 seconds. A guarded fill rejects it against floor 8.
Fresh reader loads 8 Reads the committed row An atomic matching-version fill can publish 8.
  • A deletion-only invalidation leaves an empty key that an older in-flight reader can refill. Value TTL bounds that fill’s residence from insertion. The age of the underlying database change can already exceed the TTL.
Version 7 refill after the version 8 commitjson
{
  "kind": "illustrative cache-aside acceptance case",
  "entity": "catalog item 17",
  "keysInSameSlot": ["catalog:{17}:min-version", "catalog:{17}:value"],
  "initial": {"databaseVersion": 7, "cacheFloor": 7, "value": null},
  "events": [
    {"actor": "slow reader", "action": "miss and load database version 7, pause before publishing"},
    {"actor": "writer", "action": "commit database version 8 and its durable change event together"},
    {"actor": "invalidator", "action": "atomically advance floor to 8 and remove the old value"},
    {"actor": "slow reader", "action": "attempt to install version 7 with a fresh 30-second value TTL"},
    {"actor": "fresh reader", "action": "miss, reload database version 8 and attempt to install it"}
  ],
  "expectedBlindFill": {"afterSlowReader": 7, "staleUntil": "expiry or a later invalidation"},
  "expectedGuardedFill": {"afterSlowReader": null, "afterFreshReader": 8},
  "requirements": [
    "One monotonically increasing database version per item, including deletes.",
    "Redis atomically checks the current floor and installs a candidate with the matching version.",
    "A duplicate or older change event never reduces the floor or evicts a valid newer entry.",
    "On an absent floor, bypass caching and initialize from authoritative state after fencing in-flight fills.",
    "Recover lost/rolled-back Redis state and reconcile the event watermark before allowing fills or serving cached reads."
  ]
}

Decide what may be served

  • Keep each value’s database version with its payload. Run the comparison and value-plus-expiry update atomically. A client-side GET followed by SET reopens the race.
  • Serialize version-floor advance and value removal for each item. Under Cluster, place both keys in the same slot. Increasing versions make repeat and out-of-order invalidations safe when handlers reject lower or equal event versions.
  • During delayed or lost invalidation a warm cache may still serve the previous version. Authorization, financial writes and strict read-after-write callers need an authoritative or suitably version-checked read.
  • After eviction, failover rewind or lost floor, bypass the cache until the version floor and event watermark have been rebuilt and old fills fenced. Redis replication and persistence affect the cache’s correctness contract.

Limit the cost of losing the cache

  • Coalesce concurrent misses per key. Limit fallbacks by available database connections, deadlines and acceptable queueing.
  • With 1,000 reads/s and a 95% hit rate, roughly 50 reads/s reach the database when healthy. A complete cache outage can push that workload toward 1,000 reads/s before coalescing or shedding.
  • Split expiry times to avoid coordinated reloads. Track hit rate by operation, the oldest unprocessed change, rejected stale fills and database acquisition latency.