Pub/sub and Streams

Lightweight messaging on Redis: fire-and-forget pub/sub, Streams with consumer groups, and the point at which the bus should be Kafka instead.

Updated CompTech Team

Redis can carry messages. It can do it in two ways that get mixed up, and only one of them remembers a message that nobody has read yet. Pub/sub delivers to whoever is subscribed at that moment. Streams append to a log inside a key, and a consumer group can read at its own pace. Neither one is a substitute for Kafka when the message is the business event. The Kafka labs are that bus. This lab draws the line and runs both Redis mechanisms on the cache instance from the earlier labs.

Pub/sub

A subscriber receives messages published after it subscribed, on the connection that is waiting. If the connection is down, the message is gone. It is not in the RDB, it is not in the AOF, and a second subscriber does not get a backlog. Use it for signals where a miss is harmless: "drop your local cache of the catalog", "the config changed, reload".

Open a subscriber and leave it running:

oc exec -it -n cache cache-0 -- redis-cli SUBSCRIBE cache:invalidate

In another shell:

oc exec -n cache cache-0 -- redis-cli PUBLISH cache:invalidate catalog:product:42

The first shell prints the channel and the payload. Stop the subscriber, publish again, start the subscriber. The second publish is not waiting for it. That is the whole contract.

PSUBSCRIBE cache:* listens to a pattern. Patterns are convenient and they are a reason to be careful with channel names, because a loose pattern receives everything. Name channels the way you name keys: cache:invalidate, not events.

Pub/sub is not sharded by the key. On Redis Cluster a publish still has to reach subscribers that may sit on other nodes. It works. It is the wrong tool once the traffic looks like a pipeline.

Streams

A stream is a key. XADD appends an entry. The entry stays until you trim the stream. Consumers can be offline. This is the Redis mechanism that deserves the word "messaging", inside one application.

oc exec -n cache cache-0 -- redis-cli XADD orders:placed '*' id 1001 sku 42
oc exec -n cache cache-0 -- redis-cli XRANGE orders:placed - +
oc exec -n cache cache-0 -- redis-cli XGROUP CREATE orders:placed workers '$' MKSTREAM
oc exec -n cache cache-0 -- redis-cli XREADGROUP GROUP workers c1 COUNT 10 BLOCK 2000 STREAMS orders:placed '>'

'$' as the group start means "only entries that arrive after the group is created". Use '0' when the group should read the backlog. BLOCK 2000 waits up to two seconds. '>' means "entries this consumer has not been delivered yet".

The reply includes an id such as 1710000000000-0. Until you acknowledge it, the entry sits in the pending entries list:

oc exec -n cache cache-0 -- redis-cli XACK orders:placed workers 1710000000000-0
oc exec -n cache cache-0 -- redis-cli XPENDING orders:placed workers

Replace the id with the one XREADGROUP printed. XPENDING goes to zero after the ack. A consumer that crashed after delivery and before XACK leaves the entry pending. Another worker claims it:

oc exec -n cache cache-0 -- redis-cli XAUTOCLAIM orders:placed workers c2 60000 0-0 COUNT 10

60000 is the idle time in milliseconds. Claim is how you get at-least-once. It is also how you get a double delivery. The handler has to tolerate seeing the same id 1001 twice: an upsert, not a second insert. Exactly-once is a property you build in the consumer, which the Kafka labs discuss for payments.

Trim, or the key grows until the memory alert:

oc exec -n cache cache-0 -- redis-cli XADD orders:placed MAXLEN '~' 10000 '*' id 1002 sku 43

'~' lets Redis trim a little past 10,000 for efficiency. Pick the length from how long a consumer may be down, not from a hope that someone will always be reading.

What each one survives

Pub/sub Streams Kafka
Subscriber was offline Message dropped Entry waits in the key Record waits in the partition, for the retention period
Process restarts Nothing to reload The stream is there if persistence or a replica had it The log is the point of the system
Two independent teams consume Both must be connected now Two consumer groups on one stream Two groups, retained for days, replayable next month
Payload is the legal record of a payment No No Yes, if you designed it that way
Effort One command A group, acks, a trim A cluster, a schema, clients

The persistence lab applies to the stream key and not to pub/sub. If Redis has no AOF and no replica, a stream dies with the process like any other key.

When the answer is Kafka

Move the flow to Kafka when any of these are true:

  • A consumer you have not written yet will need yesterday's events.
  • More than one application owns a subscription, and they deploy on different days.
  • You need a schema that can evolve (Avro or Protobuf in a registry) rather than a string both sides hope matches.
  • The volume would compete with the cache for the same memory. A stream of every click will evict the sessions if they share a Redis.
  • You already run Streams for Apache Kafka on OpenShift. A second bus inside Redis is a second place for an incident.

Keep Redis when the producer and the consumer are the same application, the backlog is measured in minutes, and losing the key means "recompute", not "lose the payment". Cache invalidation stays on pub/sub. A short work queue inside one service can stay on a stream. Order events that billing, fraud and the warehouse all read belong on Kafka, which is also where Debezium lands change data capture.

On Redis Cluster, a stream is one key and lives on one shard. A busy stream is a hot shard. Hash tags do not split a single stream. If you are designing around that limit, you have outgrown the mechanism.

What's next

Vector search is the other Redis 8 feature this series uses, for RAG rather than for messaging. Security and monitoring closes the port. The durability of the stream key is Persistence and backup.