0
0
Redisquery~10 mins

XTRIM for stream capping in Redis - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - XTRIM for stream capping
Start with Redis Stream
Add new entries to stream
Check stream length
Is length > max length?
NoContinue adding entries
Yes
Trim stream to max length
Stream capped at max length
Wait for next entry to add
XADD adds new entries to a Redis stream and XTRIM trims it to keep the stream length within a maximum limit.
Execution Sample
Redis
XADD mystream * field1 value1
XADD mystream * field2 value2
XTRIM mystream MAXLEN 2
Add two entries to a stream and then trim it to keep only the latest 2 entries.
Execution Table
StepCommandStream Length BeforeActionStream Length AfterResult
1XADD mystream * field1 value10Add entry1Entry added, stream length is 1
2XADD mystream * field2 value21Add entry2Entry added, stream length is 2
3XTRIM mystream MAXLEN 22Trim stream to max length 22Stream length capped at 2, no entries removed
4XADD mystream * field3 value32Add entry3Entry added, stream length is 3
5XTRIM mystream MAXLEN 23Trim stream to max length 22Oldest entry removed, stream length back to 2
💡 Stream length is maintained at max length 2 after trimming
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5
Stream Length012232
Key Moments - 2 Insights
Why does the stream length increase to 3 after adding the third entry before trimming?
Because XADD adds the entry first, increasing length to 3, then XTRIM trims it back to max length 2 as shown in steps 4 and 5.
Does XTRIM remove entries if the stream length is already at or below max length?
No, as shown in step 3, when length is 2 and max length is 2, XTRIM does not remove any entries.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the stream length after step 2?
A1
B3
C2
D0
💡 Hint
Check the 'Stream Length After' column in row for step 2.
At which step does the stream length first exceed the max length of 2?
AStep 3
BStep 4
CStep 5
DStep 2
💡 Hint
Look at the 'Stream Length After' column and see when it becomes 3.
If we change MAXLEN to 3, what would be the stream length after step 5?
A3
B4
C2
D1
💡 Hint
With MAXLEN 3, trimming won't remove entries when length is 3, see step 5 logic.
Concept Snapshot
XTRIM trims a Redis stream to a max length.
Syntax: XTRIM key MAXLEN maxlen
Adds entries with XADD.
If stream length > maxlen, oldest entries removed.
Keeps stream size manageable.
Full Transcript
This visual execution shows how Redis XTRIM works to cap stream length. We start with an empty stream. Each XADD command adds an entry, increasing stream length. When the length exceeds the max length set by XTRIM, the oldest entries are removed to keep the stream capped. The execution table tracks each step, showing stream length before and after commands. Key moments clarify that XTRIM only removes entries if length exceeds max length, and that adding entries happens before trimming. The quiz tests understanding of stream length changes at each step. This helps beginners see how XTRIM manages stream size in Redis.