Redis › Expiration & Eviction
How does Redis actually implement key expiration internally, using both a passive and an active approach?
AdvancedAnswer
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 frequencyReal-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.