Search code examples
databaseredisreal-time

Interval dependent leaderboard


I am trying to build a leaderboard for keeping track of how many times a certain event has occurred in the last X minutes. There will be multiple timeframes (1min, 5min, 15min, 30min, and 60min) and I will then query the top 10 events from each one of these leaderboards.

From my initial research, I thought I would go with Redis and sorted sets but I'm not sure if that's optimal? I came across this solution based on which I would need to have one key for each minute but that seemed a little tedious.

What would be the best way to approach this? Appreciate any input :)


Solution

  • For anyone that might find this in the future, this is what I've ended doing:

    Have one helper sorted set per event type, which is holding timestamps as both Member & Score (for easy filtering no longer relevant timestamps). Abstracted to something like:

    { "event name": [ timestamps ] }
    

    Then have the main sorted set per timeframe (this is the leaderboard), holding event names as Member and occurrence count (in the relevant timeframe) as the Score. Abstracted to something like:

    { "event name": count }
    

    every time new events get processed, I then filter out expired timestamps from relevant helper set and update the count in each of the leaderboards.