Become a Publisher with Redis and Ruby

Date:
Tags:
Redis Development Ruby
By

Lately I have been learning Redis and experimenting with the features it provides. One such feature is its publisher/subscriber functionality that allows a publisher to push messages to many subscribers. This functionality is deceptively easy to use and a very handy tool to know, so in this post I will describe how to use the this Redis functionality in Ruby.

Note: If you want to learn about Redis Redis in Action

#Sending Messages There are many problems that can be simplified to the publish subscribe pattern (pub/sub). Twitter is a great example of a massive publish and subscribe service, where someone publishes or tweets a message to many subscribers or followers.

The pub/sub pattern can also be used as an enterprise service bus to help scale an organisations internal messaging. As described in my previous post Enterprise Software and Building Infinite Staircases, there are many dispersed teams in enterprise organisations. Using the pub/sub pattern allows teams to communication while limiting interdependencies that could cause technology to become fragile.

These benefits of the pub/sub pattern can be summarised as:

  1. Reduced coupling: The publisher does not need to know anything about the subscriber and vice versa. The only parts where they are both dependant upon is where the message is published, and the message content.
  2. Scalability: A publisher does not know if it has one subscriber or thousands, and a subscriber does not know how many other subscribers there are. This independence of size means that this pattern can increase in scale without increasing in complexity.

#Redis Pub/Sub Redis is the most popular Key/Value store according to the monthly rankings at db-engines.com. It has functionality for lists, sets, sorted sets, and a few other data structures. It is also memory based, holding its entire data-set in memory at once making it very fast.

Redis also provides the functionality to publish a message to a channel, and for subscribers to listen to messages published on various channels. To experiment with this functionality first the redis gem needs to be installed to handle the communication aspects with Redis.

Note: I assume you already have a Redis server installed. If not brew install redis on OSX or apt-get install redis on Ubuntu is a good start

gem install redis

Then a subscriber script can be written:

ruby require 'redis' connection = Redis.new connection.subscribe 'stream' do |on| on.message do |channel, msg| puts "#{channel} says #{msg}" end end

Redis.new will create an object that connects to a Redis server on localhost with the default Redis port of 6379. The subscribe function takes a channel 'stream' and a block that yields an on callback. A block is given to the message function on the callback which is executed when a message is received. In this example the block just prints the channel and message to the console.

Note: This subscriber will run in an infinite loop listening for incoming messages.

A subscriber could also explicitly listen to multiple channels with:

ruby connection.subscribe 'stream:1', 'stream:2' do |on| on.message{ |channel, message|... } end

Or using wild cards to listen to channels described by a pattern:

ruby connection.psubscribe 'stream:*' do |on| on.pmessage{ |query, channel, message| ... } end

The publisher can then be written:

ruby require 'redis' connection = Redis.new connection.publish 'stream', 'Hello'

It is that easy; just call publish on a Redis connection with a channel stream with a message Hello. This will cause the subscriber above to write "stream says Hello" to the console.

#Conclusion The more I learn about Redis, the more I see potential uses for it in applications I create. It is a great tool to have on my tool belt and I will continue to experiment with the Redis's pub/sub, as well as the rest of its features.

Related Posts

comments powered by Disqus