Typorithm · medium

Sorted Sets (Leaderboards)

redis-sorted-setsrediszsetscoreleaderboard
Saved on this device
Full lesson · 8 learning steps Open any exercise for focused practice
1

Step 1 of 8 · intro card

Learn Sorted Sets (Leaderboards)

0%

Data Types

A Redis sorted set keeps members ordered by a float score, making it the natural structure for leaderboards where rank follows the score.

A sorted set (zset) stores unique members each paired with a float score, and Redis keeps them ordered by that score. ZADD inserts a member with its score.

ZINCRBY adds to an existing member's score atomically — here bumping bob by 5. ZRANGE board 0 -1 returns members in ascending score order, and WITHSCORES includes each score alongside the member.

Rank queries make leaderboards trivial: ZREVRANK returns a member's position counting from the top (highest score = rank 0), so you can show where a player stands instantly.

01

Members are ordered by a float score.

02

ZADD adds; ZINCRBY bumps a score.

03

ZREVRANK gives rank from the top.

ZADD board 100 "ada"
ZADD board 80 "bob"
ZINCRBY board 5 "bob"
ZRANGE board 0 -1 WITHSCORES
ZREVRANK board "ada"

Read the implementation

Connect each part of the code to the concept before you type.

1. Add with score

ZADD inserts a member with a float score, keeping the set ordered by that score.

ZADD board 100 "ada"

2. Another member

Each member has its own score; bob enters with 80, ranking below ada's 100.

ZADD board 80 "bob"

3. Bump score

ZINCRBY adds to a member's existing score, here raising bob from 80 to 85.

ZINCRBY board 5 "bob"

4. List by rank

ZRANGE returns members by ascending rank; WITHSCORES appends each member's score.

ZRANGE board 0 -1 WITHSCORES

5. Rank from top

ZREVRANK gives a member's 0-based position counting from the highest score down.

ZREVRANK board "ada"

A complete learning loop

01

Understand

Read the concept map and follow the code breakdown.

02

Reconstruct

Type the implementation, repair mistakes and work through every recall exercise.

03

Verify

Use the final check without hints; revisit weak concepts in short practice sessions.

Sorted Sets (Leaderboards): Lesson & Practice · Typorithm | ApplyDjinn