Databases 10 min read

Master Redis Expiration: Key Commands That Preserve or Reset TTL

This article explains how Redis handles key expiration, detailing which commands clear or keep TTL, how PERSIST and RENAME affect expiration, the impact of negative or past timestamps, and the lazy and periodic deletion strategies Redis uses to manage expired keys efficiently.

Programmer DD
Programmer DD
Programmer DD
Master Redis Expiration: Key Commands That Preserve or Reset TTL

Things to watch when setting expiration for a key

1. DEL/SET/GETSET commands clear expiration

// set mykey with an expiration of 300s
127.0.0.1:6379> set mykey hello ex 300
OK
// check TTL
127.0.0.1:6379> ttl mykey
(integer) 294
// overwrite mykey with set
127.0.0.1:6379> set mykey olleh
OK
// TTL is cleared
127.0.0.1:6379> ttl mykey
(integer) -1

2. INCR/LPUSH/HSET do not clear expiration

INCR only modifies the value and keeps the TTL:

// set incr_key with expiration 300s
127.0.0.1:6379> set incr_key 1 ex 300
OK
127.0.0.1:6379> ttl incr_key
(integer) 291
// increment
127.0.0.1:6379> incr incr_key
(integer) 2
127.0.0.1:6379> ttl incr_key
(integer) 277

LPUSH adds elements to a list without clearing its TTL:

// create a list and set expiration
127.0.0.1:6379> LPUSH list 1
(integer) 1
127.0.0.1:6379> expire list 300
(integer) 1
127.0.0.1:6379> ttl list
(integer) 292
// push another value
127.0.0.1:6379> lpush list 2
(integer) 2
127.0.0.1:6379> ttl list
(integer) 252

3. PERSIST command clears expiration

// set a key with expiration
127.0.0.1:6379> set persist_key haha ex 300
OK
127.0.0.1:6379> ttl persist_key
(integer) 296
// make it persistent
127.0.0.1:6379> persist persist_key
(integer) 1
// TTL is removed
127.0.0.1:6379> ttl persist_key
(integer) -1

4. RENAME transfers expiration to the new key

// set key_a with 300s expiration
127.0.0.1:6379> set key_a value_a ex 300
OK
// set key_b with 600s expiration
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
// rename key_a to key_b
127.0.0.1:6379> rename key_a key_b
OK
// new key_b inherits key_a's TTL
127.0.0.1:6379> ttl key_b
(integer) 248

5. EXPIRE/PEXPIRE with negative TTL or EXPIREAT/PEXPIREAT with past timestamps delete the key

EXPIRE with -1:

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

EXPIREAT with a past timestamp:

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

6. EXPIRE can update an existing expiration

// set key_1 with 100s expiration
127.0.0.1:6379> set key_1 value_1 ex 100
OK
127.0.0.1:6379> ttl key_1
(integer) 95
// update to 300s
127.0.0.1:6379> expire key_1 300
(integer) 1
127.0.0.1:6379> ttl key_1
(integer) 295

In Redis versions prior to 2.1.3, updating the TTL with expire may fail, and modifying a key’s value with commands like LPUSH/HSET could cause the key to be deleted.

Redis expiration strategy

Redis combines lazy deletion and periodic deletion to handle expired keys efficiently.

Lazy deletion

When a client accesses a key, Redis checks its expiration time and deletes it immediately if it has expired. This avoids extra CPU usage but leaves long‑unaccessed expired keys in memory, consuming resources.

Periodic deletion

Redis stores all keys with an expiration in a dictionary and, at regular intervals, randomly samples a subset of those keys to check and delete the expired ones.

By default, Redis performs 10 expiration scans per second, each scanning 20 random keys. If more than 25 % of the sampled keys are expired, the scan repeats. The total scan time is limited to 25 ms to prevent long pauses.

https://redis.io/commands/expire#expire-accuracy
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

redisTTLkey managementExpirationLazy DeletionPeriodic Deletion
Programmer DD
Written by

Programmer DD

A tinkering programmer and author of "Spring Cloud Microservices in Action"

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.