Redis › Pub/Sub

How would you design a real time notification system using Pub/Sub while working around its lack of guaranteed message delivery?

Advanced

Answer

You would use Pub/Sub for the fast, real time delivery portion of the system, but also persist important notifications to a database or a Redis Stream as the source of truth, so if a client happens to be disconnected when a Pub/Sub message is sent, it can still retrieve any missed notifications by querying the persistent storage once it reconnects.
-- Publish for real time delivery
PUBLISH notifications:user123 'New message'
-- Also persist for reliability
XADD notifications_log '*' user 'user123' message 'New message'
Real-world example A messaging application delivers new message notifications instantly through Pub/Sub for currently connected users, while also logging every notification to a Redis Stream so a user who was briefly offline can catch up on anything they missed upon reconnecting.

Common follow-ups: How do you efficiently let a reconnecting client catch up on missed messages?;What is the tradeoff of maintaining both a Pub/Sub channel and a persistent log for the same events?