Databases 5 min read

Do Expired Redis Keys Get Deleted Instantly?

Redis uses both time‑based and lazy expiration, marking keys as expired at their TTL but only removing them on the next access or during periodic cleanup, so expired keys are not deleted the instant they expire.

java1234
java1234
java1234
Do Expired Redis Keys Get Deleted Instantly?

Redis provides two expiration strategies—time‑based and lazy—to manage key lifetimes.

Time‑Based Expiration (Time‑Based Expiration) : When the EXPIRE command sets a TTL, Redis marks the key as expired at the specified moment. The key is physically removed only when the next operation (read or write) accesses it.

Lazy Expiration : Redis does not delete a key at the exact expiration instant. Instead, it waits until the key is accessed again, which saves CPU cycles under high load.

In addition, Redis runs a periodic cleanup process that scans a fraction of keys with expiration flags and frees their memory, ensuring that even never‑accessed expired keys are eventually reclaimed.

The following Java example using Jedis demonstrates the behavior. It creates a key myKey with a 5‑second TTL, then loops once per second to read the key. After the TTL expires, jedis.get("myKey") returns null, and the loop exits, showing that the key is removed only on access.

import redis.clients.jedis.Jedis;
public class RedisExpireExample {
    public static void main(String[] args) {
        // Create Jedis object
        Jedis jedis = new Jedis("localhost", 6379);
        // Set key and expiration of 5 seconds
        jedis.set("myKey", "myValue");
        jedis.expire("myKey", 5);
        System.out.println("Set key myKey with value myValue and set TTL to 5 seconds.");
        // Access myKey every second
        for (int i = 0; i < 10; i++) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            String value = jedis.get("myKey");
            if (value != null) {
                System.out.println("Value of myKey: " + value);
            } else {
                System.out.println("myKey has expired.");
                break; // Exit loop if key expired
            }
        }
        // Close Jedis connection
        jedis.close();
    }
}

In summary, expired Redis keys are not deleted the instant they expire; they are removed either on the next access or during Redis's periodic cleanup, which helps developers design more efficient memory‑aware applications.

JavaRedisJedisLazy DeletionKey Expiration
java1234
Written by

java1234

Former senior programmer at a Fortune Global 500 company, dedicated to sharing Java expertise. Visit Feng's site: Java Knowledge Sharing, www.java1234.com

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.