XTRIM for stream capping in Redis - Time & Space Complexity
When using Redis streams, controlling their size is important to keep performance steady.
We want to understand how the time to trim a stream grows as the stream gets bigger.
Analyze the time complexity of the following Redis command.
XTRIM mystream MAXLEN 1000
XTRIM mystream MAXLEN ~ 1000
XTRIM mystream MINID 1526985058136-0
XTRIM mystream MINID ~ 1526985058136-0
This code trims a Redis stream to keep it within a certain length or minimum ID, optionally using approximate trimming.
Look for repeated work inside the trimming process.
- Primary operation: Removing entries from the stream one by one until the limit is met.
- How many times: Up to the number of entries exceeding the limit, which depends on stream size.
As the stream grows larger beyond the limit, more entries need to be removed.
| Input Size (entries over limit) | Approx. Operations (entries removed) |
|---|---|
| 10 | 10 |
| 100 | 100 |
| 1000 | 1000 |
Pattern observation: The work grows linearly with how many entries must be trimmed.
Time Complexity: O(n)
This means the time to trim grows directly with the number of entries removed from the stream.
[X] Wrong: "XTRIM always runs in constant time no matter the stream size."
[OK] Correct: The command must remove each extra entry, so more entries mean more work and longer time.
Understanding how trimming scales helps you manage data size and performance in real Redis applications.
"What if we use the approximate (~) flag with XTRIM? How might that affect the time complexity?"
Practice
XTRIM command do when used with the MAXLEN option?Solution
Step 1: Understand the purpose of XTRIM with MAXLEN
TheXTRIMcommand withMAXLENis 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 CQuick Check:
XTRIM + MAXLEN = Remove oldest entries [OK]
- Thinking XTRIM adds entries instead of removing
- Confusing XTRIM with deleting the whole stream
- Assuming XTRIM renames the stream
mystream to keep at most 1000 entries approximately?Solution
Step 1: Recall syntax for approximate trimming
Approximate trimming uses the tilde (~) before the number in theXTRIMcommand.Step 2: Match correct syntax
The correct syntax isXTRIM mystream MAXLEN ~1000with spaces separating arguments.Final Answer:
XTRIM mystream MAXLEN ~1000 -> Option AQuick Check:
Approximate trim syntax = MAXLEN ~ number [OK]
- Omitting the tilde (~) for approximate trimming
- Using equals sign (=) which is invalid syntax
- Not spacing arguments correctly
XADD mystream * field1 value1 XADD mystream * field2 value2 XADD mystream * field3 value3 XTRIM mystream MAXLEN 2
How many entries will
mystream contain after these commands?Solution
Step 1: Count entries added before trimming
Three entries are added tomystreamusingXADD.Step 2: Apply XTRIM with MAXLEN 2
TheXTRIMcommand trims the stream to keep only the latest 2 entries, removing the oldest one.Final Answer:
2 entries -> Option AQuick Check:
3 added, trimmed to 2 = 2 entries [OK]
- Assuming no trimming happens after XTRIM
- Counting all added entries without trimming
- Thinking XTRIM deletes all entries
XTRIM mystream MAXLEN ~1000 but notice the stream still has over 2000 entries. What is the most likely reason?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 BQuick Check:
Approximate trim = possible temporary size excess [OK]
- Assuming syntax error caused no trimming
- Thinking XTRIM only works on empty streams
- Ignoring approximate trimming behavior
events capped at 500 entries but want to improve trimming speed by allowing approximate trimming. Which command should you use?Solution
Step 1: Identify approximate trimming syntax
Approximate trimming uses the tilde (~) before the number withMAXLEN.Step 2: Choose correct command
XTRIM events MAXLEN ~500caps the stream at about 500 entries with faster trimming.Final Answer:
XTRIM events MAXLEN ~500 -> Option DQuick Check:
Approximate trim = MAXLEN ~ number [OK]
- Using MINLEN instead of MAXLEN
- Using equals sign (=) which is invalid
- Omitting the tilde (~) for approximate trimming
Ready to prove it?
Pass the test and this pattern moves unknown
