Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redis pubsub vs blocking operations

How should I choose between pubsub and blocking operations of redis ?

Redis gives blocking operations like BLPOP which blocks the operation till an element can be popped from the list. Why should I not use this to achieve the functionality of PUBSUB. PUBSUB allows you to define channels which are a higher level construct than the basic lists. If my usecase is simple without multiple channels, can I go with the basic blocking operations.

like image 738
sunil Avatar asked Apr 26 '14 07:04

sunil


People also ask

Is Redis pub/sub blocking?

Redis' pub/sub sends messages to clients subscribed (listening) on a channel. If you are not listening, you will miss the message (hence the blocking call). If you want to have it non-blocking, I recommend using a queue instead (redis is pretty good at that too).

Is Redis good for Pubsub?

Redis Best Practices Aside from data storage, Redis can be used as a Publisher/Subscriber platform. In this pattern, publishers can issue messages to any number of subscribers on a channel.

Is Redis pub/sub fast?

Redis Pub/Sub is designed for speed (low latency), but only with low numbers of subscribers —subscribers don't poll and while subscribed/connected are able to receive push notifications very quickly from the Redis broker—in the low ms, even < 1ms as confirmed by this benchmark.

What is Redis Pubsub?

Redis Pub/Sub is the oldest style of messaging pattern supported by Redis and uses a data type called a “channel,” which supports typical pub/sub operations, such as publish and subscribe. It's considered loosely coupled because publishers and subscribers don't know about each other.


1 Answers

There is an important difference between using lists with blocking operations and the pub/sub facilities.

A list with blocking operations can easily be used as a queue, while pub/sub channels do not involve any queuing. The only buffers involved in pub/sub are related to communication (i.e. socket management). It means when a message is published, it will be transmitted ASAP to the subscribers, it is never kept in Redis. A consequence is if the subscribers do not listen anymore to the Redis socket, the items are lost for these subscribers.

Another important difference is the pub/sub mechanism can multicast items. When the items are published they are sent to all the subscribers. On the contrary, considering multiple daemons dequeuing a list in Redis using blocking operations, pushing an item to the list will result in the item to be dequeued by one and only one daemon.

Blocking lists (i.e. queues) and pub/sub channels are really complementary facilities.

If you do not need to multicast items, you should rather use list with blocking operations, since they are much more reliable.

like image 69
Didier Spezia Avatar answered Oct 03 '22 00:10

Didier Spezia