{
  "schema_version": 2,
  "id": "develop/use-cases/leaderboard",
  "title": "Redis leaderboard",
  "url": "https://redis.io/docs/latest/develop/use-cases/leaderboard/",
  "summary": "Build ranked leaderboards with Redis sorted sets and user metadata",
  "tags": [
    "docs",
    "develop",
    "stack",
    "oss",
    "rs",
    "rc"
  ],
  "last_updated": "2026-09-16T16:54:57+01:00",
  "children": [
    {
      "id": "develop/use-cases/leaderboard/redis-py",
      "summary": "Implement a Redis leaderboard in Python with redis-py and sorted sets",
      "title": "Redis leaderboard with redis-py",
      "url": "https://redis.io/docs/latest/develop/use-cases/leaderboard/redis-py/"
    },
    {
      "id": "develop/use-cases/leaderboard/nodejs",
      "summary": "Implement a Redis leaderboard in JavaScript with node-redis and sorted sets",
      "title": "Redis leaderboard with node-redis",
      "url": "https://redis.io/docs/latest/develop/use-cases/leaderboard/nodejs/"
    },
    {
      "id": "develop/use-cases/leaderboard/go",
      "summary": "Implement a Redis leaderboard in Go with go-redis and sorted sets",
      "title": "Redis leaderboard with Go",
      "url": "https://redis.io/docs/latest/develop/use-cases/leaderboard/go/"
    },
    {
      "id": "develop/use-cases/leaderboard/java-jedis",
      "summary": "Implement a Redis leaderboard in Java with Jedis and sorted sets",
      "title": "Redis leaderboard with Java and Jedis",
      "url": "https://redis.io/docs/latest/develop/use-cases/leaderboard/java-jedis/"
    },
    {
      "id": "develop/use-cases/leaderboard/java-lettuce",
      "summary": "Implement async and reactive Redis leaderboards in Java with Lettuce and sorted sets",
      "title": "Redis leaderboard with Java (Lettuce)",
      "url": "https://redis.io/docs/latest/develop/use-cases/leaderboard/java-lettuce/"
    },
    {
      "id": "develop/use-cases/leaderboard/dotnet",
      "summary": "Implement a Redis leaderboard in C# with StackExchange.Redis and sorted sets",
      "title": "Redis leaderboard with .NET",
      "url": "https://redis.io/docs/latest/develop/use-cases/leaderboard/dotnet/"
    },
    {
      "id": "develop/use-cases/leaderboard/php",
      "summary": "Implement a Redis leaderboard in PHP with Predis and sorted sets",
      "title": "Redis leaderboard with PHP",
      "url": "https://redis.io/docs/latest/develop/use-cases/leaderboard/php/"
    },
    {
      "id": "develop/use-cases/leaderboard/ruby",
      "summary": "Implement a Redis leaderboard in Ruby with redis-rb and sorted sets",
      "title": "Redis leaderboard with Ruby",
      "url": "https://redis.io/docs/latest/develop/use-cases/leaderboard/ruby/"
    },
    {
      "id": "develop/use-cases/leaderboard/rust",
      "summary": "Implement sync and async Redis leaderboards in Rust with redis-rs and sorted sets",
      "title": "Redis leaderboard with Rust",
      "url": "https://redis.io/docs/latest/develop/use-cases/leaderboard/rust/"
    }
  ],
  "page_type": "content",
  "content_hash": "f3109f1a330b5fae766dc0216480752dee899aa078d74579c1b508c40fa0a6f4",
  "sections": [
    {
      "id": "when-to-use-a-redis-leaderboard",
      "title": "When to use a Redis leaderboard",
      "role": "overview",
      "text": "Use a Redis leaderboard when you need to rank entities — players, products, sellers, stocks — by a score that changes continuously and serve those rankings to users in real time."
    },
    {
      "id": "why-the-problem-is-hard",
      "title": "Why the problem is hard",
      "role": "content",
      "text": "Computing a single entity's rank in a relational database requires an `ORDER BY` across the full table — O(N) at best — and at millions of rows this pushes query time into seconds. Caching the result doesn't help because scores change on every user action, so any TTL-based cache is immediately stale or triggers thundering-herd refreshes on expiry.\n\nLocal in-process data structures break behind load balancers because no single instance holds the complete ranking. A dedicated OLAP or leaderboard service could solve the ranking problem but adds operational overhead for what is fundamentally a single sorted data structure.\n\nThis use case is also distinct from [Redis Streams](https://redis.io/docs/latest/develop/data-types/streams) (which record event history but don't maintain ranked state) and from the probabilistic [Top-K](https://redis.io/docs/latest/develop/data-types/probabilistic/top-k) data structure (which approximates heavy hitters but cannot return exact ranks or neighborhood queries)."
    },
    {
      "id": "what-you-can-expect-from-a-redis-solution",
      "title": "What you can expect from a Redis solution",
      "role": "content",
      "text": "You can:\n\n-   Show any user their exact global rank and the neighbors immediately above and below them, updated on every score change.\n-   Serve top-N pages to thousands of concurrent readers without degrading write performance.\n-   Run daily, weekly, and monthly boards that create and expire themselves automatically.\n-   Aggregate time-windowed boards into composite rankings without application-side sorting.\n-   Support millions of ranked members in roughly 500 MB of RAM.\n-   Add leaderboard functionality to an existing deployment without provisioning new infrastructure."
    },
    {
      "id": "how-redis-supports-the-solution",
      "title": "How Redis supports the solution",
      "role": "content",
      "text": "In practice, the leaderboard is stored as a Redis sorted set — one key per board — with members keyed by entity ID and scored by the value you want to rank on. A companion hash per entity holds metadata (name, avatar, tier) so the sorted set stays lightweight.\n\nRedis provides the following features that make it a good fit for leaderboards:\n\n-   [Sorted sets](https://redis.io/docs/latest/develop/data-types/sorted-sets) maintain rank order automatically, with\n    [`ZADD`](https://redis.io/docs/latest/commands/zadd), [`ZRANGE`](https://redis.io/docs/latest/commands/zrange), and\n    [`ZREVRANK`](https://redis.io/docs/latest/commands/zrevrank) all running in O(log N) regardless of set size.\n-   [`ZINCRBY`](https://redis.io/docs/latest/commands/zincrby) updates scores atomically in place — no\n    read-modify-write cycle and no cache invalidation.\n-   [`ZRANGE`](https://redis.io/docs/latest/commands/zrange) with `REV` and `LIMIT` serves both top-N and\n    \"around me\" neighborhood queries in a single command.\n-   [`EXPIRE`](https://redis.io/docs/latest/commands/expire) on a per-window key cleans up daily, weekly,\n    and monthly boards automatically, and\n    [`ZUNIONSTORE`](https://redis.io/docs/latest/commands/zunionstore) aggregates windows without\n    application-level coordination.\n-   [Hashes](https://redis.io/docs/latest/develop/data-types/hashes) store entity metadata so the sorted set\n    stays small and fast.\n-   Sub-millisecond latency on reads and writes means the leaderboard sits on the request path\n    without adding meaningful delay. If Redis is already in the stack, the leaderboard adds zero\n    marginal infrastructure."
    },
    {
      "id": "ecosystem",
      "title": "Ecosystem",
      "role": "content",
      "text": "The following libraries and frameworks provide Redis sorted set integrations suitable for leaderboards:\n\n-   **Java**:\n    [Spring Data Redis (`ZSetOperations`)](https://docs.spring.io/spring-data/redis/reference/redis/template.html)\n-   **Python**: [redis-py](https://redis.readthedocs.io/),\n    [redis-om-python](https://github.com/redis/redis-om-python)\n-   **Node.js**: [ioredis](https://github.com/redis/ioredis),\n    [node-redis](https://github.com/redis/node-redis)\n-   **Infrastructure**: [Redis Cloud](https://redis.io/docs/latest/operate/rc)\n    (Active-Active for multi-region with conflict-free replicated sorted sets)"
    },
    {
      "id": "code-examples-to-build-your-own-redis-leaderboard",
      "title": "Code examples to build your own Redis leaderboard",
      "role": "example",
      "text": "The following guides show how to build a simple Redis-backed leaderboard.\nEach guide includes a runnable interactive demo for each of the following client libraries:\n\n* [redis-py (Python)](https://redis.io/docs/latest/develop/use-cases/leaderboard/redis-py)\n* [node-redis (Node.js)](https://redis.io/docs/latest/develop/use-cases/leaderboard/nodejs)\n* [go-redis (Go)](https://redis.io/docs/latest/develop/use-cases/leaderboard/go)\n* [Jedis (Java)](https://redis.io/docs/latest/develop/use-cases/leaderboard/java-jedis)\n* [Lettuce (Java)](https://redis.io/docs/latest/develop/use-cases/leaderboard/java-lettuce)\n* [StackExchange.Redis (C#)](https://redis.io/docs/latest/develop/use-cases/leaderboard/dotnet)\n* [Predis (PHP)](https://redis.io/docs/latest/develop/use-cases/leaderboard/php)\n* [redis-rb (Ruby)](https://redis.io/docs/latest/develop/use-cases/leaderboard/ruby)\n* [redis-rs (Rust)](https://redis.io/docs/latest/develop/use-cases/leaderboard/rust)"
    }
  ],
  "examples": []
}
