Bird
Raised Fist0
Redisquery~10 mins

Priority queue pattern in Redis - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Priority queue pattern
Add item with score
↓
Store in sorted set
↓
Retrieve item with lowest score
↓
Process item
↓
Remove item from sorted set
↓
Repeat or exit if empty
Items are added with a priority score to a sorted set. The lowest score item is retrieved and removed for processing, repeating until empty.
Execution Sample
Redis
ZADD queue 10 task1
ZADD queue 5 task2
ZRANGE queue 0 0 WITHSCORES
ZREM queue task2
Add tasks with priorities, get the highest priority task, then remove it from the queue.
Execution Table
StepCommandActionSorted Set StateOutput
1ZADD queue 10 task1Add task1 with score 10{task1:10}1 (new element added)
2ZADD queue 5 task2Add task2 with score 5{task1:10, task2:5}1 (new element added)
3ZRANGE queue 0 0 WITHSCORESGet item with lowest score{task1:10, task2:5}["task2", "5"]
4ZREM queue task2Remove task2 from queue{task1:10}1 (element removed)
5ZRANGE queue 0 0 WITHSCORESGet next item with lowest score{task1:10}["task1", "10"]
6ZREM queue task1Remove task1 from queue{}1 (element removed)
7ZRANGE queue 0 0 WITHSCORESQueue empty, no items{}[]
💡 Queue is empty after removing all tasks, no more items to process.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 4After Step 6Final
queue (sorted set){}{task1:10}{task1:10, task2:5}{task1:10}{}{}
Key Moments - 3 Insights
Why does ZRANGE with 0 0 return the lowest score item?
ZRANGE returns items in ascending order by score, so 0 0 selects the first item with the lowest score, as shown in execution_table step 3.
What happens if we try to remove an item not in the queue?
ZREM returns 0 and does not change the queue. This is not shown here but follows the same pattern as step 4 where removal returns 1 for existing items.
Why do we remove the item after processing?
Removing the item (step 4 and 6) ensures it won't be processed again, keeping the queue updated with only pending tasks.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the queue state after step 2?
A{"task1":5, "task2":10}
B{"task1":10, "task2":5}
C{"task2":10}
D{}
💡 Hint
Check the Sorted Set State column in row for step 2.
At which step does the queue become empty?
AStep 4
BStep 7
CStep 6
DStep 3
💡 Hint
Check the Sorted Set State column in row for step 6.
If we add a task with score 3 after step 6, what will ZRANGE 0 0 WITHSCORES return?
A["new_task", "3"]
B["task1", "10"]
C["task2", "5"]
D[]
💡 Hint
New lower score means it will be first in ascending order, see concept_flow.
Concept Snapshot
Priority queue in Redis uses sorted sets.
Add items with ZADD and a score (priority).
ZRANGE 0 0 WITHSCORES gets the highest priority item.
Remove processed items with ZREM.
Repeat until queue is empty.
Full Transcript
This visual execution shows how Redis implements a priority queue using sorted sets. Items are added with a score representing priority. The command ZRANGE with 0 0 and WITHSCORES retrieves the item with the lowest score, which is the highest priority. After processing, the item is removed with ZREM. This cycle repeats until the queue is empty. The execution table tracks each step, showing the queue state and outputs. Key moments clarify why the lowest score is retrieved first and why removal is necessary. The visual quiz tests understanding of queue state changes and command effects.

Practice

(1/5)
1. What does a priority queue in Redis typically use to manage item importance?
easy
A. Lists with push and pop operations
B. Hashes with key-value pairs
C. Sorted sets with scores representing priority
D. Strings storing serialized data

Solution

  1. Step 1: Understand Redis data structures

    Redis offers several data types; sorted sets allow ordering by score.
  2. Step 2: Identify priority queue implementation

    Priority queues use sorted sets where scores represent priority levels.
  3. Final Answer:

    Sorted sets with scores representing priority -> Option C
  4. Quick Check:

    Priority queue = sorted sets with scores [OK]
Hint: Remember: sorted sets sort by score for priority [OK]
Common Mistakes:
  • Confusing lists with priority queues
  • Using hashes which don't order by score
  • Thinking strings can manage priority
2. Which Redis command adds an item with a priority score to a priority queue?
easy
A. HSET
B. ZADD
C. LPUSH
D. SET

Solution

  1. Step 1: Identify command to add to sorted sets

    ZADD adds members with scores to sorted sets in Redis.
  2. Step 2: Match command to priority queue addition

    Priority queues use sorted sets, so ZADD is the correct command.
  3. Final Answer:

    ZADD -> Option B
  4. Quick Check:

    Add to priority queue = ZADD [OK]
Hint: Use ZADD to add with score in sorted sets [OK]
Common Mistakes:
  • Using LPUSH which is for lists
  • Using HSET which is for hashes
  • Using SET which is for strings
3. Given the commands:
ZADD tasks 1 "task1" 3 "task3" 2 "task2"
ZPOPMIN tasks

What will be the output of ZPOPMIN tasks?
medium
A. ["task1", "1"]
B. ["task3", "3"]
C. ["task2", "2"]
D. Syntax error

Solution

  1. Step 1: Understand ZADD insertion

    Three tasks added with scores 1, 3, and 2 respectively.
  2. Step 2: Understand ZPOPMIN behavior

    ZPOPMIN removes and returns the member with the lowest score, which is "task1" with score 1.
  3. Final Answer:

    ["task1", "1"] -> Option A
  4. Quick Check:

    Lowest score item popped = task1 [OK]
Hint: ZPOPMIN returns lowest score member first [OK]
Common Mistakes:
  • Picking highest score instead of lowest
  • Confusing ZPOPMIN with ZPOPMAX
  • Expecting syntax error from valid commands
4. What is wrong with this Redis command sequence for a priority queue?
ZADD queue "task1" 1
ZPOPMIN queue
medium
A. ZADD requires at least two members
B. ZPOPMIN does not remove items
C. Queue key must be a list, not a sorted set
D. ZADD syntax is incorrect; score must come before member

Solution

  1. Step 1: Check ZADD command syntax

    ZADD syntax requires the score before the member, but here member is before score.
  2. Step 2: Verify ZPOPMIN behavior

    ZPOPMIN removes the lowest score member correctly; no issue here.
  3. Final Answer:

    ZADD syntax is incorrect; score must come before member -> Option D
  4. Quick Check:

    ZADD syntax = score before member [OK]
Hint: Score always before member in ZADD [OK]
Common Mistakes:
  • Swapping score and member in ZADD
  • Thinking ZPOPMIN doesn't remove items
  • Using wrong data type for priority queue
5. You want to implement a priority queue in Redis where lower scores mean higher priority. You add tasks:
ZADD jobs 5 "job5" 1 "job1" 3 "job3"

Then you run:
ZPOPMIN jobs 2

What will be the result and what remains in the queue?
hard
A. Returns ["job1", "1", "job3", "3"]; queue left with ["job5"]
B. Returns ["job5", "3", "job3", "1"]; queue left with ["job1"]
C. Returns ["job1", "5", "job3", "3"]; queue left with ["job5"]
D. Returns error because ZPOPMIN cannot pop multiple items

Solution

  1. Step 1: Understand ZPOPMIN with count

    ZPOPMIN with argument 2 pops two lowest score members.
  2. Step 2: Identify lowest scores and remaining items

    Lowest scores are 1 (job1) and 3 (job3), so these are popped; job5 with score 5 remains.
  3. Final Answer:

    Returns ["job1", "1", "job3", "3"]; queue left with ["job5"] -> Option A
  4. Quick Check:

    ZPOPMIN 2 pops two lowest score items [OK]
Hint: ZPOPMIN count pops that many lowest score items [OK]
Common Mistakes:
  • Mixing up scores and members order
  • Thinking ZPOPMIN pops highest scores
  • Assuming ZPOPMIN can't pop multiple items
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard