📖 Scenario: You are building a simple event logging system for a web application. Each event represents a user action like login, logout, or page visit. You want to store these events in a way that keeps their order and allows easy retrieval later.
🎯 Goal: Create a Redis stream to store event logs, add events with timestamps, and read them back in order.
📋 What You'll Learn
Create a Redis stream called event_logs
Add three events with exact fields and values to event_logs
Set a maximum length for the stream to keep it manageable
Read all events from event_logs in the order they were added
💡 Why This Matters
🌍 Real World
Event logs are essential for tracking user actions, debugging, and analytics in web applications.
Use the Redis command XADD to add an event to the stream called event_logs. Add the first event with the fields user set to alice and action set to login. Use * for the ID to let Redis generate the timestamp.
Redis
Hint
Remember, XADD adds an entry to a Redis stream. The * lets Redis assign the current timestamp as the ID.
2
Add two more events to the stream
Add two more events to the event_logs stream using XADD. The second event should have user as bob and action as visit_page. The third event should have user as alice and action as logout. Use * for the IDs.
Redis
Hint
Use XADD again with * for the ID to add each event.
3
Limit the stream length to keep it manageable
Modify the XADD commands to limit the event_logs stream length to a maximum of 100 entries. Use the MAXLEN option with approximate trimming (~).
Redis
Hint
The MAXLEN ~ 100 option trims the stream approximately to 100 entries to avoid it growing too large.
4
Read all events from the stream in order
Use the Redis command XRANGE to read all events from the event_logs stream in the order they were added. Use - as the start ID and + as the end ID to get all entries.
Redis
Hint
XRANGE event_logs - + returns all entries from the stream in order.
Practice
(1/5)
1. Why are Redis streams well-suited for handling event logs?
easy
A. Because they only store one event at a time
B. Because they delete old events automatically
C. Because they store events in order with unique IDs
D. Because they require complex setup to use
Solution
Step 1: Understand Redis streams storage
Redis streams store events sequentially with unique IDs, preserving order.
Step 2: Match this to event log needs
Event logs need ordered, unique entries to track actions accurately.
Final Answer:
Because they store events in order with unique IDs -> Option C
Quick Check:
Streams = Ordered unique events [OK]
Hint: Streams keep events ordered with unique IDs [OK]
Common Mistakes:
Thinking streams delete old events automatically
Believing streams store only one event
Assuming streams need complex setup
2. Which Redis command is used to add a new event to a stream?
easy
A. XREAD
B. XADD
C. XRANGE
D. XDEL
Solution
Step 1: Identify command to add events
XADD is the Redis command designed to add new entries to a stream.
Step 2: Differentiate from other commands
XREAD reads events, XRANGE queries a range, XDEL deletes entries.
Final Answer:
XADD -> Option B
Quick Check:
XADD = Add event [OK]
Hint: XADD adds events to streams [OK]
Common Mistakes:
Confusing XADD with XREAD or XRANGE
Thinking XDEL adds events
Using XRANGE to add events
3. Given the Redis stream commands below, what will XRANGE mystream - + return?
XADD mystream * event "login" user "alice" XADD mystream * event "logout" user "bob"
medium
A. All events in 'mystream' from the oldest to newest
B. Only the first event added to 'mystream'
C. An error because XRANGE needs a count parameter
D. No events because the stream is empty
Solution
Step 1: Understand XRANGE command
XRANGE with '-' and '+' returns all entries from oldest to newest in the stream.
Step 2: Check stream content after XADD commands
Two events were added: a login by alice and a logout by bob.
Final Answer:
All events in 'mystream' from the oldest to newest -> Option A
Quick Check:
XRANGE - + = all events [OK]
Hint: XRANGE - + returns all stream events [OK]
Common Mistakes:
Thinking XRANGE returns only one event
Believing XRANGE requires a count parameter
Assuming stream is empty after XADD
4. You run the command XADD mystream * event but get an error. What is the likely cause?
medium
A. XRANGE command is required before XADD
B. Using * instead of a specific ID
C. Stream 'mystream' does not exist
D. Missing field-value pairs after the event name
Solution
Step 1: Check XADD syntax requirements
XADD requires field-value pairs after the ID; 'event' lacks a value.
Step 2: Validate other options
Using '*' is correct for auto ID, streams can be created automatically, and XRANGE is unrelated.
Final Answer:
Missing field-value pairs after the event name -> Option D
Quick Check:
XADD needs field-value pairs [OK]
Hint: XADD needs pairs: field and value [OK]
Common Mistakes:
Thinking * is invalid for ID
Assuming stream must exist before XADD
Believing XRANGE must run first
5. You want to track user actions in a Redis stream but only keep the last 100 events to save memory. Which command option helps achieve this?
hard
A. XADD mystream MAXLEN ~ 100 * event "login" user "alice"
B. XADD mystream * event "login" user "alice" COUNT 100
C. XADD mystream * event "login" user "alice" MAXLEN 100
D. XADD mystream * event "login" user "alice" LIMIT 100
Solution
Step 1: Understand MAXLEN option in XADD
MAXLEN limits stream length; '~' makes it approximate for better performance.
Step 2: Identify correct syntax for trimming
XADD mystream MAXLEN ~ 100 * event "login" user "alice" uses 'MAXLEN ~ 100' which trims stream to about 100 entries efficiently.
Final Answer:
XADD mystream MAXLEN ~ 100 * event "login" user "alice" -> Option A
Quick Check:
MAXLEN ~ 100 trims stream length [OK]
Hint: Use MAXLEN ~ 100 to trim stream length efficiently [OK]
Common Mistakes:
Using MAXLEN without ~ causes slower trimming
Using COUNT or LIMIT which are invalid here
Not trimming stream and causing memory issues
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.