Use Redis data structures with Spring

Use RedisTemplate operations to work with Redis data structures, such as a sorted set for a leaderboard.

Spring Data Redis exposes each Redis data structure through an operations object on a template. With the starter, Spring Boot creates a StringRedisTemplate that you can inject into any bean. This page shows how to use the sorted set operations (ZSetOperations) to build a leaderboard. See Sorted sets for details of the underlying data type.

Build a leaderboard with a sorted set

Get the ZSetOperations object from the template and use it to update and query scores:

@Service
public class Leaderboard {
    private static final String KEY = "leaderboard:global";

    private final ZSetOperations<String, String> zset;

    Leaderboard(StringRedisTemplate template) {
        this.zset = template.opsForZSet();
    }

    public void addScore(String player, double points) {
        zset.incrementScore(KEY, player, points);
    }

    public Set<TypedTuple<String>> top(int n) {
        return zset.reverseRangeWithScores(KEY, 0, n - 1);
    }

    public Long rank(String player) {
        Long rank = zset.reverseRank(KEY, player);
        return rank == null ? null : rank + 1; // convert 0-based to 1-based
    }
}

The incrementScore method adds to a player's score with ZINCRBY, creating the player if needed. The reverseRank method uses ZREVRANK, and reverseRangeWithScores reads the highest scores with a reverse range query.

After addScore("alice", 10), addScore("bob", 25), and addScore("alice", 20), top(2) returns alice with a score of 30.0 followed by bob with 25.0, and rank("alice") returns 1. The reverseRank method returns null for a player that isn't in the sorted set, and its ranks start at 0, so the example adds 1 to get a 1-based rank.

Other data structures

The template provides a similar operations object for each data type, such as opsForValue(), opsForHash(), opsForList(), opsForSet(), and opsForStream(). To store objects rather than strings, define a RedisTemplate with the serializers you need. See the Spring Data Redis template documentation for details.

RATE THIS PAGE
Back to top ↑