Skip to content
Tutorials Center
My learning

Topics

Lesson 17 of 37 ยท Strategies and Invalidation

Invalidation Strategies

Learn how to keep cached data fresh: TTL, explicit delete on write, event-driven invalidation, tag-style groups, and the delete-then-refill race, with Python examples.

There is an old joke that the two hard problems in computer science are naming things, cache invalidation and off-by-one errors. The joke endures because invalidation is genuinely tricky: the cache holds a copy, and you must decide when the copy stops being trustworthy. This lesson lays out the main approaches, when each one fits and how they go wrong.

The goal

You want cached data to be fresh enough for the use case, at a cost you can afford. "Fresh enough" is a business decision. A news headline can be a minute old; the number of items in a shopping cart should not be.

Strategy 1: time-based (TTL)

Give every item an expiry and let it die by itself. No code is needed on writes.

  • Pro: trivial, robust, self-healing: even a missed invalidation ends when the TTL expires.
  • Con: staleness up to the TTL; a short TTL lowers the hit ratio.
  • Fits: data where bounded staleness is fine (listings, counts, reference data).

TTL is the backstop you should keep even when you use the other strategies.

Strategy 2: explicit delete on write

When code changes the source data, it deletes the matching cache keys. The next read rebuilds them.

  • Pro: fresh data right after a change.
  • Con: you must find every key that depends on the changed data. Miss one and users see stale data for a whole TTL.
  • Fits: well-understood entities with a small number of keys (a product page).

The difficulty is derived keys. If product 42 appears on its own page, in a category listing, in search results and on the home page, one price change touches many keys.

Python One change, many keys to delete

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.

Keeping a function like keys_depending_on_product in one place, next to the code that builds those keys, is essential. When a new cached fragment is added, you update that one function.

Strategy 3: event-driven invalidation

Instead of every code path calling delete, publish an event ("product 42 changed") and let a subscriber delete the right keys. The subscriber can be in the same process or consume a message queue or database change stream.

  • Pro: invalidation logic lives in one place, and changes made by other systems (an admin tool, a batch import) still invalidate.
  • Con: more infrastructure; events can be delayed or lost, so keep a TTL.
Python Subscribers delete keys when events fire

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.

Strategy 4: tags and groups

Sometimes you want to invalidate "everything about category mugs" without knowing every key. Memcached has no built-in tags or pattern delete (you cannot say delete category:mugs:*), and it cannot list keys reliably. The standard answer is a generation counter stored in the cache itself: include the counter in every dependent key, then bump the counter to invalidate the whole group at once. The next lesson builds this.

Strategy 5: flush_all

flush_all marks every item as invalid at once (optionally after a delay). It is the nuclear option: the hit ratio drops to zero and the database takes the full load. Use it in development or in a real emergency, never as a regular invalidation method. It also affects all applications sharing the server.

The delete-and-refill race

A subtle bug affects explicit deletes. Consider this timeline:

  1. Reader R misses and reads old data from a replica or from the database before the write commits.
  2. Writer W commits the new data and deletes the cache key.
  3. R now stores its old data in the cache.

The cache serves stale data until the TTL ends. The window is small but real, and it is wider when reads come from a lagging database replica. Defences, from simplest to strongest:

  • Keep a TTL on everything (bounded damage).
  • Write to the primary database first, then delete.
  • Read from the primary for the rebuild after a miss on hot, change-prone data.
  • Use delete with a short "tombstone" lock or versioned keys: the writer leaves a marker so late refills are rejected.
Python Reproducing the race

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 combination

Data Suggested approach
Public listings, counts Short TTL only
Single entities (product, profile) Delete on write plus a TTL
Derived pages and lists Event-driven deletes or a generation counter plus a TTL
Anything with a financial or safety impact Do not cache, or cache only with strict versioning

Rules that hold nearly everywhere:

  1. Always have a TTL. It limits the damage of any invalidation bug.
  2. Centralise key building so that "what depends on what" is in one file.
  3. Invalidate close to the write, in the same code path or a reliable event.
  4. Measure staleness in tests: change the data and assert that the cache reflects it.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why should you keep a TTL even when you delete keys on every write?

    TTL is the safety net: a bug in the invalidation code only causes staleness until the item expires.
  2. 2. Can you delete all keys matching category:mugs:* with one Memcached command?

    Memcached has no key listing or wildcard delete. flush_all removes everything on the server, not a subset.
  3. 3. What is the delete-and-refill race?

    The reader's old copy is written after the invalidation, so stale data survives until the TTL.
  4. 4. When is flush_all appropriate?

    It empties the whole cache, causing a cold start and database load, and affects every application on the server.

Summary

  • Invalidation decides when a cached copy stops being trusted; "fresh enough" is a business choice.
  • TTL is the simple backstop; explicit deletes give fresh data but must cover every dependent key.
  • Events centralise invalidation but add infrastructure; Memcached has no tags or wildcard delete.
  • The delete-and-refill race can leave stale data until the TTL ends.
  • Keep key building in one place and always keep a TTL.

Next: Versioned Keys and Namespaces