Stackexchange Redis notes - Events

The ConnectionMultiplexer type exposes multiple events:

  • ConfigurationChanged triggered when the connection configuration in the ConnectionMultiplexer is changed

  • ConfigurationChangedBroadcast receives a reconfigured message through the publish / subscribe function; This is usually due to the use of iserver Makemaster has changed the replication configuration of a node, or it can choose to broadcast a request to all customers.

  • ConnectionFailed when the connection fails; Note: you will not receive a ConnectionFailed notification for this connection until the connection is re established.

  • ConnectionRestored when reconnecting to the node that failed before

  • ErrorMessage when a user initiates a request, the Redis server will respond with an error message; This is immediately reported to the caller in addition to the general exception / failure.

  • HashSlotMoved when the "Redis cluster" indicates that the hash slot has been migrated between nodes, note: requests are usually automatically rerouted, so the user will not ask for anything specified here.

  • InternalError when an unexpected failure occurs in the internal execution of the Redis class library; This is mainly for debugging. Most users should not need this event.

Note: stackexchange The working principle of pub/sub implemented by redis is similar to that of events. Subscribe / SubscribeAsync accepts an Action

        ConnectionMultiplexer conn = GetConnection();
            conn.ConfigurationChanged += (object sender, EndPointEventArgs e) =>
                Console.WriteLine("When configuration changes");
            conn.ConfigurationChangedBroadcast += (object sender, EndPointEventArgs e) =>
                Console.WriteLine("When updating configuration through publish subscription");
            conn.ConnectionFailed += (object sender, ConnectionFailedEventArgs e) =>
                Console.WriteLine("The connection failed. You will not receive this notification if you reconnect successfully");
            conn.ConnectionRestored += (object sender, ConnectionFailedEventArgs e) =>
                Console.WriteLine("Error before reconnecting");
            conn.ErrorMessage += (object sender, RedisErrorEventArgs e) =>
                Console.WriteLine("An error occurred");
            conn.HashSlotMoved += (object sender, HashSlotMovedEventArgs e) =>
                Console.WriteLine("Change cluster");
            conn.InternalError += (object sender, InternalErrorEventArgs e) =>
                Console.WriteLine("redis Class library error");

key invalidation event listening: (not tested)

  1. Events are distributed through Redis' subscription and publishing function (pub/sub), so subscription is required keyevent@0:expired channel
    0 means that db0 selects the appropriate number according to its own dbindex

  2. Modify redis Conf file, modify notify keyspace events ex


K key space notification to__ keyspace@__ Is prefix
E key event notification to__ keysevent@__ Is prefix
G notification of generic commands independent of type, such as del, expand, rename
$String command
L list command
S set command
H hash command
z ordered set command
x expiration event (generated each time a key expires)
e expulsion event (generated when the key is cleared when the memory is full)
A g$lshzxe alias, so "AKE" means all events

  1. Restart redis to test the triggering of invalid events. The value obtained by listening is key
ConnectionMultiplexer conn1 = GetConnection();
            ISubscriber subscriber = conn1.GetSubscriber();
            subscriber.Subscribe("__keyspace@0__:*", (channel, notificationType) =>
                Debug.WriteLine(channel + "|" + notificationType);
            subscriber.Subscribe("__keyevent@0__:expired", (channel, notificationType) =>
                Debug.WriteLine(channel + "|" + notificationType);

Tags: Redis

Posted by daok on Wed, 01 Jun 2022 00:57:36 +0530