codecompleting
codecompleting

Reputation: 9611

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.

Upvotes: 19

Views: 12079

Answers (2)

rm-rf
rm-rf

Reputation: 1333

I'm not totally sure, but I believe that yes, pub/sub requires a persistent connection.

For an alternative I would take a peek at resque and how it handles that. Instead of using pub/sub it simply adds an item to a list in redis, and then whatever daemon or cron job you have can use the lpop command to get the first one.

Sorry for only giving a pseudo answer and then a plug.

Upvotes: 2

antirez
antirez

Reputation: 18504

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

Upvotes: 47

Related Questions