Skip to content
← All posts

cacheLife Is a Freshness Budget, Not Just a TTL

cacheLife expresses more than one deadline. It distinguishes client reuse, when server-side revalidation can begin, and when an old result must no longer be served while fresh work is pending. Choose those values from the behavior your product can accept.

“Cache it for five minutes” is not a complete requirement. Five minutes until what?

Start with the consequence of old data

Imagine a directory of public events. An outdated description is inconvenient. An outdated cancellation notice may send someone to the wrong place. Those fields do not automatically deserve the same policy.

Write a concrete requirement: a visitor may see a description that is several minutes old, but a cancellation must be pushed through an explicit update path. This separates ordinary freshness from exceptional corrections.

The cacheLife reference defines stale, revalidate, and expire. In particular, revalidate is not a timer that proactively refreshes every entry at the deadline. Request activity matters.

This is a server-side freshness sketch. Client reuse is another policy dimension, not the first box on the same server timeline.

Use explicit values for the discussion

Here is an illustrative policy, not a universal default:

cacheLife({
  stale: 30,
  revalidate: 120,
  expire: 600,
});

Read it as a product decision to review with the team. A client can reuse an entry for a limited period; the server can begin refreshing after its revalidation threshold; eventually a request must wait for a fresh result rather than receive the old one.

Check the current API constraints and your pinned framework version before choosing values. Do not copy these numbers into prices, permissions, or financial records just because the example is short.

Time-based freshness and invalidation complement each other

Time-based refresh is useful when occasional delay is acceptable. An explicit invalidation event is useful when your system knows that a relevant change has occurred.

For example, an event editor can publish a change and invalidate the affected data after the write commits. Tag revalidation provides one mechanism, with behavior determined by its selected profile.

The write should not report success merely because an invalidation was requested. Observe the resulting content too. A downstream CDN or an already-open browser may have its own reuse behavior.

Test the moments around a boundary

Use a test-only short policy and a source that exposes a revision number. Record the first response, a repeat response before revalidation, a request after revalidation becomes eligible, and a request after expiry.

Then make the source slow. This is where the distinction between background refresh and blocking work becomes visible. Finally, make the source fail and record the actual behavior of your application and host rather than assuming every cache handles errors identically.

Use a controllable clock where your test stack permits it. Otherwise, leave sufficient margins around boundaries so a flaky timing test does not masquerade as a cache bug.

Set the budget where the data is used

A lifetime is part of the data's meaning. Keep it close to the cached function so someone reviewing that function can see the freshness tradeoff.

Review policies after product changes. A directory that becomes a booking system has new correctness requirements, even if its components look the same.

The best value is not the largest lifetime your cache accepts. It is the longest reuse your product can justify, paired with an update path that works when waiting is not acceptable.

About the author