Build the mental model
A sorted set keeps unique members ordered by floating-point scores. `ZINCRBY` updates scores atomically; `ZRANK` or `ZREVRANK` returns position; score or rank ranges return leaders. Define tie behavior, score precision, season rollover, and member-removal policy up front.
Connect it to a real scenario
Store weekly completed-lesson counts in `leaderboard:lessons:2026-W35` and return the top ten in descending order. PostgreSQL completion events remain the source of truth so the leaderboard can be rebuilt. If the weekly key expires, retain it longer than the reporting requirement.
Try the working example
ZINCRBY leaderboard:lessons:2026-W35 1 user:42
ZINCRBY leaderboard:lessons:2026-W35 3 user:7
ZINCRBY leaderboard:lessons:2026-W35 2 user:9
ZREVRANGE leaderboard:lessons:2026-W35 0 9 WITHSCORES
ZREVRANK leaderboard:lessons:2026-W35 user:42
EXPIRE leaderboard:lessons:2026-W35 7776000You get a weekly leaderboard with top users, scores, and user 42's rank.5-minute try-it
Build a course-specific leaderboard and document product behavior for equal-score ties.
One important caution
Do not use sorted-set floating scores as the source of truth for money or exact decimal accounting.
Redis — Sorted Sets — Redis