redis

Is there a connection between Redis pub/sub and the notifyKeyspaceEvent function?


This code snippet is part of the sremCommand function in the t_set.c file of Redis.

The srem command removes one or more members from a set specified by a key.

When one or more members are successfully deleted, the deleted variable is set to 1, triggering the execution of the contents inside the if condition.

    if (deleted) {
        signalModifiedKey(c,c->db,c->argv[1]);
        notifyKeyspaceEvent(NOTIFY_SET,"srem",c->argv[1],c->db->id);
        if (keyremoved)
            notifyKeyspaceEvent(NOTIFY_GENERIC,"del",c->argv[1],
                                c->db->id);
        server.dirty += deleted;
    }

Inside the if condition, the function notifyKeyspaceEvent is used to pass type, event, key, and dbid. I'm curious about how exactly this function operates.

I'm also wondering if it is related to `Redis pub/sub.

If there is a connection, please explain how notifyKeyspaceEvent is used in Redis pub/sub.

have a great day - kevin


Solution

  • Redis keyspace notifications are an optional feature that you can turn on via the redis.conf file or using the CONFIG SET command.

    There are a fair number of options on the types of notifications, but all of them are communicated using the PubSub mechanism already in Redis. These events are a great way to watch for key expiration, deletion, and eviction. And you get events based on either the keyname or the event type. So, you can monitor a single key for all events. Or a single event for all keys.