Redis/Pub/Sub lab

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.

Break modePUBLISH sends only to subscribers connected at that moment.
Fix modeXADD stores messages so a consumer can reconnect and catch up.
InvariantIf every message matters, offline consumers must not lose messages.
fan out
offline
stored
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

PatternUse whenAvoid when
Pub/SubLive 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.
StreamsConsumers 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

Code Pointers

CodeWhy it matters
cmd/pubsub/main.goThe offline subscriber case and the streams comparison path.
pubsub/MakefileTargets for fire-and-forget breakage and stream-backed recovery.
pubsub/compose.yamlRedis container used by the lab.
pubsub/README.mdExpected output for message loss and stream catch-up.
Cache Rate limit Locks Streams