TIL: Redis Keyspace Notifications
Tags: codes, tech, thoughts, rediscallback
Published: 2018-03-19 18:25:57

---
title: TIL: Redis Keyspace Notifications
published: true
tags: Codes,Tech,Thoughts,redis callback
canonical_url: 
---

For some reason, I was figuring out a way to be able to listen to Redis so I can fire callbacks when the Redis command or event  is encountered.

I wanted to do something like:

> **When a key gets removed from the sorted set, it will trigger a script that will notify a user. **

Browsing online, I came across the so-called [**Redis Keyspace Notifications**](https://redis.io/topics/notifications). It basically acts as a Pub/Sub mechanism that enables clients to subscribe to a Redis channel and receive “published” events whenever a Redis command or data alteration is encountered. The feature is available since version 2.8, apparently.

## Diving in…

 ![Literally a person who is diving in. LOL](https://i1.wp.com/blog.katpadi.ph/wp-content/uploads/2017/01/1.1282587206.mark-diving-in.jpg?resize=491%2C369)

Literally a person who is diving in. LOL

The said notifications are enabled using the `notify-keyspace-events` of redis.conf or via the **CONFIG SET**. It is disabled by default.

Setup will look something like:

 
```
# Set it up.
$redis.config(:set, 'notify-keyspace-events', 'zE')
```
 
 [redis-kesypace-event.rb](https://gist.github.com/2eca03737eb188707cc79e9969185eee "See redis-kesypace-event.rb") [view raw](https://gist.githubusercontent.com/katpadi/2eca03737eb188707cc79e9969185eee/raw/213a4d7f0f2055ed2853ba3997b382d9da9ec61d/redis-kesypace-event.rb "Back to redis-kesypace-event.rb") 
 

## zE… wut???

zE looks so random if you weren’t briefed that each character in that string has a meaning.

Below is the list of variable mapping for keyspace events:

![](https://i1.wp.com/blog.katpadi.ph/wp-content/uploads/2017/01/Screen-Shot-2017-01-12-at-6.48.57-PM.png?resize=752%2C294)

For my particular case, I needed to use “`zE`” string because I want to catch the name of the key (and not the name of the event) when `zrem`, which is a sorted set command, is encountered.

## Keyspace v.s. Keyevent

Let’s cut to the chase! Use keyspace if you want to receive the **name of the event**. Use keyevent if you want to receive the **name of the key**.

###### _TILception (TIL within a TIL): If you want to enable all events, use “KEA”._

So, here’s an example of what I played around with:

 
```
# Subscriber
$redis.psubscribe(' __keyevent@*__ :zrem') do |on|
  on.pmessage do |pattern, channel, message|
    puts "==== AFTER zrem EVENT ====="
    # Available things:
    puts "pattern: #{pattern}"
    puts "channel: #{channel}"
    puts "message: #{message}"
  end
end
```
 
 [redis-kesypace-event.rb](https://gist.github.com/2eca03737eb188707cc79e9969185eee "See redis-kesypace-event.rb") [view raw](https://gist.githubusercontent.com/katpadi/2eca03737eb188707cc79e9969185eee/raw/213a4d7f0f2055ed2853ba3997b382d9da9ec61d/redis-kesypace-event.rb "Back to redis-kesypace-event.rb") 
 

As I told earlier, I wanted to catch the “key” as a message and not the “event”. So, the message variable in the above code will output the key.

Now, in your redis-cli, if you try to prepare a sorted set by using `zadd` and then doing a `zrem`, you will see the output in your console.

=)

Now it’s time for me to send that notification!

The post [TIL: Redis Keyspace Notifications](http://blog.katpadi.ph/til-redis-keyspace-notifications/) appeared first on [KatPadi's Point](http://blog.katpadi.ph).