Pub/Sub Lab: Fire-And-Forget vs Streams
This lab compares Redis Pub/Sub with Redis Streams by taking one subscriber offline in the middle of a message run.
PUBLISH sends only to subscribers connected at that moment.XADD stores messages so a consumer can reconnect and catch up.Redis Commands
SUBSCRIBE channel
Opens a live subscription. The client receives future messages while connected.
PUBLISH channel payload
Sends a payload to currently connected subscribers. Return value is subscriber count, not durable delivery proof.
XGROUP CREATE ... MKSTREAM
Creates a stream consumer group for the durable comparison path.
XADD stream * field value
Stores a message in a stream so it can be read later.
XREADGROUP
Reads stored stream messages for a consumer group.
XACK
Acknowledges that a stream message has been processed.
When To Use Each
| Pattern | Use when | Avoid when |
|---|---|---|
| Pub/Sub | Live fan-out where missed messages are acceptable, such as local invalidation signals or ephemeral notifications. | Jobs, payments, orders, audit events, or anything that must be replayed. |
| Streams | Consumers need backlog, ack, retry, or crash recovery. | You only need a transient signal and do not want to manage retention. |
Run Targets
The runnable lab is labs/redis/pubsub, backed by labs/redis/cmd/pubsub.
make break
Pub/Sub mode. Look for SUB-B going offline and missing messages permanently.
make test
Streams mode. Look for the consumer catching up after reconnect.
make load
Runs the streams comparison with more messages.
Lab Evidence: real reconnect (2026-10-31)
Real output of make test (the streams mode). Make banner text, ANSI colour codes, build output, and blank lines are removed; values are unedited.
docker compose run --rm driver -mode=streams -messages=30 -check
═══════════════════════════════════════════════════════════
REDIS PUB/SUB LAB — FIRE-AND-FORGET vs STREAMS
mode=streams messages=30 channel=events
═══════════════════════════════════════════════════════════
[ 0ms] OFFLINE consumer connection closed; processed=10
[ 0ms] ONLINE new consumer connection; catching up 10 stored messages
═══════════════════════ RESULTS ════════════════════════
messages produced : 30
consumer-A processed: 30 (including 10 'missed' while offline)
remaining in PEL : 0
══════════════════ INVARIANT CHECK ═════════════════════
processed=30 expected=30
✓ PASSED all 30 messages processed after 'reconnect'
streams buffer messages — offline consumers catch up on reconnect
contrast: Pub/Sub (make break) loses those same messages permanently
The consumer client is closed, ten entries are produced while it is absent, and a new connection catches up. This tests consumer continuity, not Redis server-crash durability.
Production Notes
- Pub/Sub is fire-and-forget. It does not keep messages for disconnected clients.
- A successful
PUBLISHdoes not mean every logical consumer processed the message. - Streams are stronger, but they require trimming, consumer-group monitoring, and idempotent handlers.
Code Pointers
| Code | Why it matters |
|---|---|
cmd/pubsub/main.go | The offline subscriber case and the streams comparison path. |
pubsub/Makefile | Targets for fire-and-forget breakage and stream-backed recovery. |
pubsub/compose.yaml | Redis container used by the lab. |
pubsub/README.md | Expected output for message loss and stream catch-up. |