Lists and sorted sets are two ways to store sequences in Redis. They help keep items in order but work differently.
List vs sorted set for sequences in Redis
List commands: LPUSH key value1 [value2 ...] # Add items to the start RPUSH key value1 [value2 ...] # Add items to the end LRANGE key start stop # Get items by index range Sorted Set commands: ZADD key score1 member1 [score2 member2 ...] # Add items with scores ZRANGE key start stop [WITHSCORES] # Get items by rank range ZRANGEBYSCORE key min max [WITHSCORES] # Get items by score range
Lists keep order by insertion and allow duplicates.
Sorted sets keep items unique and order by score.
LPUSH mylist "apple" "banana" LRANGE mylist 0 -1
RPUSH mylist "apple" "banana" LRANGE mylist 0 -1
ZADD myset 10 "apple" 20 "banana" ZRANGE myset 0 -1 WITHSCORES
ZADD myset 10 "apple" ZADD myset 5 "banana" ZRANGEBYSCORE myset 0 10 WITHSCORES
This example shows how to add items to a list and a sorted set, then retrieve them. The list keeps insertion order. The sorted set orders by score.
# Using Redis CLI commands # Create a list and add items RPUSH fruits "apple" RPUSH fruits "banana" RPUSH fruits "cherry" # Show list items LRANGE fruits 0 -1 # Create a sorted set and add items with scores ZADD fruit_scores 3 "apple" ZADD fruit_scores 1 "banana" ZADD fruit_scores 2 "cherry" # Show sorted set items with scores ZRANGE fruit_scores 0 -1 WITHSCORES
Lists are simple and fast for adding/removing items at ends (O(1) time).
Sorted sets keep items unique and sorted by score, but adding is O(log n) time.
Use lists when order of insertion matters and duplicates are allowed.
Use sorted sets when you need automatic sorting by score and uniqueness.
Lists store ordered sequences with duplicates allowed.
Sorted sets store unique items sorted by a numeric score.
Choose lists for simple order, sorted sets for scored sorting.
Practice
Solution
Step 1: Understand List behavior
Lists in Redis keep elements in the order they were added and allow duplicates.Step 2: Compare with Sorted Set
Sorted sets store unique elements sorted by score, so duplicates are not allowed.Final Answer:
List -> Option BQuick Check:
Duplicates allowed and order preserved = List [OK]
- Thinking Sorted Set allows duplicates
- Confusing Sets with Lists
- Assuming Hashes store sequences
Solution
Step 1: Identify command for sorted set insertion
ZADD adds elements with a score to a sorted set.Step 2: Check other commands
LPUSH adds to list, SADD adds to set, HSET adds to hash field.Final Answer:
ZADD myzset 10 element -> Option DQuick Check:
ZADD = add with score [OK]
- Using LPUSH for sorted sets
- Confusing SADD with ZADD
- Using HSET for sequences
LPUSH mylist aLPUSH mylist bLPUSH mylist cWhat will
LRANGE mylist 0 -1 return?Solution
Step 1: Understand LPUSH order
LPUSH adds elements to the head (start) of the list, so last pushed is first.Step 2: Trace the list after pushes
LPUSH mylist a: ["a"], then LPUSH mylist b: ["b", "a"], then LPUSH mylist c: ["c", "b", "a"]. LRANGE mylist 0 -1 returns ["c", "b", "a"].Final Answer:
["c", "b", "a"] -> Option CQuick Check:
LPUSH adds to head, so newest first [OK]
- Assuming LPUSH adds to tail
- Mixing order of elements
- Expecting error from LRANGE
ZADD myzset 1 elementZADD myzset NX 2 elementBut the score did not update as expected. What is the likely issue?
Solution
Step 1: Understand sorted set uniqueness
Sorted sets store unique elements; adding the same element updates its score by default.Step 2: Check if score updates require flags
By default, ZADD updates score if element exists; if it did not update, likely a misunderstanding or missing flag like NX or XX.Final Answer:
Sorted sets do not allow duplicate elements; score updates require special flag -> Option AQuick Check:
Sorted sets unique, score updates need care [OK]
- Thinking duplicates allowed in sorted sets
- Assuming ZADD syntax is wrong
- Trying to remove before adding
Solution
Step 1: Analyze requirement for sorting by timestamp
Sorted sets sort elements by numeric score, perfect for timestamps.Step 2: Handle duplicates with same timestamp
Sorted sets require unique elements, so append unique suffix to actions to allow duplicates with same score.Step 3: Compare with other options
Lists preserve order but cannot sort by timestamp easily; hashes and sets do not maintain order or allow sorting by score.Final Answer:
Use a Sorted Set with timestamps as scores; append a unique suffix to actions to allow duplicates -> Option AQuick Check:
Sorted set + unique elements for timestamp sorting [OK]
- Using List but losing timestamp sorting
- Ignoring uniqueness in sorted sets
- Using Hash or Set which don't sort
Ready to prove it?
Pass the test and this pattern moves unknown
