0
0
Redisquery~10 mins

Time-based event queues in Redis - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Time-based event queues
Add event with timestamp
Store event in sorted set
Wait for current time
Query events with score <= now
Process events
Remove processed events
Repeat
Events are added with timestamps to a sorted set, then queried and processed when their time arrives.
Execution Sample
Redis
ZADD event_queue 1672531200 "event1"
ZADD event_queue 1672531300 "event2"
ZRANGEBYSCORE event_queue -inf 1672531250
ZREM event_queue "event1"
Add events with timestamps, get events due by a certain time, then remove processed events.
Execution Table
StepCommandActionResultState of Sorted Set
1ZADD event_queue 1672531200 "event1"Add event1 with score 16725312001 (added)[('event1', 1672531200)]
2ZADD event_queue 1672531300 "event2"Add event2 with score 16725313001 (added)[('event1', 1672531200), ('event2', 1672531300)]
3ZRANGEBYSCORE event_queue -inf 1672531250Get events with score <= 1672531250['event1'][('event1', 1672531200), ('event2', 1672531300)]
4ZREM event_queue "event1"Remove event1 after processing1 (removed)[('event2', 1672531300)]
5ZRANGEBYSCORE event_queue -inf 1672531250Get events with score <= 1672531250 again[][('event2', 1672531300)]
6ZRANGEBYSCORE event_queue -inf 1672531350Get events with score <= 1672531350['event2'][('event2', 1672531300)]
7ZREM event_queue "event2"Remove event2 after processing1 (removed)[]
8ZRANGEBYSCORE event_queue -inf 1672531350Get events with score <= 1672531350 after all removed[][]
💡 No more events in the queue, all processed and removed.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 4After Step 7Final
event_queue[][('event1', 1672531200)][('event1', 1672531200), ('event2', 1672531300)][('event2', 1672531300)][][]
Key Moments - 3 Insights
Why do we use a sorted set for the event queue?
Because sorted sets keep events ordered by timestamp (score), making it easy to query events due up to the current time (see execution_table rows 3 and 6).
What happens if we don't remove processed events?
They remain in the queue and will be returned again in future queries, causing duplicate processing (see execution_table row 5 shows event1 is gone after removal).
Why do we query with ZRANGEBYSCORE using -inf to current time?
To get all events with timestamps less than or equal to now, ensuring we process all due events (see execution_table rows 3 and 6).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the state of the event_queue after step 2?
A[('event1', 1672531200), ('event2', 1672531300)]
B[('event2', 1672531300)]
C[('event1', 1672531200)]
D[]
💡 Hint
Check the 'State of Sorted Set' column at step 2.
At which step does the event 'event1' get removed from the queue?
AStep 3
BStep 5
CStep 4
DStep 6
💡 Hint
Look at the 'Command' and 'Action' columns to see when ZREM removes event1.
If we query ZRANGEBYSCORE with a timestamp less than 1672531200 at step 3, what would be the result?
A['event1']
B[]
C['event2']
D['event1', 'event2']
💡 Hint
Refer to step 3 where the query is up to 1672531250 and returns 'event1'; less than 1672531200 means no events.
Concept Snapshot
Time-based event queues in Redis use sorted sets.
Add events with ZADD using timestamps as scores.
Query due events with ZRANGEBYSCORE -inf to current time.
Process and remove events with ZREM.
This keeps events ordered and easy to manage by time.
Full Transcript
In Redis, time-based event queues are implemented using sorted sets. Each event is added with a timestamp as its score using ZADD. The queue stores events ordered by their timestamps. To process events due at the current time, we query the sorted set with ZRANGEBYSCORE from negative infinity up to the current timestamp. After processing, events are removed with ZREM to avoid reprocessing. This cycle repeats to handle events as their time arrives. The execution table shows adding two events, querying due events, processing, and removing them step-by-step. The variable tracker shows how the event queue changes after each operation. Key moments clarify why sorted sets are used, the importance of removing processed events, and how queries select due events. The visual quiz tests understanding of the queue state and commands at different steps.