spring-data-redis icon indicating copy to clipboard operation
spring-data-redis copied to clipboard

Support for approximate trimming in XAddOptions

Open TunaYagci opened this issue 3 years ago • 2 comments

XADD command offers another option called approximateTrimming along with MAXLEN

About XADD mystream MAXLEN ~ 1000 in this link

However trimming with MAXLEN can be expensive: streams are represented by macro nodes into a radix tree, in order to be very memory efficient. Altering the single macro node, consisting of a few tens of elements, is not optimal. So it's possible to use the command in the following special form:

XADD mystream MAXLEN ~ 1000 * ... entry fields here ...

The ~ argument between the MAXLEN option and the actual count means, I don't really need this to be exactly 1000 items. It can be 1000 or 1010 or 1030, just make sure to save at least 1000 items. With this argument, the trimming is performed only when we can remove a whole node. This makes it much more efficient, and it is usually what you want.

Lettuce driver supports with this issue: #https://github.com/lettuce-io/lettuce-core/issues/846

It would be nice to chain it with maxlen:

RedisStreamCommands.XAddOptions
  .maxlen(STREAM_LENGHT)
  .approximateTrimming(true)

also for Reactive:

ReactiveStreamCommands.AddStreamRecord
.maxlen(STREAM_LENGTH)
.approximateTrimming(true)

we can check this option in: org.springframework.data.redis.connection.lettuce.LettuceStreamCommands#xAdd

TunaYagci avatar Aug 12 '21 13:08 TunaYagci

Thanks for your suggestion. Would you be interested in contributing to Spring Data by submitting a pull request?

mp911de avatar Sep 20 '21 14:09 mp911de

@mp911de Yes, why not. I'm interested with it but cannot promise to any date yet.

TunaYagci avatar Sep 20 '21 14:09 TunaYagci

Fixed via #2247

mp911de avatar Apr 24 '23 12:04 mp911de