Redis as cache vs data store vs message broker - Performance Comparison
When using Redis in different roles like cache, data store, or message broker, it's important to understand how the time it takes to do tasks changes as the amount of data or messages grows.
We want to see how Redis handles more data or messages in each role.
Analyze the time complexity of these Redis commands in different roles.
# Cache: Set and Get operations
SET user:1 "Alice"
GET user:1
# Data Store: Sorted Set add and range query
ZADD leaderboard 100 "player1"
ZRANGE leaderboard 0 9 WITHSCORES
# Message Broker: Publish and Subscribe
PUBLISH channel1 "Hello"
SUBSCRIBE channel1
This code shows basic commands for Redis as a cache, data store, and message broker.
- Primary operation: Each command like SET, GET, ZADD, ZRANGE, PUBLISH, SUBSCRIBE.
- How many times: These commands run once per request, but many requests happen over time.
- For ZRANGE, the number of items requested affects how much work Redis does.
- For PUBLISH, Redis sends messages to all subscribers, so work grows with subscriber count.
Let's see how work grows when we increase data or messages.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 commands, small data sets, few subscribers |
| 100 | About 100 commands, larger sorted sets, more subscribers |
| 1000 | About 1000 commands, big sorted sets, many subscribers |
Pattern observation: For simple SET/GET, work grows linearly with requests. For sorted sets, work depends on how many items you ask for. For publish, work grows with number of subscribers.
Time Complexity: O(1) for cache SET/GET, O(log n) for sorted set add, O(m) for sorted set range of m items, O(s) for publish with s subscribers
This means simple cache commands take the same time no matter data size, sorted sets take a bit more time as data grows, and publishing messages takes more time as subscribers increase.
[X] Wrong: "All Redis commands take the same time no matter what."
[OK] Correct: Some commands like SET and GET are very fast and steady, but others like range queries or publishing messages depend on how much data or how many subscribers there are.
Understanding how Redis handles different tasks helps you explain your choices clearly and shows you know how systems work as they grow. This skill is useful anytime you design or talk about fast data handling.
"What if we changed the number of subscribers in the message broker role? How would the time complexity of PUBLISH change?"
Practice
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 CQuick Check:
Cache = Temporary fast storage [OK]
- Confusing cache with permanent data store
- Thinking Redis analyzes data automatically
- Mixing message broker role with cache
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.Final Answer:
PUBLISH -> Option BQuick Check:
Send message = PUBLISH [OK]
- Using SET or GET to send messages
- Confusing SUBSCRIBE with PUBLISH
- Thinking GET publishes messages
Solution
Step 1: Analyze session data with expiration
Storing data with expiration to speed up access fits caching, as data is temporary and speeds up app.Step 2: Exclude other roles
Message broker sends messages, data store keeps permanent data, analytics engine is unrelated.Final Answer:
Cache -> Option AQuick Check:
Temporary session data = Cache [OK]
- Thinking session data is permanent storage
- Confusing message broker with cache
- Assuming analytics role for session data
SUBSCRIBE news. What is wrong if your goal was to send a message?Solution
Step 1: Understand SUBSCRIBE command purpose
SUBSCRIBE listens to messages on a channel; it does not send messages.Step 2: Identify correct sending command
To send messages, use PUBLISH, not SUBSCRIBE.Final Answer:
SUBSCRIBE is for receiving messages, not sending. -> Option DQuick Check:
SUBSCRIBE = receive, PUBLISH = send [OK]
- Using SUBSCRIBE to send messages
- Thinking SUBSCRIBE deletes channels
- Believing SUBSCRIBE stores messages
Solution
Step 1: Identify Redis roles for caching and messaging
Redis can cache data to speed access and use PUBLISH/SUBSCRIBE commands to send messages between app parts.Step 2: Exclude incorrect setups
Ignoring messaging misses notification need; message broker only misses caching; permanent message storage is not typical.Final Answer:
Use Redis as a cache for data and use PUBLISH/SUBSCRIBE for messaging. -> Option AQuick Check:
Cache + Pub/Sub = correct multi-role use [OK]
- Using Redis only for one role when multiple needed
- Trying to store messages permanently in Redis
- Ignoring pub/sub for notifications
Ready to prove it?
Pass the test and this pattern moves unknown
