A real-time leaderboard is a system that tracks and ranks users or entities based on scores, achievements, or performance metrics as they update in real time.
Examples include leaderboards in online games, fitness apps, or platforms like Kaggle competitions. Users expect their ranks and positions to update instantly as new scores are submitted.
In a real-time leaderboard, updates happen almost instantly:
This real-time aspect makes the user experience more dynamic and engaging.
However, it also introduces significant technical challenges, such as:
In this chapter, we will walk through the high-level design of a real-time leaderboard system.
Let’s begin by clarifying the requirements.
Before diving into the design, lets clearly define the functional and non-functional requirements of our real-time gaming leaderboard..
The most challenging aspects of building a real-time leaderboard is database design. Choosing the right storage system is critical to ensuring that queries can be executed efficiently without performance bottlenecks.
To simplify the design process, we will follow below approach:
To support real-time leaderboard operations, we define a set of RESTful APIs that allow players to update scores, retrieve rankings, and query nearby ranks efficiently.
Updates a player's score incrementally.
Endpoint: POST /leaderboard/score/update
Request Body (JSON):
Assumption: We will use relative score updates (
scoreDelta) rather than absolute updates.
Response (JSON):
Retrieves the top-N players from the leaderboard, ranked by their scores.
Endpoint: GET /leaderboard/top?n=10
Query Parameter: n = number of top players to fetch (default 10, max 100, etc.)
Response (JSON):
Allows a player to retrieve their current rank without scanning the entire leaderboard.
Endpoint: GET /leaderboard/rank/{playerId}
Response (JSON):
Retrieves players ranked around a given player, allowing for comparison with competitors of similar skill levels.
Endpoint: GET /leaderboard/nearby/{playerId}?range=5
Query Param: range indicates how many ranks above and below to fetch (e.g., 5 above, 5 below).
Response (JSON):
Example: If a player is ranked 50th, this API allows them to see players ranked 45-55 for a competitive comparison.
While REST APIs are good for on-demand queries, WebSockets or Server-Sent Events (SSEs) can push real-time leaderboard updates to subscribed users.
Players would subscribe to leaderboard updates, and receive updates instantly without polling.