📖 Scenario: You are building a simple task manager that processes tasks based on their priority. Tasks with higher priority should be handled first.
🎯 Goal: Create a Redis sorted set to store tasks with their priorities, add tasks with specific priorities, retrieve the highest priority task, and remove it from the queue.
📋 What You'll Learn
Create a Redis sorted set named task_queue.
Add three tasks with exact names and priorities to task_queue: task1 with priority 10, task2 with priority 20, and task3 with priority 15.
Retrieve the task with the highest priority (highest score) from task_queue using the correct Redis command.
Remove the highest priority task from task_queue.
💡 Why This Matters
🌍 Real World
Priority queues are used in job scheduling, message processing, and task management systems to ensure important tasks are handled first.
💼 Career
Understanding Redis sorted sets and priority queues is valuable for backend developers, DevOps engineers, and anyone working with real-time data processing.
Progress0 / 4 steps
1
Create the priority queue sorted set
Create a Redis sorted set called task_queue and add these tasks with their priorities: task1 with score 10, task2 with score 20, and task3 with score 15 using the ZADD command.
Redis
Hint
Use ZADD followed by the sorted set name, then pairs of score member.
2
Configure retrieval of highest priority task
Write a Redis command to retrieve the task with the highest priority from task_queue using ZREVRANGE with start and stop indexes to get only the top task.
Redis
Hint
ZREVRANGE returns members ordered from highest to lowest score. Use indexes 0 to 0 to get the top one.
3
Remove the highest priority task
Write a Redis command to remove task2 from task_queue using the ZREM command.
Redis
Hint
Use ZREM followed by the sorted set name and the member to remove.
4
Verify the updated priority queue
Write a Redis command to retrieve all tasks in task_queue ordered from highest to lowest priority using ZREVRANGE with start 0 and stop -1.
Redis
Hint
Use ZREVRANGE with indexes 0 to -1 to get all members from highest to lowest score.
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
Step 1: Understand Redis data structures
Redis offers several data types; sorted sets allow ordering by score.
Step 2: Identify priority queue implementation
Priority queues use sorted sets where scores represent priority levels.
Final Answer:
Sorted sets with scores representing priority -> Option C
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
Step 1: Identify command to add to sorted sets
ZADD adds members with scores to sorted sets in Redis.
Step 2: Match command to priority queue addition
Priority queues use sorted sets, so ZADD is the correct command.
Final Answer:
ZADD -> Option B
Quick Check:
Add to priority queue = ZADD [OK]
Hint: Use ZADD to add with score in sorted sets [OK]