Advertisement
Intermediate Redis, Streams

How do you limit the size of a Redis Stream to prevent it from growing indefinitely and consuming unlimited memory?

Short answer

You use the MAXLEN option when adding entries with XADD, either specifying an exact maximum length or using the approximate trimming option for better performance, which automatically removes the oldest entries once the stream exceeds the specified length, keeping its memory…

Save Redis mock interview
Advertisement
AModel answer
You use the MAXLEN option when adding entries with XADD, either specifying an exact maximum length or using the approximate trimming option for better performance, which automatically removes the oldest entries once the stream exceeds the specified length, keeping its memory usage bounded over time.
Example

XADD mystream MAXLEN '~' 10000 '*' field 'value'

Real-world example
A high volume logging system caps its stream at approximately ten thousand entries, automatically discarding the oldest log entries as new ones arrive, preventing the stream from consuming unbounded memory over time.
Likely follow-ups
  • What is the difference between exact and approximate trimming for performance reasons?
  • How do you decide an appropriate maximum length for a specific stream's use case?
Related topics