Using the Redis NoSql database with .NET Part 8: messaging
April 19, 2017 Leave a comment
Introduction
In the previous post we looked at transactions in Redis. A transaction is a collection of database operations that are executed as a unit. The commands of a transaction are executed atomically, meaning that the process is not interrupted by an external operation. The commands either pass or fail together. Redis has at least partial implementation of transactions. We can start and execute transactions but if one or more commands fail then the other members are still executed. In other words there is no rollback mechanism. Redis also offers a concurrency check through the WATCH command if we want to perform the transaction on an unchanged set of resources.
Redis also offers a basic messaging system which will be the topic of this post.
Publish and subscribe
Messaging is a technique to solve communication between disparate systems in a reliable and maintainable manner. You can have various platforms that need to communicate with each other: a Windows service, a Java web service, an MVC web application etc. Messaging aims to integrate these systems so that they can exchange information in a decoupled fashion. A related term in this area is Publish/Subscribe or PubSub. The publisher posts a message to a channel and one or more subscribers that watch that channel receive that message. The publisher is completely unaware of the subscribers. The subscribers are free to subscribe to or unsubscribe from a channel. In a sense it’s like publishing a message on Twitter. Every Twitter user will be free to follow or unfollow your channel and you have no control over what they do with your tweets: respond, retweet, ignore, whatever. Applications that can relay messages from publishers to subscribers are often called message brokers.
Messaging is a large topic and we’ll not go through the details in this post. If you’re interested then we took up two different message brokers on this blog before:
- RabbitMq here and here
- Amazon Kinesis here
- MassTransit service bus
Full-blown message brokers are complex pieces of software with channels, queues, bindings, acknowledgements, time-to-live, dead letter queues and much more. Redis offers a limited implementation of the publish-subscribe pattern. Redis clients can publish messages to a channel and other clients can listen to them, that’s it. It’s very simple and it can be all you need in many cases so that you don’t have to go for a more complex system.
PubSub commands in Redis
The following are the most important pub/sub related commands in Redis:
- SUBSCRIBE followed by one or more channel names: subscribe to the specified channels
- PUBLISH: publish a message to a channel
- UNSUBSCRIBE followed by one or more channel names: stop watching for messages from the given channels
- PSUBSCRIBE and PUNSUBSCRIBE: same as SUBSCRIBE and UNSUBCRIBE but they accept a pattern instead of a concrete channel name. All channel names that match the pattern will be subscribed to or unsubscribed from.
Demo
Open a command prompt and run redis-cli. This first command window will be the publisher. Open two more command windows and start redis-cli on both of them. These two will act as subscribers. Run the following commands in the two subscribers:
SUBSCRIBE world-news
The console will respond as follows:
Reading messages… (press Ctrl-C to quit)
1) “subscribe”
2) “world-news”
3) (integer) 1
Let’s publish some news from the publisher:
PUBLISH world-news “Everyone should learn Redis, experts say”
PUBLISH will respond with an integer, 2 in this case, meaning that the message was relayed to two listeners.
There should now be three more entries in the subscriber windows:
1) “message”
2) “world-news”
3) “Everyone should learn Redis, experts say”
Open a fourth command window and run redis-cli in it as well. We’ll make it listen to three channels:
SUBSCRIBE world-news recipes programming
We’ll publish a message to programming and another to world-news from the publisher window:
PUBLISH programming “Redis is awesome”
PUBLISH world-news “Intelligent life found on Mars”
The first message was only collected by the third subscriber but all three of them picked up the world-news message since all of them have subscribed to that channel.
That’s it really, messaging is not more complicated than that in Redis. PSUBSCRIBE follows the same type of pattern matching like we saw before, here’s the relevant documentation:
h?llo subscribes to hello, hallo and hxllo
h*llo subscribes to hllo and heeeello
h[ae]llo subscribes to hello and hallo, but not hillo
If you want to have some super-subscriber that listens to all channels then you can write…:
PSUBSCRIBE *
We’re actually done with the basics of communicating with Redis through the native client. We’ll return to other commands later on but in the next post we’ll start looking into the Redis .NET library.
You can view all posts related to data storage on this blog here.