Pub/Sub, Streams, and how both differ from Kafka
Pub/Sub forgets instantly, Streams remember until you trim, and neither is a distributed log.
Open this lesson in the learning hubKey points
- Pub/Sub is fire and forget:
PUBLISHdelivers only to clients subscribed right now and returns the count. - A subscriber that is disconnected for two seconds loses every message sent in those two seconds, permanently.
- Streams arrived in Redis 5.0:
XADDappends entries with time-ordered IDs that stay until you trim them. XREADGROUPplusXACKgives at-least-once delivery, and unacknowledged entries sit in the pending list.XAUTOCLAIM, added in 6.2, reassigns entries a crashed consumer never acknowledged to a live one.- Trim on write with
XADD key MAXLEN ~ 10000 *, because an untrimmed stream grows until it fills memory.
Example
# Pub/Sub - nobody listening means nobody gets it
PUBLISH orders.created "991"
(integer) 0 # zero subscribers, message gone
# Streams - stored, grouped, acknowledged
XADD orders MAXLEN ~ 100000 * id 991 total 240
XGROUP CREATE orders billing $ MKSTREAM
XREADGROUP GROUP billing worker-1 COUNT 10 STREAMS orders >
XACK orders billing 1722688200000-0
XPENDING orders billing # what is claimed but not yet acknowledged
Pub/Sub for live notifications you can afford to lose, Streams for work queues, Kafka when the log must outlive memory.
This is a reading copy. The full lesson — with the visual explainer, the interactive lab and a Run button for the code — lives in the Redis Course course, and every lesson in it is listed on the Redis Course contents page.