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.

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

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