Docs5 min readLast updated: undefined

cache invalidation


title: The Hard Problem: Cache Invalidation
description: Why you see old data. The trade-off between speed and accuracy.
date: 2024-03-28
category: Web & Network
order: 16
readingTime: 6 min read

"There are only two hard things in Computer Science: cache invalidation and naming things." — Phil Karlton

What is Caching?

Caching is saving the result of an expensive calculation so you don't have to do it again.

  • Expensive: Query database for "Top 10 Products" (Takes 500ms).
  • Cache: Save result in RAM (Redis).
  • Cheap: Next user asks? Serve from RAM (Takes 1ms).

The Invalidation Problem

What happens when you change the price of Product #1?
The Cache still remembers the old price. The user sees the old price. This is Stale Data.

You must "Invalidate" (delete/update) the cache. This is hard because:

  1. Timing: If you update the database, but the cache update fails, data is out of sync.
  2. Complexity: If you have caches in the Browser, the CDN, and the Server, you have to clear all of them.

TTL (Time To Live)

The simplest strategy is TTL.
"Keep this data for 60 seconds, then delete it automatically."

  • Pros: Simple.
  • Cons: For up to 59 seconds, users might see wrong data.