Зміст курсу
Introduction to Redis
Introduction to Redis
PubSub
As shown in the illustration, publishers send messages to specific channels, and subscribers listening to those channels receive the corresponding notifications.
Practical Use Case
Subscription System
Imagine an e-commerce platform where users need to be notified about the status of their orders. As the order status changes (Processing
, Shipped
, Delivered
), users must receive timely updates. Redis subscriptions and notifications can be used to implement this system.
Subscribing to a Channel
Each user subscribes to a unique channel, typically named after their order ID. This ensures that each user receives notifications only about their specific order.
Client 1
subscribes to the channel order-123
:
After subscribing to the order-123
channel, the client listens for messages on that channel. Whenever a message is sent to this channel, the client receives it immediately.
Publishing an Order Status Update
When the status of User 1's
order changes (e.g., the order is shipped), the system publishes a message to the order-123
channel. This message is delivered to all clients subscribed to the channel.
The system publishes the following update for User 1
:
Client 1
, subscribed to order-123
, receives the following message:
Unsubscribing from a Channel
If a client no longer wants to receive updates, they can unsubscribe from the channel using the UNSUBSCRIBE
command.
Client 1
unsubscribes from order-123
:
After unsubscribing, Client 1
will no longer receive notifications about their order status unless they subscribe to the channel again.
This example demonstrates how Redis subscriptions and notifications can be used to create real-time messaging systems. Redis ensures timely delivery of notifications and simplifies communication within the system.
1. What does the SUBSCRIBE
command in Redis do?
2. Which command is used to unsubscribe from a Redis channel?
Дякуємо за ваш відгук!