Bird
Raised Fist0
Redisquery~10 mins

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

Choose your learning style10 modes available
Concept Flow - XTRIM for stream capping
Start with Redis Stream
↓
Add new entries to stream
↓
Check stream length
↓
Is length > max length?
No→Continue 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.

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

  1. 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.
  2. Step 2: Identify the effect on the stream

    It removes the oldest entries to keep the stream length within the specified maximum.
  3. Final Answer:

    It limits the number of entries in a stream by removing the oldest ones. -> Option C
  4. 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

  1. Step 1: Recall syntax for approximate trimming

    Approximate trimming uses the tilde (~) before the number in the XTRIM command.
  2. Step 2: Match correct syntax

    The correct syntax is XTRIM mystream MAXLEN ~1000 with spaces separating arguments.
  3. Final Answer:

    XTRIM mystream MAXLEN ~1000 -> Option A
  4. 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:
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?
medium
A. 2 entries
B. 3 entries
C. 1 entry
D. 0 entries

Solution

  1. Step 1: Count entries added before trimming

    Three entries are added to mystream using XADD.
  2. Step 2: Apply XTRIM with MAXLEN 2

    The XTRIM command trims the stream to keep only the latest 2 entries, removing the oldest one.
  3. Final Answer:

    2 entries -> Option A
  4. Quick Check:

    3 added, trimmed to 2 = 2 entries [OK]
Hint: XTRIM MAXLEN keeps newest entries, removes oldest [OK]
Common Mistakes:
  • Assuming no trimming happens after XTRIM
  • Counting all added entries without trimming
  • Thinking XTRIM deletes all entries
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

  1. Step 1: Understand approximate trimming behavior

    Using the tilde (~) means trimming is approximate and may allow the stream to temporarily exceed the limit.
  2. 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.
  3. Final Answer:

    The approximate trim (~) allows the stream to exceed the limit temporarily. -> Option B
  4. 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

  1. Step 1: Identify approximate trimming syntax

    Approximate trimming uses the tilde (~) before the number with MAXLEN.
  2. Step 2: Choose correct command

    XTRIM events MAXLEN ~500 caps the stream at about 500 entries with faster trimming.
  3. Final Answer:

    XTRIM events MAXLEN ~500 -> Option D
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard