Search code examples
redisarchitecturebackendnode-redis

How to stored last ~1500 events in Sorted Set in Redis


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:

  1. Simplest is using something like CRON or some interval to clear it
  2. Using some global count and increment it in every request to this table. And clear it when this count are more than 100

or is there another better way?


Solution

  • 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.