Back to results for “Architecture”
Redis › Expiration & Eviction

How does Redis actually implement key expiration internally, using both a passive and an active approach?

Advanced

Answer

Redis uses passive expiration, where a key is checked and removed the moment it is actually accessed and found to be expired, combined with active expiration, where a background process periodically samples a random set of keys with expiration times and removes any that have already expired, ensuring memory used by expired keys is eventually reclaimed even if they are never directly accessed again.
-- Active expiration runs periodically in the background
-- sampling keys with an expiration set
CONFIG GET hz -- controls background task frequency
Real-world example A team notices memory usage stays reasonably controlled even for expired session keys that are never actually accessed again, thanks to Redis's active expiration cycle periodically cleaning them up in the background.

Common follow-ups: How often does the active expiration cycle run by default?;Can active expiration cause a noticeable performance impact on a busy server?