21

I haven't been able to find in the documentation on how the messages in a channel get stored in redis publish/subscribe.

When you publish to a redis channel, is that message stored or persisted? If so, how long is it stored and how do you get historical messages?

Otherwise, I'm assuming that it just broadcasts that message and drops/deletes that message after doing so?

MonkeyBonkey
  • 46,433
  • 78
  • 254
  • 460
  • 1
    Actually https://redislabs.com/redis-best-practices/communication-patterns/pub-sub/ said clearly "These messages are fire-and-forget, in that if a message is published and no subscribers exists, the message evaporates and cannot be recovered." – Qiulang Nov 03 '19 at 09:11

2 Answers2

35

The pub/sub messages are not queued, and even less persisted.

They are only buffered in the socket buffers, and immediately sent to the subscribers in the same event loop iteration as the publication.

If a subscriber fails to read a message, this message is lost for the subscriber.

Didier Spezia
  • 70,911
  • 12
  • 189
  • 154
  • 1
    any idea on how to save these messages ? i am trying to implement chat like feature – Rahul Dess Mar 18 '15 at 03:41
  • 3
    You could store them in lists. Use LPUSH to store an item, and LPOP or BLPOP to dequeue it. You have to implement the pub/sub behavior yourself. – Didier Spezia Mar 18 '15 at 19:59
  • 2
    @RahulDess Now you can use [Redis Keyspace Notifications](https://redis.io/topics/notifications) – InQβ Jan 30 '19 at 02:07
  • @DidierSpezia if this is the case, why do I see high output buffer size in pubsub clients? – Michael Jul 15 '19 at 18:11
2

You can play with Redis Streams that were released sine 5.0 version. They support persistency and can fit your needs. More details you can find from the article that compares these similar but different data types.

Stepan Tsybulski
  • 1,121
  • 8
  • 18