Skip to main content

Command Palette

Search for a command to run...

Real-Time Leaderboard with Redis Sorted Sets

Published
•2 min read
Real-Time Leaderboard with Redis Sorted Sets
P

đź‘‹ Hi there! I'm a Software Engineer with a passion for building scalable solutions in the internet industry. With expertise in Data Structures, Algorithms, Distributed Systems and Event-driven architecture, I thrive on crafting distributed software systems and scalable databases.

I enjoy delving into how leading companies architect solutions to meet client needs, constantly learning and applying new insights to my work. Let's connect and discuss the latest in software engineering and architecture!

Introduction

In the realm of football gaming, where excitement and competition reign supreme, keeping players engaged requires dynamic features like real-time leaderboards. Redis, a robust in-memory data store, provides the tools needed to create and manage leaderboards efficiently. In this guide, we’ll dive into the world of Redis sorted sets and explore how they can elevate your football game with real-time leaderboards featuring famous player names.

Understanding Redis Sorted Sets

Redis sorted sets are like the MVPs of data structures when it comes to storing and sorting data. Picture them as the referees of your game, keeping track of players and their scores. These sets store a collection of unique members, each associated with a score. What sets them apart is their ability to sort these members based on their scores, making retrieval of top-ranked items a breeze. This makes sorted sets the perfect choice for scenarios like low-latency leaderboards.

Creating a Leaderboard

To kick off our leaderboard creation, we first initialize a Redis sorted set key with the name “player:leaderboard”. We add players to this set along with their initial scores using the ZADD command. Think of it as drafting players into the league. For example:

ZADD player:leaderboard 0 "Lionel Messi"

Here, we add Lionel Messi with an initial score of 0. The structure is simple: “player:leaderboard” is the key, 0 is the score, and “Lionel Messi” is the member.

Updating Scores in Real-Time

Just like in a real football match, players’ scores can change in an instant. We simulate this real-time action by incrementing a player’s score using the ZINCRBY command. For instance:

ZINCRBY player:leaderboard 500 "Cristiano Ronaldo"

This command elevates Cristiano Ronaldo’s score by 500 points.

Displaying Leaderboard Rankings

Now, it’s time to showcase the top players in our leaderboard. We utilize the ZREVRANGE command, which retrieves a range of members in descending order of score. For example:

ZREVRANGE player:leaderboard 0 4 WITHSCORES

Output:

1) Cristiano Ronaldo 1000
2) Lionel Messi 700
3) Neymar Jr. 600
4) Robert Lewandowski 550
5) Kylian Mbappe 500

This command displays the top 5 players with their associated scores.

Checking Player Rank

Want to know where your favorite player stands? You can determine a player’s rank in the leaderboard using the ZREVRANK command. For instance:

ZREVRANK player:leaderboard "Cristiano Ronaldo"

Output:

0

This means Cristiano Ronaldo holds the top spot in the leaderboard.

Conclusion

Redis sorted sets offer a comprehensive solution for creating real-time leaderboards. By leveraging commands like ZADD, ZINCRBY, ZREVRANGE, and ZREVRANK, developers can efficiently manage player rankings and scores.