Redis / Basic: keys and bounded data structures / Lesson 7
Publish and subscribe to transient messages
Use a synthetic channel and explain the delivery limits of Redis Pub/Sub.
On this page
Lesson goals
- Explain publisher, subscriber, and channel roles.
- Run a two-client thought experiment in an isolated lab.
- Distinguish transient delivery from a replayable workflow.
Define what a notification means
Pub/Sub sends a message to clients subscribed to a channel when it is published. The publisher does not address each subscriber individually. Redis Pub/Sub has at-most-once delivery: if a subscriber is disconnected or cannot process a message, Redis does not replay that message for it. Choose this model for a transient notification only when the application can tolerate missed messages or recover state from an authoritative source.
Subscribe in a disposable lab
Use two separate clients connected to the same isolated Redis lab and a synthetic channel. Start the subscriber first and wait for its subscription acknowledgement before publishing. The example does not connect to a configured service. Client libraries manage subscription connections and callback behavior differently, so consult the driver contract rather than assuming that an ordinary command connection behaves identically.
SUBSCRIBE lab:announcements
Client A expresses interest in lab:announcements. The actual acknowledgement and connection behavior depend on the client and protocol. No subscriber output is asserted here.
Publish from a second client
After the subscription is active, client B publishes a synthetic notification. A connected subscriber may receive it, but the publication result and timing depend on the active topology and clients. The returned delivery count is not proof that a subscriber completed a business action. Keep the channel name and payload free of private information when using a shared training environment.
PUBLISH lab:announcements "catalog refreshed"
The payload announces a catalog refresh to current subscribers. The example does not claim a measured count or output; verify the subscriber’s actual receipt in the isolated lab.
Test the disconnected case
Disconnect the subscriber in the lab, publish another message, and reconnect it. Under Pub/Sub’s at-most-once contract the disconnected subscriber cannot replay the missed publication from the channel. If every event must be processed, design a persistent workflow such as a stream with an explicit acknowledgement and recovery policy. A notification can instead prompt a refresh from authoritative state when missing one is acceptable.
Verify the application boundary
Test subscriber startup, reconnection, missed messages, and duplicate application actions under the chosen client implementation. Record how a subscriber rebuilds state after an outage and how it shuts down cleanly. Restrict channel access according to the deployment’s ACL policy and avoid embedding secrets in payloads. Pub/Sub delivery is a transport event, not an acknowledgement that a downstream database or external service committed a change.
Common errors and fixes
A subscriber misses a notification during an outage.
- Why it happens
- Pub/Sub does not retain the publication for an offline subscriber.
- How to fix it
- Refresh authoritative state or use a replayable workflow when required.
- Prevent it next time
- Test disconnected subscribers and document delivery requirements.
The publisher assumes a business action completed.
- Why it happens
- A delivery count was treated as an application acknowledgement.
- How to fix it
- Use a separate acknowledged workflow when completion must be confirmed.
- Prevent it next time
- Define transport and business completion separately.
A subscriber receives nothing in the lab.
- Why it happens
- Publication occurred before the subscription was active or on another endpoint.
- How to fix it
- Confirm the target and subscription acknowledgement before publishing.
- Prevent it next time
- Test startup ordering and connection configuration.
2 practice exercises
1. What happens to a message published while the subscriber is disconnected?
Show worked solution
That subscriber cannot replay the missed Pub/Sub message after reconnecting. It must recover state separately or use a durable mechanism designed for replay.
2. Does the PUBLISH response prove that an invoice was processed?
Show worked solution
No. It reports delivery information for the publication, not a business acknowledgement or a committed invoice. Design a separate confirmation and recovery contract if required.
Key takeaways
- Pub/Sub broadcasts to current subscribers and is at-most-once.
- A disconnected subscriber cannot replay missed messages from the channel.
- Define recovery and business acknowledgement separately.