Course contentsShow
← Software Engineering
Lesson 962 of 1,93330. Caching StrategiesFree lesson

Time-to-Live and Expiration Policies

Setting appropriate TTL values to balance data freshness against cache effectiveness and stale data risks.

Time-to-Live and Expiration Policies

What you'll learn: How to set cache expiration times that keep data fresh without sacrificing cache performance.


Every cached item needs an expiration policy—a rule that determines how long data stays valid before being refreshed. The Time-to-Live (TTL) is the duration a cached entry remains usable before it's considered stale and must be refetched from the original source.

The Core Challenge

Setting TTL values is a balancing act. Too short, and you're constantly invalidating cache entries, defeating the purpose of caching—your cache hit ratio plummets. Too long, and users see outdated data, potentially leading to business logic errors or poor user experience.

Think of TTL like milk in your refrigerator. The expiration date tells you when it's no longer safe to use. Set it too conservatively (1 day), and you waste perfectly good milk. Set it too liberally (3 months), and you risk serving spoiled milk.

Strategic TTL Selection

Different data types warrant different TTL values:

  • Highly dynamic data (stock prices, live sports scores): Short TTL (seconds to minutes)
  • Moderately stable data (user profiles, product details): Medium TTL (minutes to hours)
  • Static or rarely changing data (configuration settings, reference tables): Long TTL (hours to days)

Consider volatility, consistency requirements, and acceptable staleness for each cache key or data category.

Common Expiration Strategies

You can implement absolute expiration (expires at a specific time) or sliding expiration (extends TTL on each access). Some systems combine both: cache user sessions with sliding windows but hard limits.

The key is matching TTL to your data's natural change frequency and business tolerance for staleness.

Key Takeaway: TTL values must balance cache effectiveness against data freshness—choose expiration policies based on how frequently data changes and how much staleness your application can tolerate.