Course contentsShow
← Node.js
Lesson 1345 of 1,98012. Caching Strategies and Application SpeedFree lesson

Configuring Expiration Policies for Dynamic Data

You will calculate optimal TTLs based on expected data volatility.

In this lesson, you will learn how to mathematically determine the optimal Time-To-Live (TTL) for your cached data based on how frequently that data changes.

The Balancing Act

Configuring expiration policies isn’t just about setting a random number of seconds; it’s a trade-off between Cache Hit Ratio and Data Freshness. If your TTL is too high, your users see stale data. If it’s too low, you defeat the purpose of caching by constantly hitting your primary database.

To calculate the optimal TTL, we use the formula: TTL = Δt / ε, where Δt represents the average time between updates and ε represents the maximum acceptable staleness threshold.

Calculating TTL in Practice

Imagine you are building a real-time stock price tracker. If your database updates prices every 10 seconds, and your business requirements allow for a maximum drift of 2 seconds, your optimal TTL is effectively 2 seconds. Conversely, if you are caching a user’s profile settings that only change once a week (604,800 seconds), a TTL of 3,600 seconds would be an unnecessary waste of cache resources.

When implementing this in Node.js, you can structure your cache logic to assign TTLs dynamically based on the "volatility score" of the data category:

const getVolatilityTTL = (dataType) => {
  const policies = {
    'user-profile': 3600,  // Low volatility: 1 hour
    'product-inventory': 60, // Medium: 1 minute
    'live-price-ticker': 2   // High: 2 seconds
  };
  return policies[dataType] || 300; // Default to 5 minutes
};

// Usage
const ttl = getVolatilityTTL('product-inventory');
cache.set(key, data, { ttl });

Avoiding "Herd" Expiration

When calculating these values, remember that if your TTLs are perfectly synchronized across many keys, you might trigger a "cache stampede" where thousands of items expire simultaneously. Always introduce a small jitter (random variance) to your calculated TTL to ensure keys expire at slightly different intervals, smoothing out the load on your Node.js event loop and backend databases.

Key Takeaway: The optimal TTL is the maximum duration your business logic can tolerate stale data, adjusted downward by a jitter factor to prevent simultaneous mass-expiration events.