0
0
Redisquery~10 mins

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

Choose your learning style9 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.