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

codecompleting picture codecompleting · Oct 5, 2011 · Viewed 10.8k times · Source

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.

Answer

antirez picture antirez · Oct 6, 2011

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