ProductionAdvanced

When the database changes, how does the cache know?

“Compare cache invalidation strategies.”

What this tests

  • Invalidation approaches
  • Matching strategy to staleness need

Answers by level

Read the beginner answer first and notice what is missing.

TTL only: accept staleness up to the TTL; free, right when a few minutes old is fine. Delete on write: the write path deletes affected keys; correct but every write site must know every dependent key. Versioned keys: bump a version to orphan old entries; the answer to fan-out invalidation. Change data capture: tail the WAL and invalidate from actual committed changes; the only approach that catches every write, at the cost of real infrastructure.

Pick the weakest strategy that meets the staleness requirement, and write the requirement down.

Green flags · Red flags

Strong green flag · Frames invalidation as tracking a derived value’s dependency.
Green flags
  • Names all four
  • Matches strategy to staleness need
  • Documents the tolerance
Red flags
  • Only knows TTL
  • No stated staleness requirement

Follow-up questions

F1
When is CDC worth the infrastructure?

Scenario

Cached user profiles go stale when an admin edits a user directly in SQL. Which strategy catches that?

Learn this topic