Expiration & Eviction

7 questions found

How do you set an expiration time on a Redis key so it automatically gets deleted after a certain period?

Beginner
You can set an expiration time when creating a key using SET with the EX option for seconds or PX for milliseconds, or apply it to an already existing key using the EXPIRE command, after which Redis automatically removes the key once that time has passed without needing any manual cleanup.
SET session:abc123 'userdata' EX 3600
EXPIRE session:abc123 3600
Real-world example A web application stores a user's login session in Redis with a one hour expiration, automatically logging them out and cleaning up the session data without needing a separate scheduled cleanup process.

Common follow-ups: What happens if you set a new value on a key that already had an expiration set?;How do you check how much time is left before a key expires?

Session Management with Redis;Caching Patterns

What is the difference between the EXPIRE, TTL, and PERSIST commands in Redis?

Beginner
EXPIRE sets a countdown timer on a key after which it will be automatically deleted, TTL checks how much time remains before that countdown reaches zero, and PERSIST removes any existing expiration from a key, making it permanent again until explicitly deleted or given a new expiration.
EXPIRE mykey 60
TTL mykey -- returns remaining seconds
PERSIST mykey -- removes the expiration entirely
Real-world example A promotional discount code is given a temporary expiration using EXPIRE, checked periodically with TTL to show a countdown to customers, and made permanent using PERSIST if the promotion is later extended indefinitely.

Common follow-ups: What does TTL return if a key has no expiration set at all?;What does TTL return if the key does not exist at all?

Redis CLI & Basic Commands;Caching Patterns

What eviction policies are available in Redis for handling the situation when memory usage reaches its configured maximum limit?

Intermediate
Redis offers several eviction policies including noeviction, which rejects new writes once memory is full, allkeys-lru and volatile-lru, which remove the least recently used keys, allkeys-lfu and volatile-lfu, which remove the least frequently used keys, and random based policies, letting you choose the behavior that best fits whether your data is primarily used as a cache or as a persistent store.
CONFIG SET maxmemory-policy allkeys-lru
CONFIG SET maxmemory 2gb
Real-world example A caching layer for a busy website configures the allkeys-lru eviction policy, ensuring that when memory fills up, Redis automatically removes the least recently accessed cached items to make room for new ones.

Common follow-ups: What is the difference between the allkeys and volatile variants of these eviction policies?;What happens if you use noeviction and memory becomes completely full?

Redis Memory Optimization;Redis Architecture & Installation

What is the difference between the LRU and LFU eviction algorithms, and when would you choose one over the other?

Intermediate
LRU, meaning least recently used, evicts the key that has not been accessed for the longest time, while LFU, meaning least frequently used, evicts the key that has been accessed the fewest number of times overall, with LFU generally being a better choice when some keys are accessed very frequently over a long period even if there are occasional gaps between accesses.
CONFIG SET maxmemory-policy allkeys-lfu
Real-world example A recommendation engine that repeatedly accesses certain popular product keys throughout the day, but with occasional gaps, chooses LFU eviction to avoid accidentally evicting those consistently popular keys just because they were not accessed in the last few minutes.

Common follow-ups: How does Redis actually track access frequency for the LFU algorithm?;Which eviction policy is the more commonly recommended default for a general purpose cache?

Redis Memory Optimization;Caching Patterns

How would you diagnose and resolve a situation where important data is being unexpectedly evicted from Redis due to memory pressure?

Advanced
You would check the current maxmemory setting and eviction policy, review which keys are consuming the most memory, determine whether you actually need more available memory or a different eviction policy, and consider whether some data should be marked without an expiration and moved to a separate Redis instance using noeviction to guarantee it is never evicted.
CONFIG GET maxmemory-policy
INFO memory
MEMORY USAGE somekey
Real-world example A team discovers important configuration data was being evicted from a shared Redis cache under memory pressure, so they move that critical data to a separate Redis instance configured with noeviction to guarantee it is never accidentally removed.

Common follow-ups: Should critical, non expendable data be kept in the same Redis instance as cache data at all?;How do you calculate how much memory your critical data actually needs?

Redis Memory Optimization;Redis Monitoring & Observability

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

Advanced
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?

Redis Performance Tuning & Benchmarking;Redis Architecture & Installation

How do you monitor how many keys are currently being evicted from Redis due to memory pressure, and why is this an important metric to track?

Intermediate
You check the evicted_keys statistic available through the INFO command, and a consistently rising number of evictions can indicate your Redis instance does not have enough memory for your workload, which is important to monitor since unexpected evictions of data your application still needs can lead to increased load on your backing database or degraded user experience.
redis-cli info stats | grep evicted_keys
Real-world example A team notices a steadily increasing eviction count in their monitoring dashboard, prompting them to either increase their Redis instance's available memory or investigate why so much data is being stored that does not truly need to remain cached.

Common follow-ups: What is a healthy or expected eviction rate for a typical caching workload?;How do you set up an alert for when evictions start increasing unexpectedly?

Redis Monitoring & Observability;Redis Memory Optimization