Redis Pub/Sub and Redis Stream
WEB back-end

Redis Pub/Sub and Redis Stream


Is Redis Stream better than Redis Pub/Sub for my chat service?

That was a question for me when I first read about Redis Stream - a new data type introduced with Redis 5.0. So, I will go into a lot of details on them to get the answer.

The chat service that I built is a small project for researching Golang and Redis. I'm using Redis Pub/Sub to receive messages from senders (publisher) and send them to specific receivers (subscribers). It allows me to create chat rooms easily by creating a channel, then Redis will help me take care of all the work of distributing messages. Pub/Sub has some key properties as below:

  • Pub/Sub is a Publisher/Subscriber platform, it's not data storage and the published messages will be removed from the buffer when they are delivered to subscribers.
  • All the published messages will be delivered to all subscribers. But they need to be active at the same time because Pub/Sub is synchronous communication (push protocol).
  • The published messages will be removed, regardless if there was any subscriber because there is no ACK message.

So, how about the Redis Stream?

The Stream is a new data type introduced with Redis 5.0, which models a log data structure in a more abstract way.

That is the introduction to Redis Streams on the official document. I went through the official document and listed out some key properties as below:

  • The messages are stored in memory and stay there until commanded to be deleted.
  • Redis Streams allow both synchronous and asynchronous communication. the synchronous is like Pub/Sub, but with the ability to resume on disconnection without losing messages.
  • With consumer groups, each message is served to a different consumer with at-least-once delivery (explicit acknowledgment sent by the receiver). So that it is not possible that the same message will be delivered to multiple consumers.

Even though Redis Stream still has more features that I haven't written down here, I think my answer is still using Redis Pub/Sub in my small project because my use cases can be achieved with it. But in the new project with more complex user cases, I will try with Redis Stream.

Thank you for reading and enjoy.