Redis data structure change notifications

Hi all,

everyone knows Redis, right? Redis is an open source (BSD licensed) data structure server with extended pub/sub features, allowing applications to subscribe in Redis channels & be able to be notified when a channel update occurs, while other apps (in the other side) generate the updates, publishing their update in particular Redis channels.

Although, in this post, i am already assuming that you are familiar with Redis and already know how to code a script that publishes to/subscribes in a channel, & you already know how to manipulate some of Redis data structures, such as the Hashes, the Sets & the List.

We are interested in receiving data structure updates in order to know that something has changed & if our app is interested in that particular change, then it should do something useful with it. In addition, we would like our app to be interested in only one specific data structure of redis, one specific structure that holds data of interest for our app.

Redis supports pubsub for channels but, can a data structure has its own channel? Yes, by using Redis Keyspace Notifications, from version 2.8.0. Thus, when a data structure update change occurs, a Redis event is generated & propagated through the channel. Your app just needs to listen to that channel in order to be notified about the event.

The name of that particular channel is formed as : __keyspace@0__:" "data structure name"Thus, if your app is interested in that specific list named “registeredArtists“, then your app should subscribe & listen to the channel __keyspace@0__:registeredArtists.

Demo

Requires:

  • linux
  • redis
  • Python
  • python-pip redis module

1.1 Client

In order to show you the functionality, i prepared a small Python demo for it:

from redis import StrictRedis
from time import sleep
from sys import argv  def show_msg(msg):
 print ‘Latest List Operation =>’,msg

 def listen_for_data_structure_operations(dt_key,host=‘localhost’):
     redis = StrictRedis(host=host)
     s = redis.pubsub()
     s.subscribe(**{ ( ‘__keyspace@0__:%s’ % dt_key) :show_msg})

    while True:
         msg = s.get_message()
       if msg : print msg
         sleep(1)

if __name__ == ‘__main__’:
    if len(argv) > 1:
          key = argv[1]
    else:
          key = ‘alist’
      listen_for_data_structure_operations(key)

 

If you run the code with argument the name of a redis data structure, your script will subscribe to the channel of changes/operations applying in that particular data structure. The data structure does not necessarily need to exist in order to subscribe to it.

 gclkaze@tzertzelos:~/Tzertzelos/Redis$ python dtUpdateTest.py registeredArtists
{‘pattern’: None, ‘type’: ‘subscribe’, ‘channel’: ‘__keyspace@0__:registeredArtists’, ‘data’: 1L}

1.2 Server

Now, use the terminal to connect to redis, and

gclkaze@tzertzelos:~/Tzertzelos/Scriptz/Redis$ redis-cli
127.0.0.1:6379> LPUSH registeredArtists “Motorhead”
(integer) 1

OK, our new list named registeredArtists has length equal to 1, and its only element is the string Motorhead.

1.3 Client-side update

The notification generated after the LPUSH in the server should be visible in the client side!

gclkaze@tzertzelos:~/Tzertzelos/Scriptz/Redis$ python dtUpdateTest.py registeredArtists
{‘pattern’: None, ‘type’: ‘subscribe’, ‘channel’: ‘__keyspace@0__:registeredArtists’, ‘data’: 1L}

Latest List Operation =>  

{‘pattern’: None, ‘type’: ‘message’, ‘channel’: ‘__keyspace@0__:registeredArtists’, ‘data’: ‘lpush’}

Yea! Our client was able to capture the event generated by the LPUSH that applied to the list that resides in the server.

Redis’ pubsub feature really simplifies the implementation of a push mechanism between theserver & the app, that is actually a core feature of the system.

Now go & experiment with Redis!

kazeone

Linx

An introduction to Redis data types and abstractions

Redis Commands

Redis Keyspace Notifications

Musix

Truckfighters – Gravity X (2005)

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s