Learnitweb

Redis Key Deletion Notifications

In the previous section, we learned how to receive key expiration notifications from Redis using the ExpiredObjectListener.
Now, let’s explore another important feature — getting notified when a Redis key is explicitly deleted.

Redis can trigger events not only when keys expire but also when they are manually deleted using commands such as DEL.
With Redisson, we can easily listen to such deletion events using the DeletedObjectListener interface.


1. Why Listen for Key Deletion Events?

Key deletion events are useful in many real-world scenarios, especially in distributed systems where multiple microservices or nodes share the same Redis cache.
You may want to react when a key is deleted to:

  • Clear dependent cache entries.
  • Invalidate in-memory data structures or local caches.
  • Synchronize state across multiple services.
  • Trigger audit logs or business workflows.

Instead of constantly polling Redis, you can rely on event-based notifications that automatically tell you when a key is removed.


2. Introducing the DeletedObjectListener

Just like ExpiredObjectListener notifies you when a key expires, DeletedObjectListener notifies you when a key is deleted.
This listener is part of Redisson’s event-driven API, which allows you to subscribe to specific Redis key events.

The interface is simple:

public interface DeletedObjectListener {
    void onDeleted(String key);
}

It contains a single method that provides the key name that was deleted.

Because it’s a functional interface (single abstract method), you can also use a lambda expression instead of a traditional class-based implementation.


3. Example: Listening for Redis Key Deletion Events

Let’s look at an example to understand how this works step-by-step.

import org.redisson.api.RBucket;
import org.redisson.api.RedissonClient;
import org.redisson.api.listener.DeletedObjectListener;

public class KeyDeletionExample {

    public static void main(String[] args) throws InterruptedException {
        RedissonClient redisson = RedissonConfig.getClient();

        // Create a bucket (key) that will not expire automatically
        RBucket<String> bucket = redisson.getBucket("user:1:name");
        bucket.set("John");
        System.out.println("Key 'user:1:name' created and stored in Redis.");

        // Add a listener for deletion events
        redisson.getKeys().getKeys().addListener((DeletedObjectListener) deletedKey -> {
            System.out.println("Deleted Key: " + deletedKey);
        });

        // Keep the program running for 1 minute to listen for events
        System.out.println("Waiting for manual deletion...");
        Thread.sleep(60000);

        System.out.println("End of program.");
    }
}

What’s Happening Here

  1. We create a Redis key user:1:name and assign it a value. This key has no TTL — it will stay until manually deleted.
  2. We register a DeletedObjectListener to listen for deletion notifications.
  3. We then keep the program running for a minute using Thread.sleep(60000) so we can delete the key manually from Redis CLI during this time.
  4. When you delete the key manually using DEL user:1:name, Redis will emit a deletion event, and our listener will receive it instantly.

4. Manually Triggering the Deletion

While your Java program is running, open a Redis CLI session and delete the key manually:

DEL user:1:name

Once you execute this command, switch back to your console where the Java program is running.
You should see an output similar to this:

Key 'user:1:name' created and stored in Redis.
Waiting for manual deletion...
Deleted Key: user:1:name
End of program.

This confirms that your listener successfully received the key deletion notification from Redis.


5. Why It Works – Understanding Redis Event Mechanism

Redis internally publishes events on special channels whenever specific actions happen to keys (like creation, modification, expiration, or deletion).
Redisson subscribes to those channels and exposes them through listener interfaces.

However, these events will only work if keyspace notifications are enabled in Redis — just like in the key expiration example.

You can verify or enable them using the following Redis CLI command:

CONFIG SET notify-keyspace-events AKE

This configuration allows Redis to emit events for:

  • A: All types of key events
  • K: Keyspace notifications
  • E: Expiration and deletion events

Without enabling this configuration, your listeners won’t receive any notifications.


6. Behavior Across Multiple Instances

Redis events are broadcast to all connected clients that have registered listeners.
So if multiple microservices or Java applications are connected to the same Redis server, each one will receive the deletion event notification.

This makes it extremely useful in distributed caching systems where data consistency must be maintained across nodes.

For example:

  • If Service A deletes a user key, Service B can immediately clear its local cache or update a dashboard.
  • If you have multiple Redisson clients connected, all of them will receive the same event in real time.

7. Summary

Let’s summarize what we learned:

  • The DeletedObjectListener allows you to listen for Redis key deletion events.
  • You can use it in Redisson just like the ExpiredObjectListener.
  • Redis emits these events only when keyspace notifications are enabled (notify-keyspace-events AKE).
  • You can use a lambda expression for concise listener implementation.
  • Multiple applications connected to Redis will all receive the deletion event.

By combining expiration and deletion listeners, your Java application can react dynamically to Redis key lifecycle changes — enabling smarter caching, real-time synchronization, and cleaner data management.