Skip to content
StackDecode
System Design
7. Caching ArchitectureAnimated

Time-To-Live (TTL)

The lifetime of a cache entry.

The idea

A TTL counts down from the moment of storage. When it hits zero the entry is gone.

Watch it work

Press play, pause, or step through. Some animations have settings you can change.

request streamABFBFADADADFAADAACBDAFcache (4 slots)Ain at 1MISS A is fetched from the databasehit rate 0% (0 of 1)
  • hit
  • current request

A is missing: fetch it from the database and store it.

step:
1
hits:
0
1/22

Eviction policy: oldest inserted.

Step by step

  1. 1Set a TTL on store
  2. 2Entry is served until it expires
  3. 3It is deleted
  4. 4Next request reloads

Where it is used

Almost all caches and DNS.

Watch out for

Too short loses benefit; too long serves stale data.

Related