Databases 5 min read

How Redis Handles Expired Keys: Periodic vs Lazy Deletion Strategies

This article explains Redis's two expiration mechanisms—periodic scanning with configurable frequency and lazy deletion on client access—detailing their configurations, execution steps, performance trade‑offs, and replication pitfalls to help developers manage memory efficiently.

Ma Wei Says
Ma Wei Says
Ma Wei Says
How Redis Handles Expired Keys: Periodic vs Lazy Deletion Strategies

Periodic Deletion Strategy

Redis stores the expiration time of each key in a dedicated expiration dictionary. A server‑cron task runs at a frequency defined by the hz setting in redis.conf (default 10 scans per second, i.e., every 100 ms) to clean up expired keys.

During each scan Redis does not traverse the whole dictionary; instead it randomly selects a small batch of keys (20 by default), checks their expiration, and deletes the ones that have expired. After the batch is processed, Redis checks the ratio of expired keys; if it exceeds 25 % the scan repeats immediately, otherwise it waits until the next scheduled interval.

Periodic deletion configuration
Periodic deletion configuration

The hz parameter can be set from 1 to 500. Setting it too high may cause excessive CPU usage because deletion operations become frequent and long; setting it too low may let expired keys linger, wasting memory. In most cases a value above 100 is unnecessary unless the workload demands ultra‑low latency.

Periodic deletion flow
Periodic deletion flow

1) Server‑cron triggers based on the hz frequency.

2) Randomly pick 20 keys from the expiration dictionary.

3) Delete the selected expired keys.

4) If the expired‑key ratio remains above 25 %, repeat steps 2–3; otherwise finish the current cycle.

Lazy Deletion Strategy

When a client issues a read or write command, Redis first checks whether the target key has expired. If it has, the key is immediately removed and no value is returned to the client. This on‑demand cleanup is CPU‑friendly because it only runs when a key is accessed.

The downside is that expired keys that are rarely accessed may remain in memory for a long time, increasing overall memory consumption. Therefore lazy deletion is most effective when keys expire shortly after their last use.

In a master‑slave replication setup, care must be taken because a slave that falls behind may not receive the master's expiration‑deletion events. Consequently, expired keys can persist on the slave, leading to memory leaks and potential consistency issues.

memory managementRedisdatabasesExpirationLazy DeletionPeriodic Deletion
Ma Wei Says
Written by

Ma Wei Says

Follow me! Discussing software architecture and development, AIGC and AI Agents... Sometimes sharing insights on IT professionals' life experiences.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.