TTL (time to live)
Also called: time to live, expiry, expiration time, max-age.
A lifetime in seconds attached to a cached value, a DNS record or a message; when it runs out, the copy is deleted or treated as missing. A short TTL means fresher data and more load on the source; a long TTL means less load and staler data.
The key lives 10 s here (the prompt uses 300). Until then, reads come from Redis, even if Postgres changed.
product:1 is not in Redis · Cache hits: 0 · Database reads: 0
Redis is empty. Read the key to load it from Postgres.
Say it in a prompt
Set a TTL on every cache write: 300 seconds for product pages, 30 seconds for stock counts, 24 hours for the country list. Add ±10% random jitter to each TTL so keys written together don't all expire in the same second, and delete a key right away when its row changes. Vague vs precise prompt
Vague prompt
make sure the cache doesn't show old data Typical resultClears the whole cache whenever anything changes, or turns caching off for that page, so the database load comes straight back.
Precise prompt
Give each cache entry a TTL: 300s for product pages, 30s for stock counts. Add ±10% jitter so keys don't expire together, and delete a key as soon as its row is updated. Typical resultStock is never more than 30 seconds old and product pages at most 5 minutes, and expiries spread out instead of hitting the database all at once.
Seen on
- Redis: EXPIRE key seconds sets a timeout, after which the key is deleted automatically; the TTL command shows how many seconds are left.
- Cloudflare DNS: Every DNS record has a TTL that controls how long it is cached; the Auto setting is 300 seconds.
- Amazon CloudFront: By default a file stays in an edge location for 24 hours before it expires, unless the origin's headers say otherwise.
You might describe it as
- how long before the saved copy counts as old
- auto-delete the cached value after a while
- expire the key after five minutes
Not to be confused with
- Cache-aside
A TTL is only the expiry timer on an entry; cache-aside is the whole read, miss, load and store routine that usually sets one.
- Cache stampede
A TTL is the timer that makes a key expire; a cache stampede is the rush of reloads when a busy key expires and many requests miss it together.