#1
Many modern applications require ranking systems, such as game leaderboards, trending products, or top-performing users. Redis provides a powerful data structure called Sorted Sets, which makes it easy to build real-time ranking systems.
In this article, you will learn how to implement a leaderboard using Redis Sorted Sets in Spring Boot.

1. What Are Redis Sorted Sets

A Sorted Set (ZSET) stores unique elements with a numeric score. Redis automatically sorts elements based on the score.
Example leaderboard:
leaderboard
 ├── Alice : 1500
 ├── Bob   : 1200
 └── Carol : 900
Features:
  • Automatic ranking
  • Fast score updates
  • Efficient top-N queries

2. Adding Players to the Leaderboard

Use ZADD to add players and scores.
Example service:
@Service
public class LeaderboardService {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    private static final String LEADERBOARD = "game-leaderboard";

    public void addScore(String player, double score) {
        redisTemplate.opsForZSet().add(LEADERBOARD, player, score);
    }
}
Example usage:
Player: Alice
Score: 1500
Redis automatically places the player in the correct rank.

3. Updating Player Scores

Scores can change frequently in real-time systems.
public void updateScore(String player, double score) {
    redisTemplate.opsForZSet().incrementScore(LEADERBOARD, player, score);
}
This increases the player's score while maintaining the ranking order.

4. Getting Top Players

To retrieve the top players:
public Set<String> getTopPlayers(int limit) {
    return redisTemplate.opsForZSet()
            .reverseRange(LEADERBOARD, 0, limit - 1);
}
Example result:
1. Alice
2. Bob
3. Carol
reverseRange returns players with the highest scores first.

5. Getting Player Rank

You can also retrieve a specific player's ranking.
public Long getPlayerRank(String player) {
    return redisTemplate.opsForZSet()
            .reverseRank(LEADERBOARD, player);
}
Example:
Alice rank: 1
Bob rank: 2
Ranks start from zero, so you may add +1 for display.

6. Removing Players

If a player leaves the system, remove them from the leaderboard.
public void removePlayer(String player) {
    redisTemplate.opsForZSet().remove(LEADERBOARD, player);
}
Redis updates the ranking automatically.

7. Exposing Leaderboard via REST API

Example controller:
@RestController
@RequestMapping("/leaderboard")
public class LeaderboardController {

    @Autowired
    private LeaderboardService leaderboardService;

    @PostMapping("/score")
    public String addScore(@RequestParam String player,
                           @RequestParam double score) {
        leaderboardService.addScore(player, score);
        return "Score updated";
    }

    @GetMapping("/top")
    public Set<String> getTopPlayers() {
        return leaderboardService.getTopPlayers(10);
    }
}
Example request:
GET /leaderboard/top>
This returns the top 10 ranked players.

8. Scaling Leaderboards

Redis Sorted Sets scale well because operations are optimized with O(log N) complexity.
For large systems:
  • Use Redis clusters
  • Separate leaderboards by category
  • Set expiration for temporary rankings
Example key structure:
leaderboard:daily
leaderboard:weekly
leaderboard:monthly
#ads

image quote pre code