Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redis as a message broker

Tags:

redis

Question

I want to pass data between applications, in a publish-subscribe manner. Data may be produced at a much higher rate than consumed and messages get lost, which is not a problem. Imagine a fast sensor and a slow sensor data processor. For that, I use redis pub/sub and wrote a class which acts as a subscriber, receives every message and puts that into a buffer. The buffer is overwritten when a new message comes in or nullified when the message is requested by the "real" function. So when I ask this class, I immediately get a response (hint that my function is slower than data comes in) or I have to wait (hint that my function is faster than the data).

This works pretty good for the case that data comes in fast. But for data which comes in relatively seldom, let's say every five seconds, this does not work: imagine my consumer gets launched slightly after the producer, the first message is lost and my consumer needs to wait nearly five seconds, until it can start working.

I think I have to solve this with Redis tools. Instead of a pub/sub, I could simply use the get/set methods, thus putting the cache functionality into Redis directly. But then, my consumer would have to poll the database instead of the event magic I have at the moment. Keys could look like "key:timestamp", and my consumer now has to get key:* and compare the timestamps permamently, which I think would cause a lot of load. There is no natural possibility to sleep, since although I don't care about dropped messages (there is nothing I can do about), I do care about delay.

Does someone use Redis for a similar thing and could give me a hint about clever use of Redis tools and data structures?

edit

Ideally, my program flow would look like this:

  • start the program
  • retrieve key from Redis
  • tell Redis, "hey, notify me on changes of key".
  • launch something asynchronously, with a callback for new messages.

By writing this, an idea came up: The publisher not only publishes message on topic key, but also set key message. This way, an application could initially get and then subscribe.

Good idea or not really?

What I did after I got the answer below (the accepted one)

Keyspace notifications are really what I need here. Redis acts as the primary source for information, my client subscribes to keyspace notifications, which notify the subscribers about events affecting specific keys. Now, in the asynchronous part of my client, I subscribe to notifications about my key of interest. Those notifications set a key_has_updates flag. When I need the value, I get it from Redis and unset the flag. With an unset flag, I know that there is no new value for that key on the server. Without keyspace notifications, this would have been the part where I needed to poll the server. The advantage is that I can use all sorts of data structures, not only the pub/sub mechanism, and a slow joiner which misses the first event is always able to get the initial value, which with pub/sib would have been lost.

When I need the value, I obtain the value from Redis and set the flag to false.

like image 517
wal-o-mat Avatar asked Aug 05 '14 07:08

wal-o-mat


People also ask

How does Redis work as a message broker?

Redis Streams doubles as a communication channel for building streaming architectures and as a log-like data structure for persisting data, making Streams the perfect solution for event sourcing. Redis Pub/Sub is an extremely lightweight messaging protocol designed for broadcasting live notifications within a system.

Is Redis a good Message Queue?

The high performance of Redis makes it a favorite data store, cache, and even lightweight message broker by using the tools like Redis message queue. According to DB-Engines ranking, Redis is the most popular key-value data store.

Should I use Redis or RabbitMQ?

Redis provides fast and in memory capabilities. So, it is best for short retention of messages where persistence is not important. On the other hand, if there is a requirement for complex routing, you should directly opt for RabbitMQ.

Can Redis replace Kafka?

Conclusion. Redis is used if you want to deliver messages instantly to the consumer and you can live up with data loss, and the amount of data to deal is less. Kafka can be used when you're looking for reliability, high throughput, fault-tolerant, and volume of data is huge.


2 Answers

One idea is to push the data to a list (LPUSH) and trim it (LTRIM), so it doesn't grow forever if there are no consumers. On the other end, the consumer would grab items from that list and process them. You can also use keyspace notifications, and be alerted each time an item is added to that queue.

like image 120
soveran Avatar answered Oct 15 '22 23:10

soveran


From Redis 5, there is new data type called "Streams" which is append-only datastructure. The Redis streams can be used as reliable message queue with both point to point and multicast communication using consumer group concept Redis_Streams_MQ

like image 32
Nagendran Avatar answered Oct 15 '22 23:10

Nagendran