Quick revision
The short answer
Use one sorted set per period, lb:all, lb:day:2026-11-12 and lb:week:2026-W46, and update all three in one pipeline or script on each score event, giving period keys an EXPIRE a few days after the period ends. Build the weekly board incrementally rather than with ZUNIONSTORE over seven daily keys, which is O(N) plus O(M log M) on tens of millions of members and blocks the server. The around-me view is ZREVRANK followed by ZRANGE from rank - 10 to rank + 10 with REV, both O(log N) plus 21 members. Expect very roughly 80 to 120 bytes per member plus the member string, so several GB for the all-time board, and keep scores in a durable store so boards can be rebuilt.
What to remember
In this answer, we designed one sorted set per period with the period in the key, updated all boards per score event in one pipeline, and set expiries a few days after each period ends. We avoided ZUNIONSTORE on large boards because it blocks Redis, built the around-me view from ZREVRANK and a REV range of 21 members, estimated several gigabytes for the all-time board with much smaller period boards, and kept a durable event log so any board can be rebuilt.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Use one sorted set per period, lb:all, lb:day:2026-11-12 and lb:week:2026-W46, and update all three in one pipeline or script on each score event, giving period keys an EXPIRE a few days after the period ends. Build the weekly board incrementally rather than with ZUNIONSTORE over seven daily keys, which is O(N) plus O(M log M) on tens of millions of members and blocks the server. The around-me view is ZREVRANK followed by ZRANGE from rank - 10 to rank + 10 with REV, both O(log N) plus 21 members. Expect very roughly 80 to 120 bytes per member plus the member string, so several GB for the all-time board, and keep scores in a durable store so boards can be rebuilt.
Why this question is asked
A leaderboard for 50 million players is big enough that the obvious designs start to hurt: rebuilding boards on read blocks Redis, and memory becomes a line item. Interviewers ask this design question to see whether we can choose keys, commands and expiry with their costs in mind. In this answer, we will design the keys, write the update path, explain why weekly boards are built incrementally, implement the around-me view, estimate memory, and finish with durability.
1Keys: One Sorted Set per Period
A sorted set orders members by score and answers rank and range queries in O(log N). We keep one per board: lb:all for all time, lb:day:2026-11-12 for the day and lb:week:2026-W46 for the ISO week. Putting the period in the key name means a new day simply starts a new key; there is no reset job that has to clear 50 million entries at midnight.
Whether the score is a best score (ZADD GT) or a total (ZINCRBY) is a product decision; the structure is the same. Here we assume totals for day and week and best score for all time, to show both.
2The Update Path
Each score event updates all three boards. We send the three commands in one pipeline, which costs one round trip, or in a small Lua script if we want them applied as a unit. Period keys get an expiry a few days after their period ends, so last week's board is still readable for a while and then removes itself.
Setting the expiry on every event is cheap and idempotent, so we do not need a separate job to remember it. In Redis Cluster these keys live on different slots unless we give them a common hash tag; a pipeline does not care, but a script touching all three would need them in one slot, which would put all boards on one node. Usually the pipeline is the better choice for that reason.
3Why Not Build the Weekly Board From Daily Boards
It is tempting to keep only daily boards and compute the week on demand with ZUNIONSTORE over seven keys. ZUNIONSTORE costs O(N) in the total size of the inputs plus O(M log M) in the size of the output. With tens of millions of members per day, one call can take seconds, and Redis runs it on its single main thread, so every other command, including other boards' reads, waits. Doing that on each read is not an option, and even once per hour it is a multi-second stall.
Incremental updates spread the same work across the score events: each event adds its points to the weekly key as it happens, one O(log N) operation, and the weekly board is always current.
| Approach | Cost | Effect on Redis |
|---|---|---|
| ZUNIONSTORE of 7 days per read | O(N) + O(M log M) on tens of millions | Seconds of blocking per read |
| ZUNIONSTORE once per hour | Same, but scheduled | A multi-second stall every hour |
| Incremental ZINCRBY per event | O(log N) per event | Spread evenly, always current |
4The Around-Me View
To show the ten players above and below the current player, we first find the player's rank, then read the slice around it.
Both commands are O(log N) plus the 21 members returned, so the view costs about the same for the 50-millionth player as for the first. The two calls are not atomic, so a score change between them can shift the window by a place; for a leaderboard that is fine, or a short script can do both in one step. If the player is not on the board, ZREVRANK returns nil and the view shows a 'play to get ranked' message.
5Estimating Memory
A large sorted set uses a skiplist plus a hash table. Very roughly, each member costs 80 to 120 bytes of overhead plus the member string itself. With player ids of about 10 bytes, the all-time board of 50 million members needs on the order of 50 million x 110 bytes, around 5 to 6 GB. A weekly board with every player active would cost the same, but daily and weekly boards are much smaller in practice because only active players appear: 5 million daily players means around half a gigabyte per daily board. Retained period boards add up, which is another reason for the expiry. We confirm estimates by loading a sample and checking MEMORY USAGE on the key, since the exact overhead depends on version and allocator.
| Board | Members | Approximate size |
|---|---|---|
| lb:all | 50 million | 5 to 6 GB |
| lb:week (active players) | 15 million | 1.5 to 2 GB |
| lb:day (active players) | 5 million | about 0.5 GB |
6Redis Is the Serving Copy
Finally, Redis should not be the only place scores live. Every score event is also written to a durable store, such as a PostgreSQL table or a Kafka topic retained for the season. If a node is lost, a failover drops recent writes, or a bug corrupts a board, we can rebuild any board by replaying the events for its period, and we can audit disputed rankings.
7Follow-up questions
How would you show a percentile instead of an exact rank?
Percentile is rank divided by board size: ZREVRANK lb:all p42 divided by ZCARD lb:all, both cheap, gives 'top 2.1 percent'. For many players outside the top thousands, a percentile is more meaningful than 'rank 1,048,204', and it can be cached or computed from a histogram of score buckets, which is much cheaper than exact ranks if the board is ever sharded.
What happens to the daily board at midnight?
Nothing needs to happen. Score events after midnight write to the new day's key, which starts empty, and yesterday's key stays readable until its expiry removes it a few days later.
Why not one key with period prefixes inside member names?
Because each period must be ranked separately; mixing periods in one set would rank yesterday's players against today's. Separate keys also let each period expire on its own.
8Conclusion
In this answer, we designed one sorted set per period with the period in the key, updated all boards per score event in one pipeline, and set expiries a few days after each period ends. We avoided ZUNIONSTORE on large boards because it blocks Redis, built the around-me view from ZREVRANK and a REV range of 21 members, estimated several gigabytes for the all-time board with much smaller period boards, and kept a durable event log so any board can be rebuilt.
Open with
I would use one sorted set per board, with the period in the key: lb:all, lb:day:2026-11-12 and lb:week:2026-W46. Each score event updates all three in one pipeline, or a small script, and I set EXPIRE or EXPIREAT on period keys to a few days after the period ends, so old boards clean themselves up and there is no midnight reset job.
Then, if they want more
I would not build the weekly board with ZUNIONSTORE over seven daily keys, because on tens of millions of members it costs O(N) plus O(M log M) and blocks the server for seconds; instead every event increments the weekly key directly.
For around me, ZREVRANK gives the player's 0-based rank r, then ZRANGE from max of 0 and r minus 10 to r plus 10 with REV and WITHSCORES returns the 21 players, and both are O(log N) plus the members returned.
For memory, a skiplist sorted set costs very roughly 80 to 120 bytes per member plus the member string, so 50 million players is around 5 to 6 GB for the all-time board, while daily and weekly boards are much smaller because only active players appear.
And I treat Redis as the serving copy: scores also go to a durable store or event log, so any board can be rebuilt.
About this answer
- Last updated
- 13 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Redis documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Design the keys and commands, estimate the memory, and explain how the daily and weekly...": https://scrivio-interview-library.netlify.app/databases/redis-patterns/fcea5a14db/