Skip to content

Redis Data Type : Streams

A Redis stream presents itself as a versatile data structure resembling an append-only log but with added functionalities to surpass the constraints of a conventional append-only log. These enhancements include efficient random access in O(1) time and the facilitation of intricate consumption strategies like consumer groups. By leveraging Redis streams, you can seamlessly capture and disseminate events in real time. Various scenarios illustrate the utility of Redis streams:

  1. Event Sourcing: Utilize Redis streams to track diverse user actions such as clicks, interactions, and other events in real-time, facilitating robust event sourcing mechanisms.
  2. Sensor Monitoring: Employ Redis streams to monitor and log sensor data from devices deployed in various environments, enabling real-time tracking and analysis of readings.
  3. Notifications: Create dedicated streams within Redis to store personalized notifications for each user, ensuring efficient retrieval and management of notification history.

Each entry within a Redis stream is accompanied by a unique identifier generated by Redis. These identifiers enable seamless retrieval of associated entries or continuous processing of subsequent stream entries. It’s important to note that since these IDs are time-based, they may vary, and the ones illustrated here might differ from those observed in individual Redis instances.

Create a Stream

  • You can create a Stream by using XADD.
  • The key of the Stream is “HRSteam”. The message contains age, age, salary and experience.
127.0.0.1:6379> XADD  HRStream * name Ivy age 30 salary 80000  experience 7
"1712321863491-0"

127.0.0.1:6379> XADD  HRStream * name Tom age 35 salary 80000  experience 8
"1712321989133-0"

Read Messages from a Stream

  • You can read a Stream by using XREAD.
  • The “2” indicates two messages and the “0” indicates that it starts reading from the beginning of the stream.
127.0.0.1:6379> XREAD COUNT 2 STREAMS HRStream 0
1) 1) "HRStream"
   2) 1) 1) "1712321863491-0"
         2) 1) "name"
            2) "Ivy"
            3) "age"
            4) "30"
            5) "salary"
            6) "80000"
            7) "experience"
            8) "7"
      2) 1) "1712321989133-0"
         2) 1) "name"
            2) "Tom"
            3) "age"
            4) "35"
            5) "salary"
            6) "80000"
            7) "experience"
            8) "8"

Consume Messages from a Stream (Blocking)


Consuming messages from a stream in a blocking manner involves the client waiting for new messages to appear in the stream before proceeding with a response. This approach is particularly valuable for real-time applications where immediate processing of incoming messages is essential, eliminating the need for constant polling of the stream.

For example, the command blocks for up to 3 seconds (BLOCK 3000 specifies the timeout in milliseconds) waiting for new messages to arrive in the stream HRStream. The "$" indicates reading from the latest message.

The response is “nil” because there is not new message in the last 3 seconds.

127.0.0.1:6379> XREAD BLOCK 3000 STREAMS HRStream $
(nil)
(3.00s)

Consume Messages with Consumer Groups

You can create a consumer group for a specific stream by using XGROUP CREATE.

Once you have created a consumer group, consumers can start reading messages from the stream within that group. Consumers are identified by unique names within a group. You can use the XREADGROUP command to read messages.

  • GROUP msggroup consumer1: This specifies that the consumer named consumer1 belongs to the consumer group msggroup.
  • COUNT 1: This indicates that the consumer wants to read one message at a time.
  • STREAMS HRStream >: This specifies the stream HRStream and the > indicates that the consumer wants to read messages starting from the last message they received.
127.0.0.1:6379> XGROUP CREATE HRStream msggroup $
OK

127.0.0.1:6379> XREADGROUP GROUP msggroup consumer1 BLOCK 5000 COUNT 1 STREAMS HRStream >
(nil)
(5.02s)

In this example, we add a new message and receive the message by using consumer group.

127.0.0.1:6379> XADD  HRStream * name Jane age 30 salary 80000  experience 7
"1712322179615-0"
127.0.0.1:6379> XREADGROUP GROUP msggroup consumer1 BLOCK 5000 COUNT 1 STREAMS HRStream >
1) 1) "HRStream"
   2) 1) 1) "1712322179615-0"
         2) 1) "name"
            2) "Jane"
            3) "age"
            4) "30"
            5) "salary"
            6) "80000"
            7) "experience"
            8) "7"

Check Stream Information

This command provides information about the stream HRStream including the number of messages, the first and last message IDs, and other details.

127.0.0.1:6379> XINFO STREAM HRStream
 1) "length"
 2) (integer) 5
 3) "radix-tree-keys"
 4) (integer) 1
 5) "radix-tree-nodes"
 6) (integer) 2
 7) "last-generated-id"
 8) "1712322489150-0"
 9) "max-deleted-entry-id"
10) "0-0"
11) "entries-added"
12) (integer) 5
13) "recorded-first-entry-id"
14) "1712321863491-0"
15) "groups"
16) (integer) 1
17) "first-entry"
18) 1) "1712321863491-0"
    2) 1) "name"
       2) "Ivy"
       3) "age"
       4) "30"
       5) "salary"
       6) "80000"
       7) "experience"
       8) "7"
19) "last-entry"
20) 1) "1712322489150-0"
    2) 1) "name"
       2) "James"
       3) "age"
       4) "40"
       5) "salary"
       6) "100000"
Tags: