Demo: a leaderboard with sorted sets
On This Lesson 3 topics
This lesson is planned. The outline below lists the topics it will cover. The full lesson has not been published yet.
In this lesson
build a "top products this week" board. Prepares the data-structure selection scenarios.
Draft outline details
Step by step
- Create key
shop:top:week:2026-W44and record sales withZINCRBY key 1 productId. - Read the top 10 with
ZRANGE key 0 9 REV WITHSCORES. - Wrap it in a Spring
StringRedisTemplate.opsForZSet()service. - Set
EXPIREof two weeks so old boards disappear. - Planned mistake: store the board as a JSON string and rewrite it on each sale; two concurrent sales overwrite each other. Fix:
ZINCRBY, which is atomic. - Show rank lookup with
ZREVRANK.
Check your understanding
Why is ZINCRBY better than read-modify-write?
Show answer
It is atomic; no lost updates.
Practice
Apply this lesson with the module's lab, exercises and quiz.
Marking a lesson complete is optional and you can undo it any time. Your progress stays in this browser.