Reliable Messaging with Streams

Learn how to use Redis streams to provide persistent message storage and real-time capabilities to a chat application.

A possible alternative to message queues are streams. The two paradigms are similar in scope, but fundamentally different in their approach to messaging. In this section, we’re going to unveil the power of streams by leveraging Redis Streams to implement our chat application.

Characteristics of a streaming platform

In the context of system integration, a stream (or log) is an ordered, append-only, durable data structure. Messages—which in the context of streams would be more appropriately called records—are always added at the end of the stream and, unlike queues, they’re not automatically deleted once they’re consumed. Essentially, this characteristic makes a stream more similar to a datastore than to a message broker. And like a datastore, a stream can be queried to retrieve a batch of past records or replayed starting from a specific record.

Another important characteristic of streams is that records are pulled by the consumer from the stream. This intrinsically allows the consumer to process the records at its own pace without risking being overwhelmed.

Based on these features, a stream allows us to implement reliable message delivery out of the box, since no data is ever lost from the stream (even though data can still be removed explicitly or can be deleted after an optional retention period). In fact, as the illustration below shows, if a consumer crashes, all it has to do is start reading the stream from where it left off.

Get hands-on with 1400+ tech skills courses.