Skip to content
Tutorials Center
My learning

Topics

Lesson 20 of 65 ยท Caching

Cache Invalidation, TTL and Consistency

Learn why cache invalidation is hard, how TTL, explicit deletion, versioned keys, write-through updates and event-driven invalidation work, and the races to avoid.

A cache holds copies. The moment the original changes, every copy is potentially wrong. Cache invalidation is the work of removing or refreshing stale copies at the right time. It has a reputation as one of the two hard problems in computer science, and the reason is simple: the cache and the source of truth are separate systems, and no one can update two separate systems at exactly the same instant. Every invalidation scheme is a way of choosing how long and how often users may see stale data.

Start by deciding how stale is acceptable. A product description can be a minute old. A stock level at checkout cannot. A bank balance must never be stale in a decision that moves money. That single decision picks the technique.

Time to live (TTL)

The simplest method: every cache entry gets an expiry time. After the TTL passes, the entry is treated as missing and reloaded on the next request. There is no coordination with writers at all. The maximum staleness equals the TTL.

Choosing a TTL:

  • Short (seconds): fresh data, more origin load. Good for fast-changing values such as trending lists.
  • Medium (minutes to hours): typical for product pages, profiles and configuration.
  • Long (days): static assets with versioned names, rarely changing reference data.

Two refinements matter:

  • Jitter: add a random offset (for example, TTL of 600 s plus or minus 10%) so thousands of keys loaded together do not all expire at the same instant and stampede the database (cache stampede).
  • Negative caching: also cache "not found" results for a short time, so repeated lookups of missing items do not hit the database.
Python TTL cache with jitter

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The cache keeps returning the old price (100) until the entry expires at t=30, even though the database changed at t=25. That window is the price of TTL-only invalidation: simple, but stale by up to the TTL.

Explicit invalidation on write

For data that must be fresher, the writer tells the cache. When the application updates a row, it also removes (or updates) the cached copy.

  • Delete on write (cache-aside): after the database update commits, delete the cache key. The next read reloads fresh data. This is the safest common approach, because deleting is idempotent and there is no risk of caching a wrong value.
  • Update on write (write-through): write the new value to the cache as well. Reads stay hot, but two concurrent writers can leave the cache holding the older value.
  • Purge by key, by tag or by prefix: remove many related entries at once (all pages of a product, everything for a tenant). Tags make this manageable: store product:42 pages under a tag and purge the tag.

The race to know about

Even delete-on-write has a race. Imagine reader R and writer W:

  1. R misses the cache and reads the old value v1 from the database.
  2. W writes v2 to the database and deletes the cache key.
  3. R (slowly) stores v1 in the cache.

Now the cache holds stale v1 indefinitely (until the TTL), because the delete happened before R's store. The fix is layered: always keep a TTL as a safety net, delete the key a second time after a short delay ("delayed double delete"), or use versions so an older value cannot overwrite a newer one.

Python The stale-write race, and a version check that fixes it

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Comparing versions (or timestamps) makes the late, older write harmless. Many caches and databases support this with compare-and-set operations.

Versioned keys: never invalidate

A powerful alternative is to avoid invalidation by changing the key. Put a version or content hash in the name: app.3f9a1c.js, product:42:v7. When the data changes, the new version has a new key, so old entries are simply never requested again and age out by eviction. This is how static assets are cached forever (with Cache-Control: immutable) while deploys still update instantly.

The same idea works inside the app: keep a small version counter per entity or per namespace, store it in a fast place, and include it in the keys. To invalidate everything about a user, increment their version number: one write replaces a purge of many keys.

Python Namespace versioning: invalidate many keys with one increment

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

One increment made every old entry unreachable. The orphaned entries fall out through TTL or eviction, so memory is not leaked forever.

Event-driven invalidation

In larger systems the writer and the cache are far apart. The database or the service that owns the data publishes a change event ("product 42 updated") to a message bus, and cache servers, CDNs and other services subscribe and invalidate. Common building blocks are change data capture (reading the database's transaction log), an outbox table, or publish/subscribe channels. Benefits: the writer does not need to know about every cache; staleness shrinks to message delay (milliseconds to seconds). Risks: lost or duplicated events, so keep a TTL backstop and make invalidation idempotent.

Four ways to invalidate

TTL Entries expire by themselves. Simple, bounded staleness.
Delete on write Writer removes the key. Fresh, race-prone.
Versioned keys New data, new key. Nothing to purge.
Events Changes broadcast to every cache.

Combine them: events or deletes for speed, TTL as safety net.

1 / 2

HTTP caching: validators and revalidation

On the web, browsers and CDNs use headers to cache and revalidate:

  • Cache-Control: max-age=300 lets any cache reuse the response for 5 minutes (a TTL). s-maxage is for shared caches such as CDNs. no-store forbids caching; private allows only the user's browser.
  • ETag (a fingerprint of the content) and Last-Modified let a cache ask "has it changed?" with If-None-Match. The server replies 304 Not Modified with no body if not, saving bandwidth while staying correct.
  • stale-while-revalidate=60 lets a cache serve the stale copy instantly while it refreshes in the background, hiding the reload latency.
  • Vary tells caches which request headers (such as Accept-Language) change the response so they keep separate copies.
Python Revalidation with ETags

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Choosing a strategy

Data Typical approach
Static assets (JS, CSS, images) Versioned filenames, very long max-age
Public pages and API lists Short TTL, stale-while-revalidate, purge on publish
User profile or settings Delete on write plus TTL
Computed aggregates and feeds TTL with jitter, or recompute on events
Inventory and prices at checkout Bypass the cache or read the source of truth
Reference data (countries, categories) Long TTL plus event-based refresh

Principles to take away:

  1. Decide acceptable staleness first; the technique follows.
  2. Always set a TTL, even when you also invalidate explicitly.
  3. Prefer delete over update, and versioned keys where possible.
  4. Make invalidation idempotent and tolerate lost messages.
  5. Never cache sensitive or per-user data in a shared key.
  6. Measure: hit rate, stale reads and the origin load after invalidation.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What is the main trade-off of TTL-only invalidation?

    Entries expire on their own, with no coordination, but readers may see old data until then.
  2. 2. Why add jitter to TTLs?

    Random offsets spread reloads over time and prevent synchronized expiry.
  3. 3. In delete-on-write cache-aside, what can leave stale data in the cache indefinitely?

    A reader that fetched the old value earlier may store it after the key was deleted; versions or a TTL contain the damage.
  4. 4. How do versioned or fingerprinted keys avoid invalidation?

    New content means a new name, so nothing has to be purged; old entries just age out.
  5. 5. What does a 304 Not Modified response mean?

    The client's ETag or date matched, so it may reuse its stored copy and save bandwidth.

Summary

  • Invalidation is hard because the cache and the source of truth cannot be updated atomically; choose how stale data may be.
  • TTL is simple and bounded; add jitter and negative caching.
  • Delete on write is fresher but has races; keep a TTL and use versions or delayed deletes.
  • Versioned keys and namespace versions avoid purging; events propagate changes across caches.
  • HTTP uses max-age, ETag, 304 and stale-while-revalidate for the same ideas.

Next: Distributed Caches: Redis and Memcached