How to create Realtime IPL Leaderboards using Redis

IPL Orange Cap Leaderboard Using Redis

In this article, I will be writing in detail on how to create IPL leaderboards using Redis. Below is the leaderboard which I will be creating

  • Orange Cap Leaderboard — Shows Top X Batsman with the highest number of runs

Why not use a Relational database?

  • Player Id (Unique Id/name)
  • Player’s Score (Wickets/ Runs)

The above relational database design will work fine if we have less number of users. Normally for leaderboards, every user constantly checks the ranking of the players along with the score. Calculation of ranking is fairly easy with a relational database, but as the number of users increases to millions of users, we will start facing scaling issues

  1. Query to fetch players data can take tens of seconds when the number of players increases in a large table as we need to scan millions of rows.
  2. Rank Calculation: To calculate the rank of specific player requires to compute ranks for all positions which will take more time to process.
  3. Realtime updates: Databases need to handle realtime updates of player scores like runs/wickets etc..where relational databases generally don’t perform better in this case.
  4. High Reads: Many Users will be visiting your website to see the leaderboards and positions of players. Reads will be in millions or tens of millions for popular tournaments like IPL. (We can add caching layer in between and update the player rankings frequently but it may result in showing delayed ranks/scores and also there is a possibility of showing stale data)

So, a relational database is not great choice for real time leaderboards.

Redis Sorted Sets:

Redis Sorted Sets are, similarly to Redis Sets, non repeating collections of Strings. The difference is that every member of a Sorted Set is associated with score, that is used in order to take the sorted set ordered, from the smallest to the greatest score. While members are unique, scores may be repeated.

You can find more about Redis Sorted Sets here.

In the Redis sorted set, new elements can be added or removed from the middle. Moreover, we can also quickly calculate the rank of a specific member. All these features make Sorted set great fit for leaderboards where we can calculate ranks of players in real time and also gives reasonable performance.

Orange Cap Leaderboard:

ZADD : Add one/more member for a given score to ZSET/initialize a ZSET with one/more member

ZRANGEBYSCORE : Fetches items in the ZSET based on range of scores in the ascending sorted order of scores

ZREVRANGEBYSCORE : Fetches items in the ZSET based on range of scores in the descending sorted order of scores

ZREVRANGE : Fetches all the items in the ZSET from their position in sorted order (descending order of scores)

  • Let’s fetch top 6 players with maximum runs. Dhoni is missing from the list as he is the one with least score and is not in top 6 players.

ZREVRANK : Returns the position of the given member based on his score in ZSET (Starting with 0 as first rank) in descending order of scores.

ZINCRBY : Increments the score of a member in the ZSET

  • Kohli Score is 220 & Gayle Score is 219
  • Let’s add 2 runs to gayle score using ZINCRBY now gayle score is 221.
  • Now when we fetch the rank of gayle he is in the first place of Orange Cap leaderboard and Kohli moved to second place. (ZREVRANK follows 0th index)

All the above Redis operations can be utilized to design leaderboards in a fast and scalable manner. Redis is obviously optimised for faster reads which can easily scale for millions of reads. If you have One million Players to rank in your leader board you may need approximately ~40 MB of RAM for Redis.

Hope you enjoyed the article !!!

Learning how to surf the waves of technology.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store