📖 Scenario: You are building a simple event logging system using Redis streams. Each event will be stored as an entry in a Redis stream called event_stream. This will help you keep track of user actions in a web application.
🎯 Goal: Learn how to add entries to a Redis stream using the XADD command. You will create a stream key, set a maximum length for the stream, and add event entries with specific fields.
📋 What You'll Learn
Create a Redis stream key named event_stream.
Set a maximum length of 100 entries for the stream.
Add entries with fields user and action.
Use the XADD command with the MAXLEN option.
Add at least two entries to the stream.
💡 Why This Matters
🌍 Real World
Redis streams are used in real-time event logging, messaging systems, and data pipelines to store ordered data efficiently.
💼 Career
Knowing how to add and manage entries in Redis streams is valuable for backend developers, DevOps engineers, and anyone working with real-time data processing.
Progress0 / 4 steps
1
Create the Redis stream key event_stream
Use the XADD command to create a Redis stream key called event_stream and add the first entry with fields user set to alice and action set to login. Use * for the entry ID to let Redis generate it automatically.
Redis
Hint
Use XADD event_stream * user alice action login to add the first entry.
2
Add a configuration to limit the stream length
Modify the XADD command to add a maximum length of 100 entries to the event_stream using the MAXLEN option with approximate trimming (~). Add a second entry with user set to bob and action set to logout.
Redis
Hint
Use XADD event_stream MAXLEN ~ 100 * user bob action logout to add the second entry with length limit.
3
Add a third entry with the same length limit
Add a third entry to the event_stream using XADD with the same maximum length of 100 entries. Set the user field to carol and the action field to purchase.
Redis
Hint
Use XADD event_stream MAXLEN ~ 100 * user carol action purchase to add the third entry.
4
Complete the stream setup with all entries
Ensure the Redis stream event_stream has three entries added with the XADD command, each using the MAXLEN ~ 100 option for the second and third entries. The entries must have the exact fields and values: first entry with user alice action login, second with user bob action logout, and third with user carol action purchase.
Redis
Hint
Check that all three XADD commands are present with correct fields and options.
Practice
(1/5)
1. What does the Redis command XADD do?
easy
A. Adds a new entry to a Redis stream with a unique ID
B. Deletes an entry from a Redis stream
C. Reads entries from a Redis stream
D. Creates a new Redis database
Solution
Step 1: Understand the purpose of XADD
XADD is used to add new entries to a Redis stream, each with a unique ID generated by Redis or specified by the user.
Step 2: Compare with other commands
Deleting entries is done by XDEL, reading by XRANGE or XREAD, and creating databases is unrelated to XADD.
Final Answer:
Adds a new entry to a Redis stream with a unique ID -> Option A
Quick Check:
XADD adds entries = A [OK]
Hint: XADD always adds entries to streams, not deletes or reads [OK]
Common Mistakes:
Confusing XADD with XDEL or XRANGE
Thinking XADD creates databases
Assuming XADD reads data instead of adding
2. Which of the following is the correct syntax to add an entry with fields name and age to a Redis stream mystream using XADD?
easy
A. XADD mystream name John age 30 *
B. XADD mystream name=John age=30 *
C. XADD mystream * name John age 30
D. XADD mystream * (name John) (age 30)
Solution
Step 1: Recall XADD syntax
The correct syntax is: XADD stream_name * field1 value1 field2 value2 ... The asterisk (*) tells Redis to generate the ID automatically.
Step 2: Check each option
XADD mystream * name John age 30 matches the syntax exactly. Options A, C, and D have incorrect order or formatting.
Final Answer:
XADD mystream * name John age 30 -> Option C
Quick Check:
XADD syntax = XADD stream * fields [OK]
Hint: Use * right after stream name to auto-generate ID [OK]
Common Mistakes:
Placing * after fields instead of immediately after stream name
Using equal signs (=) in field-value pairs
Adding parentheses around fields
3. Given the Redis command: XADD mystream MAXLEN 2 * temperature 22 humidity 60 executed three times in a row, how many entries will mystream contain afterwards?
medium
A. 2 entries
B. 0 entries
C. 1 entry
D. 3 entries
Solution
Step 1: Understand MAXLEN usage
The MAXLEN 2 option limits the stream length to 2 entries by trimming older entries when new ones are added.
Step 2: Apply the command three times
After the first two adds, the stream has 2 entries. The third add triggers trimming, keeping only the latest 2 entries.
Final Answer:
2 entries -> Option A
Quick Check:
MAXLEN 2 limits stream to 2 entries [OK]
Hint: MAXLEN limits entries; stream never exceeds that number [OK]
Common Mistakes:
Assuming all 3 entries remain without trimming
Confusing MAXLEN with deleting all entries
Thinking MAXLEN sets minimum length
4. What is wrong with this command? XADD mystream MAXLEN 5 name John age
medium
A. Stream name cannot be 'mystream'
B. MAXLEN must be after the fields
C. XADD does not support MAXLEN option
D. Missing value for field 'age'
Solution
Step 1: Check field-value pairs
Fields and values must come in pairs. Here, 'age' has no value following it.
Step 2: Verify MAXLEN position and support
MAXLEN is correctly placed after stream name and before fields. XADD supports MAXLEN.
Final Answer:
Missing value for field 'age' -> Option D
Quick Check:
Fields need values; 'age' missing one [OK]
Hint: Every field must have a value; check pairs carefully [OK]
Common Mistakes:
Forgetting to provide value for last field
Misplacing MAXLEN option
Thinking stream name is invalid
5. You want to add multiple entries to a Redis stream sensor_data but keep only the latest 100 entries to save memory. Which command correctly adds a new entry with fields temp and humidity and enforces this limit?
hard
A. XADD sensor_data * MAXLEN 100 temp 25 humidity 40
B. XADD sensor_data MAXLEN 100 * temp 25 humidity 40
D. XADD sensor_data * temp 25 humidity 40 MAXLEN 100
Solution
Step 1: Understand MAXLEN usage and placement
MAXLEN must come immediately after the stream name, optionally with ~ for approximate trimming. The * must come after MAXLEN to auto-generate ID.
Step 2: Analyze options
XADD sensor_data MAXLEN 100 * temp 25 humidity 40 correctly places MAXLEN 100 after stream name and before *, then fields. XADD sensor_data MAXLEN ~ 100 * temp 25 humidity 40 uses ~ (approximate) which is optional but not required. Options B and D have wrong order.