Using Redis as Cache, Data Store, and Message Broker
📖 Scenario: You are building a simple web application that needs to store user session data, cache frequently accessed product information, and send notifications between different parts of the app.Redis can help with all these tasks by acting as a cache, a data store, and a message broker.
🎯 Goal: Learn how to use Redis for three common roles: caching data, storing persistent data, and sending messages between services.You will write Redis commands step-by-step to set up each role.
📋 What You'll Learn
Create a Redis key-value pair to store user session data (data store)
Create a Redis key-value pair to cache product information (cache)
Set an expiration time for the cached product data
Use Redis Pub/Sub commands to send and receive a notification message (message broker)
💡 Why This Matters
🌍 Real World
Web applications often need fast access to session data, quick caching of product info, and real-time notifications. Redis supports all these needs efficiently.
💼 Career
Understanding Redis roles helps backend developers, DevOps engineers, and system architects build responsive and scalable systems.
Progress0 / 4 steps
1
Store user session data in Redis
Use the Redis command SET to create a key called session:user123 with the value {"username":"alice","role":"admin"}. This simulates storing user session data persistently.
Redis
Hint
Use SET key value to store data in Redis.
2
Cache product information with expiration
Use the Redis command SET with the EX option to create a key called cache:product:456 with the value {"name":"Coffee Mug","price":12.99} that expires after 60 seconds. This simulates caching product data temporarily.
Redis
Hint
Use SET key value EX seconds to set a key with expiration.
3
Publish a notification message
Use the Redis command PUBLISH to send the message "New order received" to the channel notifications. This simulates sending a message between services.
Redis
Hint
Use PUBLISH channel message to send messages in Redis Pub/Sub.
4
Subscribe to the notification channel
Use the Redis command SUBSCRIBE to listen to the notifications channel. This simulates receiving messages sent by other parts of the app.
Redis
Hint
Use SUBSCRIBE channel to listen for messages in Redis Pub/Sub.
Practice
(1/5)
1. Which of the following best describes Redis when used as a cache?
easy
A. It sends messages between different parts of an app.
B. It permanently stores large amounts of data for apps.
C. It temporarily stores data to speed up app responses.
D. It analyzes data to find patterns automatically.
Solution
Step 1: Understand Redis cache role
Redis cache stores data temporarily to help apps respond faster by avoiding repeated data fetching.
Step 2: Compare other roles
Permanent storage is data store, messaging is message broker, and analysis is not Redis's main role.
Final Answer:
It temporarily stores data to speed up app responses. -> Option C
Quick Check:
Cache = Temporary fast storage [OK]
Hint: Cache means temporary fast storage to speed apps [OK]
Common Mistakes:
Confusing cache with permanent data store
Thinking Redis analyzes data automatically
Mixing message broker role with cache
2. Which Redis command is used to publish a message to a channel when Redis acts as a message broker?
easy
A. SET
B. PUBLISH
C. GET
D. SUBSCRIBE
Solution
Step 1: Identify publish command in Redis
The PUBLISH command sends a message to subscribers on a channel in Redis's message broker role.
Step 2: Differentiate from other commands
SET stores data, GET retrieves data, SUBSCRIBE listens to messages but does not send them.