{
  "schema_version": 2,
  "id": "integrate/spring-framework-cache/data-structures",
  "title": "Use Redis data structures with Spring",
  "url": "https://redis.io/docs/latest/integrate/spring-framework-cache/data-structures/",
  "summary": "Use RedisTemplate operations to work with Redis data structures, such as a sorted set for a leaderboard.",
  "tags": [
    "docs",
    "integrate",
    "stack",
    "oss",
    "rs",
    "rc",
    "oss",
    "client"
  ],
  "last_updated": "2026-11-17T15:08:03Z",
  "page_type": "content",
  "content_hash": "15dcd238293afeb66bd3d318125a002e26de02e663b5e9775e1e3a26bcbf9616",
  "sections": [
    {
      "id": "overview",
      "title": "Overview",
      "role": "overview",
      "text": "Spring Data Redis exposes each Redis data structure through an operations object on a\ntemplate. With the [starter](https://redis.io/docs/latest/integrate/spring-framework-cache#requirements),\nSpring Boot creates a `StringRedisTemplate` that you can inject into any bean. This page\nshows how to use the sorted set operations (`ZSetOperations`) to build a\n[leaderboard](https://redis.io/docs/latest/develop/use-cases/leaderboard).\nSee [Sorted sets](https://redis.io/docs/latest/develop/data-types/sorted-sets) for details of the\nunderlying data type."
    },
    {
      "id": "build-a-leaderboard-with-a-sorted-set",
      "title": "Build a leaderboard with a sorted set",
      "role": "content",
      "text": "Get the `ZSetOperations` object from the template and use it to update and query scores:\n\n[code example]\n\nThe `incrementScore` method adds to a player's score with\n[`ZINCRBY`](https://redis.io/docs/latest/commands/zincrby), creating the player if needed.\nThe `reverseRank` method uses [`ZREVRANK`](https://redis.io/docs/latest/commands/zrevrank), and\n`reverseRangeWithScores` reads the highest scores with a\n[reverse range query](https://redis.io/docs/latest/commands/zrange).\n\nAfter `addScore(\"alice\", 10)`, `addScore(\"bob\", 25)`, and `addScore(\"alice\", 20)`,\n`top(2)` returns alice with a score of 30.0 followed by bob with 25.0, and `rank(\"alice\")`\nreturns 1. The `reverseRank` method returns `null` for a player that isn't in the\nsorted set, and its ranks start at 0, so the example adds 1 to get a 1-based rank."
    },
    {
      "id": "other-data-structures",
      "title": "Other data structures",
      "role": "content",
      "text": "The template provides a similar operations object for each data type, such as\n`opsForValue()`, `opsForHash()`, `opsForList()`, `opsForSet()`, and `opsForStream()`.\nTo store objects rather than strings, define a `RedisTemplate` with the serializers\nyou need. See the\n[Spring Data Redis template documentation](https://docs.spring.io/spring-data/redis/reference/redis/template.html)\nfor details."
    }
  ],
  "examples": [
    {
      "id": "build-a-leaderboard-with-a-sorted-set-ex0",
      "language": "java",
      "code": "@Service\npublic class Leaderboard {\n    private static final String KEY = \"leaderboard:global\";\n\n    private final ZSetOperations<String, String> zset;\n\n    Leaderboard(StringRedisTemplate template) {\n        this.zset = template.opsForZSet();\n    }\n\n    public void addScore(String player, double points) {\n        zset.incrementScore(KEY, player, points);\n    }\n\n    public Set<TypedTuple<String>> top(int n) {\n        return zset.reverseRangeWithScores(KEY, 0, n - 1);\n    }\n\n    public Long rank(String player) {\n        Long rank = zset.reverseRank(KEY, player);\n        return rank == null ? null : rank + 1; // convert 0-based to 1-based\n    }\n}",
      "section_id": "build-a-leaderboard-with-a-sorted-set"
    }
  ]
}
