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

Data Eviction Strategies for Time-Series Data

You will purge historical data efficiently using time-based windowing.

In this lesson, you will learn how to maintain high performance in your Node.js applications by implementing time-based windowing to purge stale time-series data.

When dealing with time-series data—like sensor logs, stock ticks, or server metrics—your database or cache can quickly become bloated. Unlike a simple LRU cache where you evict the "least recently used" item, time-series data follows a chronological progression where older data naturally loses its relevance. Keeping everything forever leads to performance degradation and memory spikes.

Implementing Time-Based Windowing

The most efficient strategy for time-series data is the "Rolling Window" approach. Instead of checking every record for an expiration timestamp, you define a fixed retention period (e.g., 24 hours) and periodically truncate data that falls outside that window.

In a Redis-backed Node.js environment, you can use sorted sets (ZSET) where the score represents the Unix timestamp of the data point. You can prune historical data using ZREMRANGEBYSCORE to remove all entries older than your cut-off threshold in a single atomic operation.

async function pruneOldMetrics(redisClient, streamKey, retentionSeconds) {
  const cutoff = Date.now() - (retentionSeconds * 1000);
  
  // Remove all entries with a score (timestamp) less than the cutoff
  const removedCount = await redisClient.zRemRangeByScore(
    streamKey, 
    '-inf', 
    cutoff
  );
  
  console.log(`Evicted ${removedCount} stale data points.`);
}

Why This Matters

By using a score based on time, you decouple the eviction process from the frequency of access. You no longer need to worry about read patterns (like in LRU). Instead, you define a strict data lifecycle: "Keep only the last X minutes/hours."

This strategy prevents your memory usage from growing linearly with time. By automating this pruning—perhaps triggered by a background interval or after a specific number of new writes—you keep your data structure lean, ensuring that lookups remain fast regardless of how long the application has been running. This is critical for maintaining consistent application speed as your system scales.

Key Takeaway: Use time-based windowing with score-sorted structures to atomically purge stale time-series data, ensuring memory footprint remains constant regardless of the total data volume.