Search code examples
redispublish-subscribe

Redis, will a topic (pub/sub) always be delivered to at least one subscriber?


If for instance an expiry event on a key / val occurs and a topic is sent out but all clients are down, will even in one years time that topic be delivered when one client comes online and subscribes to that topic?

What if two clients go online later around the same time?

What if two clients go online but with a huge delay in between them?

The topics are broadcasted later on, but I am noticing that if I am down and reconnect I can get the topics delivered when I go back online.

When I restart, I do not get the old one despite it being "a new client".

What is going on? What are the internal rules for this stuff?


Solution

  • Pub/Sub is synchronous communication. All parties need to be active at the same time to be able to communicate. Here Redis is a pure synchronous messaging broker.

    The answer to your first three questions is no. There is no persistence of the messages, not in disk, not in memory. When a message is published, it is sent to the client connections subscribed at the moment. The PUBLISH command will return the number of clients that received the message, immediately: O(N+M) where N is the number of clients subscribed to the receiving channel and M is the total number of subscribed patterns (by any client).

    ... but I am noticing that if I am down and reconnect I can get the topics delivered when I go back online

    R/ I guess it depends on what you mean with "I am down". The message must have being cached somewhere in your client. Or maybe the client connection in Redis Server was still alive and the message was there in the Client Output Buffer.

    You may find this resources useful:

    What are the main differences between Redis Pub/Sub and Redis Stream?

    Pub/Sub