TExcelTAD
Home › Redis › Redis Streams

Redis Streams

A stream is an append-only log of entries. Each entry has a unique, ever-increasing ID and a small set of field-value pairs, like a tiny hash. Streams are Redis's answer to event logs and message brokers such as Kafka: producers append events, and any number of readers can process them, re-read history, or pick up where they left off.

Compared with Difference
A list entries have IDs and fields, are never removed by reading, and can be read by range
Pub/Sub messages are stored, so a reader that was offline does not miss anything
Kafka much simpler, lives inside Redis, limited by memory

Adding entries with XADD

XADD key ID field value [field value ...] appends an entry. In production you pass * as the ID and Redis generates one from the current time in milliseconds plus a sequence number, for example 1760700000000-0. Because generated IDs change on every run, the examples here use explicit IDs like 1-1 and 2-1. They follow the same milliseconds-sequence format, and IDs must always increase.

redis · try it yourself
XADD orders 1-1 order_id 1001 customer ada total 25.50
XADD orders 2-1 order_id 1002 customer linus total 8.00
XADD orders 3-1 order_id 1003 customer grace total 112.75
XLEN orders
XADD orders 2-5 order_id 9999
redis> XADD orders 1-1 order_id 1001 customer ada total 25.50
"1-1"
redis> XADD orders 2-1 order_id 1002 customer linus total 8.00
"2-1"
redis> XADD orders 3-1 order_id 1003 customer grace total 112.75
"3-1"
redis> XLEN orders
(integer) 3
redis> XADD orders 2-5 order_id 9999
(error) ERR The ID specified in XADD is equal or smaller than the target stream top item

XADD returns the ID of the new entry. The last command fails because 2-5 is smaller than the newest ID 3-1: a stream only ever grows forward in time.

Note: When you pass 1-*, Redis keeps your milliseconds part and picks the next sequence number for you. With * it picks both.

Reading a range with XRANGE

XRANGE key start end returns entries between two IDs, inclusive. - and + mean the smallest and largest possible IDs, and COUNT limits the result. XREVRANGE reads backwards, newest first.

redis · try it yourself
XADD orders 1-1 customer ada total 25.50
XADD orders 2-1 customer linus total 8.00
XADD orders 3-1 customer grace total 112.75
XRANGE orders - +
XRANGE orders 2 + COUNT 1
XREVRANGE orders + - COUNT 1
redis> XADD orders 1-1 customer ada total 25.50
"1-1"
redis> XADD orders 2-1 customer linus total 8.00
"2-1"
redis> XADD orders 3-1 customer grace total 112.75
"3-1"
redis> XRANGE orders - +
1) 1) "1-1"
   2) 1) "customer"
      2) "ada"
      3) "total"
      4) "25.50"
2) 1) "2-1"
   2) 1) "customer"
      2) "linus"
      3) "total"
      4) "8.00"
3) 1) "3-1"
   2) 1) "customer"
      2) "grace"
      3) "total"
      4) "112.75"
redis> XRANGE orders 2 + COUNT 1
1) 1) "2-1"
   2) 1) "customer"
      2) "linus"
      3) "total"
      4) "8.00"
redis> XREVRANGE orders + - COUNT 1
1) 1) "3-1"
   2) 1) "customer"
      2) "grace"
      3) "total"
      4) "112.75"

Each entry comes back as its ID followed by the list of fields and values. An ID without a sequence part, like 2, means 2-0 as a start bound.

Paging through a stream

To read a long stream in pages, start with -, then continue from the last ID you received. The ( prefix makes the start exclusive, so the last entry is not returned twice:

redis · try it yourself
XADD clicks 1-1 page /home
XADD clicks 2-1 page /pricing
XADD clicks 3-1 page /signup
XADD clicks 4-1 page /home
XRANGE clicks - + COUNT 2
XRANGE clicks (2-1 + COUNT 2
XRANGE clicks (4-1 + COUNT 2
redis> XADD clicks 1-1 page /home
"1-1"
redis> XADD clicks 2-1 page /pricing
"2-1"
redis> XADD clicks 3-1 page /signup
"3-1"
redis> XADD clicks 4-1 page /home
"4-1"
redis> XRANGE clicks - + COUNT 2
1) 1) "1-1"
   2) 1) "page"
      2) "/home"
2) 1) "2-1"
   2) 1) "page"
      2) "/pricing"
redis> XRANGE clicks (2-1 + COUNT 2
1) 1) "3-1"
   2) 1) "page"
      2) "/signup"
2) 1) "4-1"
   2) 1) "page"
      2) "/home"
redis> XRANGE clicks (4-1 + COUNT 2
(empty array)

XREAD: reading what is new

XREAD is made for consumers that follow a stream. You give the last ID you have already processed, and it returns entries after it. It can read several streams at once:

redis · try it yourself
XADD sensor:1 1-1 temp 21.5
XADD sensor:1 2-1 temp 21.9
XADD sensor:2 1-1 temp 18.0
XREAD COUNT 10 STREAMS sensor:1 sensor:2 0 0
XREAD STREAMS sensor:1 1-1
XREAD STREAMS sensor:1 2-1
redis> XADD sensor:1 1-1 temp 21.5
"1-1"
redis> XADD sensor:1 2-1 temp 21.9
"2-1"
redis> XADD sensor:2 1-1 temp 18.0
"1-1"
redis> XREAD COUNT 10 STREAMS sensor:1 sensor:2 0 0
1) 1) "sensor:1"
   2) 1) 1) "1-1"
         2) 1) "temp"
            2) "21.5"
      2) 1) "2-1"
         2) 1) "temp"
            2) "21.9"
2) 1) "sensor:2"
   2) 1) 1) "1-1"
         2) 1) "temp"
            2) "18.0"
redis> XREAD STREAMS sensor:1 1-1
1) 1) "sensor:1"
   2) 1) 1) "2-1"
         2) 1) "temp"
            2) "21.9"
redis> XREAD STREAMS sensor:1 2-1
(nil)

Reading from 0 returns everything. When there is nothing newer, XREAD replies (nil). A real consumer adds BLOCK milliseconds and passes $ ("only entries added from now on") on the first call, then waits for new events without polling:

text
redis> XREAD BLOCK 5000 STREAMS sensor:1 $
(nil)
(5.00s)

Trimming and deleting

Streams grow forever unless you trim them. XTRIM keeps the newest entries (MAXLEN) or drops entries older than an ID (MINID). The same options work on XADD, so every append can also cap the stream. XDEL removes single entries by ID.

redis · try it yourself
XADD log 1-1 level info msg boot
XADD log 2-1 level warn msg disk-80%
XADD log 3-1 level info msg login
XADD log 4-1 level error msg disk-full
XTRIM log MAXLEN 3
XDEL log 3-1
XRANGE log - +
XADD log MAXLEN 2 5-1 level info msg cleanup
XRANGE log - +
redis> XADD log 1-1 level info msg boot
"1-1"
redis> XADD log 2-1 level warn msg disk-80%
"2-1"
redis> XADD log 3-1 level info msg login
"3-1"
redis> XADD log 4-1 level error msg disk-full
"4-1"
redis> XTRIM log MAXLEN 3
(integer) 1
redis> XDEL log 3-1
(integer) 1
redis> XRANGE log - +
1) 1) "2-1"
   2) 1) "level"
      2) "warn"
      3) "msg"
      4) "disk-80%"
2) 1) "4-1"
   2) 1) "level"
      2) "error"
      3) "msg"
      4) "disk-full"
redis> XADD log MAXLEN 2 5-1 level info msg cleanup
"5-1"
redis> XRANGE log - +
1) 1) "4-1"
   2) 1) "level"
      2) "error"
      3) "msg"
      4) "disk-full"
2) 1) "5-1"
   2) 1) "level"
      2) "info"
      3) "msg"
      4) "cleanup"

Tip: Exact trimming (MAXLEN 1000) costs a little more than approximate trimming (MAXLEN ~ 1000), which lets Redis remove whole internal nodes at once and may keep a few extra entries. Use ~ for high-volume streams.

Common uses

  • Event sourcing and audit logs: every change to an order is an entry; the current state can be rebuilt by replaying them.
  • Activity feeds and change data capture: services read the stream and update caches or search indexes.
  • Sensor and metrics data: one stream per device, trimmed by time with MINID.
  • Work queues with many workers: covered in the next lesson on consumer groups.

Test yourself

What does * mean as the ID in XADD events * type click?

  1. Add to every stream
  2. Let Redis generate the next ID from the current time
  3. Replace all entries
  4. Use ID 0-0

Why does XADD s 5-1 a 1 fail if the last entry has ID 7-1?

  1. IDs must be strings
  2. New IDs must be greater than the last ID
  3. Fields must be unique
  4. The stream is full

You processed everything up to ID 12-3. Which call returns only newer entries?

  1. XRANGE s - 12-3
  2. XREAD STREAMS s 12-3
  3. XREVRANGE s 12-3 -
  4. XREAD STREAMS s 0