← Lessons

quiz vs the machine

Gold1400

System Design

TTL And Expiry Strategies

Bounding how long cached data may live before it must be refreshed.

5 min read · core · beat Gold to climb

What TTL means

A time to live is a duration attached to a cache entry. Once that time passes, the entry is considered expired and will not be served, forcing a refresh from the source. TTL is the main lever for bounding staleness.

How expiry is enforced

  • Lazy expiry checks the timestamp only when a key is read, evicting it if expired. It is cheap but lets dead entries linger in memory.
  • Active expiry runs a background sweep that proactively removes expired keys, freeing memory sooner at some CPU cost.

Many systems combine both: lazy on read, plus periodic sampling sweeps.

Choosing a TTL

  • A short TTL means fresher data but more misses and backend load.
  • A long TTL means fewer misses but staler data.
  • Adding small random jitter to TTLs spreads expirations so many keys do not expire at the same instant.

Key idea

TTL bounds staleness by expiring entries after a set time, enforced lazily, actively, or both, with jitter to avoid synchronized expiry storms.

Check yourself

Answer to earn rating on the learn ladder.

1. What does a TTL control?

2. Why add random jitter to TTL values?