SkillByAIOpen interactive version →

Lesson 13 / 20

Cache Invalidation

Keep cached data fresh by combining TTL expiry with explicit key deletion after writes.

Two strategies

Let a TTL expire it passively, or actively delete the key the moment the underlying data changes — the second is fresher but needs discipline everywhere data is written.

Delete on write

After updating the source of truth, remove the stale cache entry — the next read will repopulate it.

# after UPDATE products SET price = 799 WHERE id = 501
DEL product:501