Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does the redis pub/sub model require persistent connections to redis?

In a web application, if I need to write an event to a queue, I would make a connection to redis to write the event.

Now if I want another backend process (say a daemon or cron job) to process the or react the the publishing of the event in redis, do I need a persistant connection?

Little confused on how this pub/sub process works in a web application.

like image 909
codecompleting Avatar asked Oct 05 '11 14:10

codecompleting


People also ask

Is Redis Pub/Sub persistent?

No - Redis' Pub/Sub has no persistence, and once a message has been published, it is sent only to the connected subscribed clients.

How does Redis implement Pub Sub?

Redis Pub/Sub implements the messaging system where the senders (in redis terminology called publishers) sends the messages while the receivers (subscribers) receive them. The link by which the messages are transferred is called channel. In Redis, a client can subscribe any number of channels.

Is Redis good for pub sub?

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. These messages are fire-and-forget, in that if a message is published and no subscribers exists, the message evaporates and cannot be recovered.

What is Pub/Sub in Redis?

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

Basically in Redis there are two different messaging models:

  • Fire and Forget / One to Many: Pub/Sub. At the time a message is PUBLISH-ed all the subscribers will receive it, but this message is then lost forever. If a client was not subscribed there is no way it can get it back.
  • Persisting Queues / One to One: Lists, possibly used with blocking commands such as BLPOP. With lists you have a producer pushing into a list, and one or many consumers waiting for elements, but one message will reach only one of the waiting clients. With lists you have persistence, and messages will wait for a client to pop them instead of disappearing. So even if no one is listening there is a backlog (as big as your available memory, or you can limit the backlog using LTRIM).

I hope this is clear. I suggest you studying the following commands to understand more about Redis and messaging semantics:

  • LPUSH/RPUSH, RPOP/LPOP, BRPOP/BLPOP
  • PUBLISH, SUBSCRIBE, PSUBSCRIBE

Doc for this commands is available at redis.io

like image 97
antirez Avatar answered Sep 17 '22 12:09

antirez