📖 Scenario: You are managing a Redis stream that collects temperature sensor readings from a smart home system. To keep the stream size manageable and avoid using too much memory, you want to limit the stream to only the latest 5 readings.
🎯 Goal: Build a Redis stream capped at 5 entries using the XTRIM command to automatically remove older entries when new ones are added.
📋 What You'll Learn
Create a Redis stream called sensor:temperature with initial entries.
Set a maximum length variable max_len to 5.
Use the XADD command with the MAXLEN option to add new entries while trimming the stream.
Verify that the stream never exceeds 5 entries after adding new data.
💡 Why This Matters
🌍 Real World
IoT devices and real-time data systems often use Redis streams to collect sensor data. Capping streams prevents memory overflow and keeps data manageable.
💼 Career
Understanding how to cap Redis streams is useful for backend developers and system administrators managing real-time data pipelines and ensuring efficient resource use.
Progress0 / 4 steps
1
Create the initial Redis stream with 3 temperature readings
Use the XADD command to create a Redis stream called sensor:temperature and add these exact entries: {"temp": "22.5"}, {"temp": "23.0"}, and {"temp": "22.8"}. Use the * ID to let Redis generate IDs automatically.
Redis
Hint
Use XADD with * for automatic IDs and specify the field temp with the given values.
2
Set a maximum length variable for the stream
Create a variable called max_len and set it to 5 to represent the maximum number of entries allowed in the stream.
Redis
Hint
Just assign the number 5 to a variable named max_len.
3
Add new entries using XADD with MAXLEN to cap the stream
Use the XADD command with the MAXLEN option set to ~ and the variable max_len to add two new temperature readings: 23.2 and 23.5. Use the * ID for automatic IDs.
Redis
Hint
Use XADD sensor:temperature MAXLEN ~ 5 * followed by the field and value to add entries while trimming.
4
Verify the stream is capped at 5 entries
Use the XLEN command on the sensor:temperature stream to check that it contains exactly 5 entries after adding the new data.
Redis
Hint
Use XLEN sensor:temperature to get the number of entries in the stream.
Practice
(1/5)
1. What does the Redis XTRIM command do when used with the MAXLEN option?
easy
A. It renames the stream to a new key.
B. It adds new entries to the stream without removing any.
C. It limits the number of entries in a stream by removing the oldest ones.
D. It deletes the entire stream immediately.
Solution
Step 1: Understand the purpose of XTRIM with MAXLEN
The XTRIM command with MAXLEN is used to keep the stream size limited by removing old entries.
Step 2: Identify the effect on the stream
It removes the oldest entries to keep the stream length within the specified maximum.
Final Answer:
It limits the number of entries in a stream by removing the oldest ones. -> Option C
Quick Check:
XTRIM + MAXLEN = Remove oldest entries [OK]
Hint: XTRIM with MAXLEN trims oldest entries to cap stream size [OK]
Common Mistakes:
Thinking XTRIM adds entries instead of removing
Confusing XTRIM with deleting the whole stream
Assuming XTRIM renames the stream
2. Which of the following is the correct syntax to trim a Redis stream named mystream to keep at most 1000 entries approximately?
easy
A. XTRIM mystream MAXLEN ~1000
B. XTRIM mystream MAXLEN=1000
C. XTRIM mystream MAXLEN ~ 1000
D. XTRIM mystream MAXLEN 1000
Solution
Step 1: Recall syntax for approximate trimming
Approximate trimming uses the tilde (~) before the number in the XTRIM command.
Step 2: Match correct syntax
The correct syntax is XTRIM mystream MAXLEN ~1000 with spaces separating arguments.
Final Answer:
XTRIM mystream MAXLEN ~1000 -> Option A
Quick Check:
Approximate trim syntax = MAXLEN ~ number [OK]
Hint: Use tilde (~) with MAXLEN for approximate trimming [OK]
Common Mistakes:
Omitting the tilde (~) for approximate trimming
Using equals sign (=) which is invalid syntax
Not spacing arguments correctly
3. Given the following commands executed sequentially:
4. You run the command XTRIM mystream MAXLEN ~1000 but notice the stream still has over 2000 entries. What is the most likely reason?
medium
A. The command syntax is incorrect and was ignored.
B. The approximate trim (~) allows the stream to exceed the limit temporarily.
C. XTRIM only works on empty streams.
D. The stream key name is case-sensitive and was misspelled.
Solution
Step 1: Understand approximate trimming behavior
Using the tilde (~) means trimming is approximate and may allow the stream to temporarily exceed the limit.
Step 2: Eliminate other causes
Syntax is correct, XTRIM works on non-empty streams, and key name issues would cause errors, not approximate trimming behavior.
Final Answer:
The approximate trim (~) allows the stream to exceed the limit temporarily. -> Option B
Quick Check:
Approximate trim = possible temporary size excess [OK]
Hint: Approximate trim (~) may exceed limit briefly [OK]
Common Mistakes:
Assuming syntax error caused no trimming
Thinking XTRIM only works on empty streams
Ignoring approximate trimming behavior
5. You want to keep a Redis stream events capped at 500 entries but want to improve trimming speed by allowing approximate trimming. Which command should you use?
hard
A. XTRIM events MAXLEN = 500
B. XTRIM events MAXLEN 500
C. XTRIM events MINLEN ~ 500
D. XTRIM events MAXLEN ~500
Solution
Step 1: Identify approximate trimming syntax
Approximate trimming uses the tilde (~) before the number with MAXLEN.
Step 2: Choose correct command
XTRIM events MAXLEN ~500 caps the stream at about 500 entries with faster trimming.
Final Answer:
XTRIM events MAXLEN ~500 -> Option D
Quick Check:
Approximate trim = MAXLEN ~ number [OK]
Hint: Use MAXLEN ~ number for fast approximate trimming [OK]
Common Mistakes:
Using MINLEN instead of MAXLEN
Using equals sign (=) which is invalid
Omitting the tilde (~) for approximate trimming
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.