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:
key
from Rediskey
".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?
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.
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.
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.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With