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?
To build leaderboard using Relational database, mainly we need two details:
- 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.
- 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.
- Rank Calculation: To calculate the rank of specific player requires to compute ranks for all positions which will take more time to process.
- 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.
- 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 is an in-memory data store (Can be configured to persist data to a disk) and also it provides speed in a high load environment which is much better than relational databases. We will be using Redis sorted sets to build an efficient and scalable leaderboard.
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:
Let’s try to build Orange Cap leaderboard. Below is the sample list of players which I will be inserting into Redis.
ZADD : Add one/more member for a given score to ZSET/initialize a ZSET with one/more member
> ZADD ipl:orange-cap 200 klrahul 190 dhawan 220 kohli 199 watson 189 dhoni 219 gayle 218 magarwal
ZRANGEBYSCORE : Fetches items in the ZSET based on range of scores in the ascending sorted order of scores
> ZRANGEBYSCORE ipl:orange-cap 100 250 WITHSCORES
ZREVRANGEBYSCORE : Fetches items in the ZSET based on range of scores in the descending sorted order of scores
> ZREVRANGEBYSCORE ipl:orange-cap +inf -inf WITHSCORES
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.
> ZREVRANGE ipl:orange-cap 0 5 WITHSCORES
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.
> ZREVRANK ipl:orange-cap kohli > ZREVRANK ipl:orange-cap gayle > ZREVRANK ipl:orange-cap magarwal > ZREVRANK ipl:orange-cap klrahul
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)
> ZINCRBY ipl:orange-cap 2 gayle > ZREVRANK ipl:orange-cap kohli > ZREVRANK ipl:orange-cap gayle
All the above Redis operations can be utilized to design leaderboards in a fast and scalable manner. Redis is obviously optimized 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 !!!
Thank you for reading
Hope you find these resources useful. If you like what you read and want to see more about system design, microservices, and other technology-related stuff... You can follow me on
- Twitter here.
- Subscribe to my newsletter above.