Redis Tutorial 0/42 lessons ~6 min read Lesson 11

    Sorted Sets

    Sorted sets (ZSET) combine a unique member with a double score — Redis maintains skip-list + hash index for O(log N) rank, range, and score queries.

    Course progress0%
    Focus
    10 guided sections
    Practice signal
    Examples included
    Career prep
    Interview Q&A included

    Introduction

    Sorted sets (ZSET) combine a unique member with a double score — Redis maintains skip-list + hash index for O(log N) rank, range, and score queries. This is the type behind leaderboards, priority queues, and time-series indexes.

    ZADD updates score; ZREVRANGE returns top-N; ZRANK gives position. No other in-memory store matches this ergonomics for real-time ranking at scale.

    Scores can be timestamps, points, or composite values — members are typically entity IDs.

    Understanding the topic

    Key concepts

    • ZADD key score member — insert or update score.
    • ZREVRANGE key 0 9 WITHSCORES — top 10 descending.
    • ZRANGE BYSCORE — filter by score band.
    • ZINCRBY — atomic score increment.
    • ZRANK/ZREVRANK — position of member.
    • ZREM, ZCARD — delete and count.
    text
    flowchart TB
    ZADD --> SkipList
    SkipList --> ZREVRANGE[Top N query]
    SkipList --> ZRANK[Rank lookup]

    Step-by-step explanation

    1. Each member appears once; score determines order.
    2. Skip list enables log-time rank and range.
    3. Same member re-ZADD updates score in place.
    4. Lex ordering available with special score encoding.
    5. Union/inter store ops merge multiple zsets.

    Syntax reference

    Common commands

    • ZINCRBY — game points, vote counts.
    • ZREVRANGE 0 9 — top 10 leaderboard.
    • Score as Unix timestamp — delayed job queue.
    bash
    ZADD leaderboard 9850 user:1001
    ZINCRBY leaderboard 50 user:1001
    ZREVRANGE leaderboard 0 9 WITHSCORES
    ZRANK leaderboard user:1001

    Informative example

    Leaderboard service with Spring Redis ZSet ops:

    java
    @Service
    public class Leaderboard {
    private final StringRedisTemplate redis;
    public Leaderboard(StringRedisTemplate redis) {
    this.redis = redis;
    }
    public void addScore(String board, String userId, double delta) {
    redis.opsForZSet().incrementScore("lb:" + board, userId, delta);
    }
    public Set<ZSetOperations.TypedTuple<String>> top10(String board) {
    return redis.opsForZSet().reverseRangeWithScores("lb:" + board, 0, 9);
    }
    }

    Trim board with ZREMRANGEBYRANK if millions of players — keep top 10K only.

    Real-world use

    Real-world use cases

    • Gaming leaderboard — ZINCRBY on match end.
    • Twitter timeline rank — score = timestamp.
    • Priority task queue — score = priority or run-at time.
    • Rate limit sliding window — ZADD timestamp member.
    • Trending topics — increment score per mention.

    Best practices

    • Trim large zsets — ZREMRANGEBYRANK remove low ranks.
    • Use member IDs not heavy JSON as members.
    • Paginate with ZREVRANGE start stop not full scan.
    • Composite scores need documented encoding.
    • Replicate read-heavy boards to replicas.
    • Cache top-N in app for sub-ms if needed.

    Common mistakes

    • Millions of members never trimmed.
    • Using member JSON with large payload.
    • ZRANGE entire zset in API hot path.
    • Float score equality bugs in range queries.

    Advanced interview questions

    Q1BeginnerWhat is a sorted set?
    Unique members each with numeric score; sorted by score.
    Q2BeginnerLeaderboard commands?
    ZADD, ZINCRBY, ZREVRANGE WITHSCORES.
    Q3IntermediateZSET internal structure?
    Hash table + skip list per key.
    Q4IntermediateDelayed queue with ZSET?
    Score = execute-at timestamp; worker ZRANGEBYSCORE now poll.
    Q5AdvancedScale leaderboard to 100M users?
    Shard boards; trim; async score aggregation; cache top 100; read replicas.

    Summary

    ZSET = Redis superpower for ranking.

    Ready to mark this lesson complete?Track your journey across the entire course.