📖 Scenario: You are building a simple real-time notification system for a social media app. Users receive notifications when someone likes their post.
🎯 Goal: Create a Redis data structure to store notifications, configure a threshold for unread notifications, implement the logic to add new notifications, and finalize the setup to mark notifications as read.
📋 What You'll Learn
Create a Redis list called notifications:user123 with three initial notifications
Add a variable max_unread set to 5 to limit unread notifications
Write a Redis command to add a new notification to notifications:user123
Write a Redis command to trim the list to keep only the latest max_unread notifications
💡 Why This Matters
🌍 Real World
Real-time notifications are essential in social media apps, messaging platforms, and online marketplaces to keep users engaged and informed instantly.
💼 Career
Understanding how to use Redis lists and commands like LPUSH and LTRIM is valuable for backend developers working on scalable, real-time systems.
Progress0 / 4 steps
1
DATA SETUP: Create initial notifications list
Create a Redis list called notifications:user123 and add these three notifications exactly: 'Like from Alice', 'Like from Bob', 'Like from Charlie' using LPUSH commands.
Redis
Hint
Use LPUSH to add items to the Redis list notifications:user123. Add each notification as a separate command.
2
CONFIGURATION: Set maximum unread notifications
Create a variable called max_unread and set it to 5 to limit the number of unread notifications stored.
Redis
Hint
Assign the number 5 to a variable named max_unread to represent the maximum unread notifications.
3
CORE LOGIC: Add a new notification
Use LPUSH to add a new notification 'Like from Dave' to the Redis list notifications:user123.
Redis
Hint
Use LPUSH to add the new notification 'Like from Dave' to the list notifications:user123.
4
COMPLETION: Trim the notifications list
Use the LTRIM command to keep only the latest max_unread notifications in the list notifications:user123. Use the range 0 to max_unread - 1.
Redis
Hint
Use LTRIM notifications:user123 0 4 to keep only the first 5 notifications (index 0 to 4).
Practice
(1/5)
1. What is the main purpose of the PUBLISH command in Redis real-time notification pattern?
easy
A. To store data permanently
B. To send messages to a specific channel
C. To listen for messages on a channel
D. To delete a channel
Solution
Step 1: Understand the role of PUBLISH
The PUBLISH command is used to send messages to a channel in Redis.
Step 2: Differentiate from SUBSCRIBE
SUBSCRIBE listens for messages, but PUBLISH sends them.
Final Answer:
To send messages to a specific channel -> Option B
Quick Check:
PUBLISH sends messages [OK]
Hint: PUBLISH sends, SUBSCRIBE listens [OK]
Common Mistakes:
Confusing PUBLISH with SUBSCRIBE
Thinking PUBLISH stores data
Assuming PUBLISH deletes channels
2. Which of the following is the correct syntax to subscribe to a channel named news in Redis?
easy
A. LISTEN news
B. PUBLISH news
C. SUBSCRIBE('news')
D. SUBSCRIBE news
Solution
Step 1: Recall Redis SUBSCRIBE syntax
The correct syntax is SUBSCRIBE channel_name without parentheses.
Step 2: Check each option
SUBSCRIBE news matches the correct syntax. SUBSCRIBE('news') uses parentheses which is invalid in Redis CLI.
Final Answer:
SUBSCRIBE news -> Option D
Quick Check:
SUBSCRIBE channel_name [OK]
Hint: SUBSCRIBE uses plain command and channel name [OK]
Common Mistakes:
Using parentheses like a function call
Confusing PUBLISH with SUBSCRIBE
Using LISTEN which is not a Redis command
3. Consider this Redis command sequence:
SUBSCRIBE updates
PUBLISH updates "New message arrived"
What will the subscriber receive?
medium
A. "New message arrived" on the updates channel
B. No message because SUBSCRIBE blocks commands
C. An error because PUBLISH cannot be used after SUBSCRIBE
D. The message on a different channel
Solution
Step 1: Understand SUBSCRIBE behavior
SUBSCRIBE listens to messages on the specified channel and receives published messages.
Step 2: Analyze PUBLISH effect
PUBLISH sends the message "New message arrived" to the updates channel, which the subscriber listens to.
Final Answer:
"New message arrived" on the updates channel -> Option A
Quick Check:
SUBSCRIBE receives PUBLISH messages [OK]
Hint: SUBSCRIBE gets messages published to its channel [OK]
Why does the subscriber not receive the "Bye" message?
medium
A. Because SUBSCRIBE blocks PUBLISH commands
B. Because PUBLISH only works before SUBSCRIBE
C. Because UNSUBSCRIBE stops listening to the channel
D. Because messages are lost after first publish
Solution
Step 1: Understand UNSUBSCRIBE effect
UNSUBSCRIBE removes the subscriber from the channel, so it stops receiving messages.
Step 2: Analyze message delivery after UNSUBSCRIBE
After unsubscribing, the subscriber no longer listens, so "Bye" is not received.
Final Answer:
Because UNSUBSCRIBE stops listening to the channel -> Option C
Quick Check:
UNSUBSCRIBE stops message reception [OK]
Hint: UNSUBSCRIBE stops listening, no more messages [OK]
Common Mistakes:
Thinking PUBLISH only works before SUBSCRIBE
Assuming messages are lost after first publish
Believing SUBSCRIBE blocks PUBLISH commands
5. You want to build a real-time notification system using Redis where multiple users subscribe to different channels. Which approach ensures users only get messages for their subscribed channels without missing any messages?
hard
A. Use PUBLISH/SUBSCRIBE pattern and keep subscribers connected continuously
B. Use Redis lists to push messages and clients poll periodically
C. Use PUBLISH to send messages and clients fetch keys manually
D. Use Redis transactions to lock channels during message send
Solution
Step 1: Understand real-time delivery needs
Real-time notifications require instant delivery without polling delays.
Step 2: Evaluate options for message delivery
PUBLISH/SUBSCRIBE keeps clients connected and delivers messages instantly to subscribed channels.
Step 3: Compare with alternatives
Polling (Use Redis lists to push messages and clients poll periodically) causes delays; fetching keys manually (Use PUBLISH to send messages and clients fetch keys manually) is inefficient; transactions (Use Redis transactions to lock channels during message send) do not help with message delivery.
Final Answer:
Use PUBLISH/SUBSCRIBE pattern and keep subscribers connected continuously -> Option A