[!NOTE] This module explores the core principles of Module Review: Streams & PubSub, deriving solutions from first principles and hardware constraints to build world-class, production-ready expertise.

1. Key Takeaways

  • Streams are Persistent: Unlike Pub/Sub, Redis Streams store data on disk (AOF/RDB) and persist across restarts.
  • Consumer Groups Scale: Use Consumer Groups (XGROUP) to distribute message processing across multiple workers.
  • PEL Ensures Reliability: The Pending Entries List tracks unacknowledged messages, ensuring “at-least-once” delivery.
  • Pub/Sub is Fast: Use Pub/Sub for fire-and-forget, real-time notifications where data loss is acceptable.
  • Event Sourcing: Streams act as a perfect immutable log for Event Sourcing architectures.

2. Flashcards

What is the main difference between Pub/Sub and Streams?
Persistence. Pub/Sub is fire-and-forget (messages are lost if no one listens), while Streams persist messages to disk like a log.
What does the `>` symbol mean in `XREADGROUP`?
It tells Redis to return only **new** messages that have never been delivered to any other consumer in the group.
What is the Pending Entries List (PEL)?
A list that tracks messages delivered to a consumer but not yet acknowledged (`XACK`). It is used for fault tolerance.
How does Redis efficiently store Stream IDs?
It uses a **Radix Tree (Rax)**, a compressed prefix tree that saves memory by sharing common prefixes of timestamps.
What command is used to take over a message from a crashed consumer?
`XCLAIM`. It changes the ownership of a pending message to the claiming consumer.

3. Cheat Sheet

Command Description Example
XADD Append a message to a stream XADD mystream * key value
XRANGE Read a range of messages XRANGE mystream - +
XGROUP CREATE Create a consumer group XGROUP CREATE mystream mygroup $
XREADGROUP Read as a consumer group XREADGROUP GROUP mygroup Alice STREAMS mystream >
XACK Acknowledge a message XACK mystream mygroup 1623...-0
XPENDING Inspect pending messages XPENDING mystream mygroup
XCLAIM Steal a pending message XCLAIM mystream mygroup Bob 1000 1623...-0
PUBLISH Broadcast a message PUBLISH channel msg
SUBSCRIBE Listen to a channel SUBSCRIBE channel

4. Glossary

Unsure about a term? Check out the Redis Glossary.