Databases 9 min read

Redis Expiration Strategies and Command Effects on Key TTL

This article explains Redis's expiration policies, detailing how commands like DEL, SET, INCR, LPUSH, PERSIST, RENAME, EXPIRE, and EXPIREAT affect key TTLs, and describes the lazy and periodic deletion mechanisms used by Redis to manage expired keys.

Architect's Tech Stack
Architect's Tech Stack
Architect's Tech Stack
Redis Expiration Strategies and Command Effects on Key TTL

Redis allows each key to have an expiration time, after which the key is automatically deleted. Understanding which commands affect the expiration time is essential for reliable data management.

Commands that clear expiration time

Commands that overwrite the key's value, such as DEL , SET , and GETSET , will remove any existing TTL.

127.0.0.1:6379> set mykey hello ex 300
OK
127.0.0.1:6379> ttl mykey
(integer) 294
127.0.0.1:6379> set mykey olleh
OK
127.0.0.1:6379> ttl mykey
(integer) -1

Commands that preserve expiration time

Operations that modify the value without overwriting it, such as INCR , LPUSH , and HSET , keep the existing TTL intact.

127.0.0.1:6379> set incr_key 1 ex 300
OK
127.0.0.1:6379> ttl incr_key
(integer) 291
127.0.0.1:6379> incr incr_key
(integer) 2
127.0.0.1:6379> ttl incr_key
(integer) 277

PERSIST command

Using PERSIST converts an expiring key into a persistent one, which also clears its expiration.

127.0.0.1:6379> set persist_key haha ex 300
OK
127.0.0.1:6379> ttl persist_key
(integer) 296
127.0.0.1:6379> persist persist_key
(integer) 1
127.0.0.1:6379> ttl persist_key
(integer) -1

RENAME command

When a key is renamed, the new key inherits the original key's expiration time, regardless of whether the target name already had a TTL.

127.0.0.1:6379> set key_a value_a ex 300
OK
127.0.0.1:6379> set key_b value_b ex 600
OK
127.0.0.1:6379> ttl key_a
(integer) 279
127.0.0.1:6379> ttl key_b
(integer) 591
127.0.0.1:6379> rename key_a key_b
OK
127.0.0.1:6379> ttl key_b
(integer) 248

Negative or past expiration values

Setting a negative TTL with EXPIRE or a past timestamp with EXPIREAT causes the key to be deleted immediately.

127.0.0.1:6379> set key_1 value_1
OK
127.0.0.1:6379> expire key_1 -1
(integer) 1
127.0.0.1:6379> get key_1
(nil)

127.0.0.1:6379> set key_2 value_2
OK
127.0.0.1:6379> expireat key_2 10000
(integer) 1
127.0.0.1:6379> get key_2
(nil)

Updating expiration time

The EXPIRE command can be used to update an existing TTL.

127.0.0.1:6379> set key_1 value_1 ex 100
OK
127.0.0.1:6379> ttl key_1
(integer) 95
127.0.0.1:6379> expire key_1 300
(integer) 1
127.0.0.1:6379> ttl key_1
(integer) 295

Redis expiration mechanisms

Redis combines lazy deletion (checking TTL on key access) with periodic deletion (randomly sampling expiring keys). Lazy deletion avoids extra CPU cost but can leave expired keys in memory if they are not accessed. Periodic deletion runs about ten times per second, sampling 20 keys each time and deleting those that have expired, repeating the process if more than 25% of sampled keys are expired, with a maximum scan time of 25 ms.

Source: http://suo.im/4mLCko

DatabaseRedisTTLcommandsexpirationLazy DeletionPeriodic Deletion
Architect's Tech Stack
Written by

Architect's Tech Stack

Java backend, microservices, distributed systems, containerized programming, and more.

0 followers
Reader feedback

How this landed with the community

login 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.