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.
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. |