Implement a Leaderboard Feature in a Game

Build on the fundamentals of the set and sorted set data types with the help of a practical application.

Application overview

This application exposes REST endpoints for managing a hypothetical game with multiple players and tracking the top players by score on a leaderboard.

We can interact with the application by executing the following operations:

  • Add/register a user in the game by sending an HTTP POST request: This is done by first checking if the user already exists in the set using a SIsMember and invoking SAdd if they don’t. If the user already exists, an error response is returned.

  • Start a game simulation where registered players score and accrue points: During the simulation, all game players are queried from the set using SMembers. Each player is added to a sorted set using the ZIncrBy method, along with the user name and the randomly generated score.

  • Query the top five players and their scores at a given point in time: The ZRevRangeWithScores method is used to make sure that the names of the top players and their scores are returned in descending order.

Get hands-on with 1200+ tech skills courses.