Time-based event queues help you manage tasks or events that need to happen at specific times. They keep events in order by when they should run.
Time-based event queues in Redis
ZADD queue_name timestamp event_data ZRANGEBYSCORE queue_name -inf current_time ZREM queue_name event_data
ZADD adds an event with a score (timestamp) to a sorted set.
ZRANGEBYSCORE fetches events with scores up to the current time.
ZADD myqueue 1686000000 "send_email:user123" ZRANGEBYSCORE myqueue -inf 1686000000
ZADD myqueue 1686003600 "backup_db" ZRANGEBYSCORE myqueue -inf 1686000000
ZRANGEBYSCORE myqueue -inf 1686000000 ZREM myqueue "send_email:user123"
This example adds two events with different times. It fetches events due at the first timestamp, removes the processed event, then fetches events due at the later timestamp.
ZADD event_queue 1686000000 "notify_user:42" ZADD event_queue 1686003600 "cleanup_temp_files" ZRANGEBYSCORE event_queue -inf 1686000000 ZREM event_queue "notify_user:42" ZRANGEBYSCORE event_queue -inf 1686003600
Time complexity for ZADD, ZREM, and ZRANGEBYSCORE is O(log N) where N is the number of events.
Space complexity depends on the number of events stored in the sorted set.
Common mistake: forgetting to remove processed events, causing repeated processing.
Use time-based queues when you need ordered, timed execution; use simple lists if order or timing is not important.
Time-based event queues use Redis sorted sets with timestamps as scores.
They let you schedule and fetch events that should happen at or before a certain time.
Always remove events after processing to avoid duplicates.
Practice
Solution
Step 1: Understand Redis sorted sets
Redis sorted sets store members with a score that determines their order.Step 2: Apply to time-based event queues
Using timestamps as scores allows events to be retrieved in chronological order.Final Answer:
To store events with timestamps as scores for ordered retrieval -> Option BQuick Check:
Sorted sets + timestamps = ordered event queue [OK]
- Thinking sorted sets don't keep order
- Confusing hashes with sorted sets
- Ignoring the timestamp as score
events?Solution
Step 1: Identify the command for sorted sets
ZADD adds members with scores to sorted sets; others are for different data types.Step 2: Check syntax correctness
ZADD events 1625097600 "event1" adds "event1" with score 1625097600 to sorted set events.Final Answer:
ZADD events 1625097600 "event1" -> Option CQuick Check:
ZADD = add to sorted set [OK]
- Using SADD which is for sets without scores
- Using HSET which is for hashes
- Using LPUSH which is for lists
events with members and scores:{"event1": 1000, "event2": 1500, "event3": 2000}What will the command
ZREVRANGEBYSCORE events 1500 -inf return?Solution
Step 1: Understand ZREVRANGEBYSCORE command
This command returns members with scores between max and min in descending order.Step 2: Apply to given scores
Scores ≤ 1500 are event1 (1000) and event2 (1500). Descending order: event2, event1.Final Answer:
["event2", "event1"] -> Option DQuick Check:
Descending scores ≤ 1500 = event2, event1 [OK]
- Confusing ascending with descending order
- Including events with scores above max
- Misreading score boundaries
ZREVRANGEBYSCORE events 1625097600 -infBut you notice the same events are returned repeatedly. What is the likely mistake?
Solution
Step 1: Understand event queue behavior
After processing events, they must be removed to avoid duplicates.Step 2: Identify cause of repeated events
If events are not removed, the same events appear on every fetch.Final Answer:
Not removing processed events from the sorted set -> Option AQuick Check:
Remove events after processing to avoid repeats [OK]
- Thinking order command causes repeats
- Ignoring event removal step
- Assuming wrong key name causes repeats
Solution
Step 1: Fetch due events with ZRANGEBYSCORE
ZRANGEBYSCORE with max = current timestamp fetches all events due up to now.Step 2: Remove fetched events with ZREM
Removing them prevents duplicates; doing both steps atomically avoids race conditions.Final Answer:
Use ZRANGEBYSCORE to get events ≤ now, then ZREM to remove them -> Option AQuick Check:
Fetch ≤ now + remove = atomic event processing [OK]
- Using ZREVRANGEBYSCORE with wrong score range
- Using sets or lists instead of sorted sets
- Not removing events after fetching
Ready to prove it?
Pass the test and this pattern moves unknown
