Skip to content
Tutorials Center
My learning

Topics

Lesson 9 of 37 ยท Caching Concepts

TTL and Expiration

Learn how Memcached expiry works: the exptime rules (0, relative seconds, Unix timestamps, negative), lazy expiration, choosing TTLs, and adding jitter to avoid mass expiry.

Every cached item should have an answer to the question "how long may this be wrong?" That answer is the time to live (TTL). When the TTL runs out the item is considered gone, and the next request rebuilds it. TTL is the simplest form of invalidation: it needs no cooperation from the code that changes the data, only patience.

This lesson explains the exact rules Memcached uses for the exptime field, how expiry really happens inside the server, how to choose TTLs, and how to avoid the mistake of making thousands of items expire at the same second.

The exptime rules

Every storage command takes an exptime value. Memcached interprets it like this:

Value Meaning
0 Never expires by time (it can still be evicted when memory is full)
1 to 2,592,000 (30 days) Seconds from now
Larger than 2,592,000 An absolute Unix timestamp
Negative Already expired: the item is immediately invisible

The 30-day threshold is a classic trap. If you pass 31 * 86400 meaning "31 days from now", Memcached reads it as a timestamp in January 1970, which is in the past, and the item vanishes at once. For long lifetimes pass an explicit timestamp or stay under 30 days.

Python How Memcached reads exptime

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Look at the 31-day line: it reads as a past timestamp. That one catches experienced developers too.

How expiry happens: lazily

Memcached does not keep a timer per item. Instead it uses lazy expiration:

  1. Each item stores its absolute expiry time.
  2. When you get an item, the server compares the time. If it has passed, the item is treated as missing and its memory is reclaimed.
  3. When the LRU maintainer or an allocation scans the tail of a queue, expired items found there are reclaimed first.

An expired item may sit in memory a while before being reclaimed, but it is never returned. This has two effects: curr_items in stats can include items that have already expired, and expiry costs nothing until someone looks.

You can see lazy expiry in MiniMemcached, which uses a controllable clock so you do not have to wait.

Python Watch an item expire

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Choosing a TTL

A TTL balances two risks. Too long and users see stale data; too short and the hit ratio drops while the backend works harder. A few rules of thumb:

  • Start from the business tolerance. "A product price may be one minute old" gives 60 s. "Yesterday's report" can be an hour.
  • Match the change rate. Data that changes every few seconds needs a TTL of a few seconds or explicit invalidation, covered in Invalidation Strategies.
  • Use longer TTLs for data that is expensive to rebuild and shorter for cheap data.
  • Cache negative results briefly. If a lookup finds "no such user", store that fact for a short time (5 to 30 seconds) so repeated lookups do not hit the database. A sentinel value distinguishes "cached: not found" from "not in cache".
  • Never rely on TTL for correctness alone when stale data causes harm. Combine it with explicit deletes.
Data Typical TTL
Rendered home page fragment 30 to 120 s
Product details 5 to 15 min
Static reference data (countries) 1 hour to 1 day
"Not found" markers 5 to 30 s
Rate-limit counters The length of the window

The mass-expiry problem and jitter

Suppose a deploy warms 50,000 keys at once, all with exptime=300. Five minutes later all 50,000 expire in the same second, and the database receives 50,000 rebuilds simultaneously. This synchronized expiry is a cousin of the stampede described in Stampede Protection.

The fix is jitter: add a random amount to each TTL so expiries spread out.

Python Jitter spreads the expiry load

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

With plus or minus 10% jitter the same 5,000 expiries spread across about 60 seconds, so the busiest second sees roughly 100 rebuilds instead of 5,000 at once.

Touching instead of re-setting

If you want to extend an item's life without changing its value, Memcached has touch (and gat, get-and-touch). That is useful for sliding expiration, such as session data that should live 30 minutes since the last use. You will use it in Append, Prepend, Incr, Decr and Touch.

Expiry versus eviction

Do not confuse the two. Expiry removes an item because its time ran out; it is predictable and you control it. Eviction removes a live item because memory is full; it is not predictable. A healthy cache has few evictions: evictions in stats should stay near zero for data you expect to live until its TTL.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does exptime 0 mean in Memcached?

    Zero means never expires by time, but a full cache can still evict it.
  2. 2. You pass 31 days in seconds as exptime. What happens?

    Values above 30 days are treated as Unix timestamps. 2,678,400 is in January 1970.
  3. 3. How does Memcached expire items?

    Each item holds its expiry time; the check happens on access or when the LRU maintainer sees the item.
  4. 4. Why add jitter to TTLs?

    Randomising each TTL a little spreads the rebuild work over time.

Summary

  • exptime 0 means never; up to 30 days is relative seconds; larger is a Unix timestamp; negative is already expired.
  • Expiry is lazy: expired items are never returned, and reclaimed when found.
  • Pick TTLs from how stale the data may be, and cache "not found" results briefly.
  • Add jitter so a batch of items does not expire in the same second.
  • Expiry is predictable; eviction (full memory) is not, and should be rare.

Next: Storage Commands: set, add, replace, get and delete