0
0
Redisquery~30 mins

List vs sorted set for sequences in Redis - Hands-On Comparison

Choose your learning style9 modes available
Using Redis Lists and Sorted Sets for Sequences
📖 Scenario: You are building a simple message queue system where messages need to be stored and retrieved in order. You want to explore two ways to store these messages in Redis: using a List and using a Sorted Set.
🎯 Goal: Learn how to create a Redis List and a Sorted Set to store sequences of messages, add messages to them, and retrieve messages in order.
📋 What You'll Learn
Create a Redis List called message_list with three messages: 'msg1', 'msg2', 'msg3'
Create a variable called score and set it to 1
Add the same three messages to a Sorted Set called message_sorted_set with increasing scores starting from score
Retrieve all messages from both message_list and message_sorted_set in order
💡 Why This Matters
🌍 Real World
Message queues and ordered event logs often use Redis Lists or Sorted Sets to store sequences of events or messages.
💼 Career
Understanding how to use Redis data structures for ordered data is important for backend developers working with caching, messaging, and real-time data.
Progress0 / 4 steps
1
Create a Redis List with messages
Use the Redis command LPUSH to create a List called message_list and add the messages 'msg1', 'msg2', and 'msg3' in that order.
Redis
Need a hint?

Remember that LPUSH adds elements to the head of the list, so add messages in reverse order to keep the sequence.

2
Create a score variable for Sorted Set
Create a variable called score and set it to 1. This will be used as the starting score for adding messages to the Sorted Set.
Redis
Need a hint?

Use the Redis SET command to create the variable score.

3
Add messages to a Sorted Set with scores
Use the Redis command ZADD to add the messages 'msg1', 'msg2', and 'msg3' to a Sorted Set called message_sorted_set. Use the variable score for the first message's score, then increment the score by 1 for each next message.
Redis
Need a hint?

Use ZADD with increasing scores starting from 1 for each message.

4
Retrieve messages from List and Sorted Set
Use the Redis commands LRANGE and ZREVRANGE to retrieve all messages from message_list and message_sorted_set respectively, in the order they were added.
Redis
Need a hint?

Use LRANGE message_list 0 -1 to get all list items and ZREVRANGE message_sorted_set 0 -1 to get all sorted set items in ascending order by score.