I am store ~1500 last events in Redis in soreted set. When set is more than 1500 elements, which is better (least loaded) way to clear first events?
Im using redis:6.0.2
Several options come to mind:
or is there another better way?
You can use ZREMRANGEBYRANK to remove the rest of the elements, that is,
ZREMRANGEBYRANK key 0 -1501
Whether to execute this command periodically, after each insertion, or after each k insertions - depends on your use case.
Generally speaking, trimming each k insertions is simpler to implement: you don't need an extra thread. In addition, the set size is also more predictable when new elements are added at a non-constant rate.
Periodic trimming is also a valid option, given a more-or-less constant insertion rate (otherwise, the set may grow unnecessarily). A theoretical advantage is saving counter increments, but if you allow the set to grow unnecessarily - the performance penalty would likely be higher.