Redis publish-subscribe: Is Redis guaranteed to deliver the message even under massive stress?

前端 未结 2 1292
臣服心动
臣服心动 2020-12-13 15:50

Provided that both the client subscribed and the server publishing the message retain the connection, is Redis guaranteed to always deliver the published message to the subs

相关标签:
2条回答
  • 2020-12-13 16:14

    Redis does absolutely not provide any guaranteed delivery for the publish-and-subscribe traffic. This mechanism is only based on sockets and event loops, there is no queue involved (even in memory). If a subscriber is not listening while a publication occurs, the event will be lost for this subscriber.

    It is possible to implement some guaranteed delivery mechanisms on top of Redis, but not with the publish-and-subscribe API. The list data type in Redis can be used as a queue, and as the the foundation of more advanced queuing systems, but it does not provide multicast capabilities (so no publish-and-subscribe).

    AFAIK, there is no obvious way to easily implement publish-and-subscribe and guaranteed delivery at the same time with Redis.

    0 讨论(0)
  • 2020-12-13 16:30

    Redis does not provide guaranteed delivery using its Pub/Sub mechanism. Moreover, if a subscriber is not actively listening on a channel, it will not receive messages that would have been published.

    I previously wrote a detailed article that describes how one can use Redis lists in combination with BLPOP to implement reliable multicast pub/sub delivery:

    http://blog.radiant3.ca/2013/01/03/reliable-delivery-message-queues-with-redis/

    For the record, here's the high-level strategy:

    • When each consumer starts up and gets ready to consume messages, it registers by adding itself to a Set representing all consumers registered on a queue.
    • When a producers publishes a message on a queue, it:
      • Saves the content of the message in a Redis key
      • Iterates over the set of consumers registered on the queue, and pushes the message ID in a List for each of the registered consumers
    • Each consumer continuously looks out for a new entry in its consumer-specific list and when one comes in, removes the entry (using a BLPOP operation), handles the message and moves on to the next message.

    I have also made a Java implementation of these principles available open-source: https://github.com/davidmarquis/redisq

    These principles have been used to process about 1,000 messages per second from a single Redis instance and two instances of the consumer application, each instance consuming messages with 5 threads.

    0 讨论(0)
提交回复
热议问题