ZADD leaderboard 1500 'player1'
ZADD leaderboard 2300 'player2'
ZREVRANGE leaderboard 0 9 WITHSCORES
Topics
40
Caching Patterns
Cluster Sharding & Hash Slots
Clustering
Connection Pooling & Client Libraries
Data Types
Distributed Locks with Redis
Expiration & Eviction
Geospatial Data in Redis
Hashes in Redis
HyperLogLog in Redis
Keyspace Notifications
Leaderboards with Sorted Sets
Lists in Redis
Lua Scripting
Persistence (RDB/AOF)
Pub/Sub
Rate Limiting with Redis
Redis Architecture & Installation
Redis as a Message Queue
Redis Backup & Disaster Recovery
Redis CLI & Basic Commands
Redis Memory Optimization
Redis Modules Overview
Redis Monitoring & Observability
Redis Performance Tuning & Benchmarking
Redis Replication
Redis Security & ACL
Redis Sentinel & High Availability
Redis vs Memcached
Redis with Docker & Kubernetes
RedisBloom (Probabilistic Data Structures)
RedisJSON
RedisSearch (Full Text Search)
RedisTimeSeries
Session Management with Redis
Sets & Sorted Sets in Redis
Streams
Strings & Bitmaps in Redis
TLS & Encryption in Redis
Transactions
Leaderboards with Sorted Sets
7 questions found
A sorted set automatically keeps its members ordered by their associated score, meaning as you add or update a player's score, Redis automatically maintains the correct ranking order internally, letting you instantly retrieve the top players or a specific player's rank without needing to manually sort anything yourself.
Real-world example
A mobile game maintains a global leaderboard using a sorted set, automatically keeping thousands of player scores properly ranked as they play and earn points throughout the day.
Sets & Sorted Sets in Redis;Data Types
You use the ZREVRANGE command, specifying the sorted set key and a range from zero to nine, which returns the ten members with the highest scores in descending order, since ZREVRANGE returns results from highest to lowest score by default.
ZREVRANGE leaderboard 0 9 WITHSCORES
Real-world example
A gaming leaderboard screen displays the current top ten players by calling ZREVRANGE, instantly retrieving both the player names and their scores in the correct descending order.
Sets & Sorted Sets in Redis;Redis CLI & Basic Commands
How do you find a specific player's current rank on a leaderboard, along with the players immediately above and below them?
IntermediateYou use the ZREVRANK command to find a specific player's zero based rank position in descending score order, and then use ZREVRANGE around that rank position to retrieve the players immediately surrounding them, giving you a personalized view showing a player exactly where they stand relative to nearby competitors.
ZREVRANK leaderboard 'player5' -- returns rank, e.g. 42
ZREVRANGE leaderboard 40 44 WITHSCORES -- players around that rank
Real-world example
A game shows a player their current global rank along with the two players just above and below them, calculated using ZREVRANK combined with a small ZREVRANGE query around their specific position.
Sets & Sorted Sets in Redis;Redis Performance Tuning & Benchmarking
How do you atomically increase a player's score on a leaderboard, such as after they earn additional points during gameplay?
IntermediateYou use the ZINCRBY command, specifying the leaderboard key, the amount to add, and the player's name, which atomically increases that player's existing score by the specified amount, automatically re-adjusting their position in the ranking as needed without any risk of a race condition from multiple simultaneous score updates.
ZINCRBY leaderboard 50 'player1'
Real-world example
A game safely increases a player's score by fifty points immediately after they complete a level, using ZINCRBY to atomically update their score even if many players are earning points at the exact same moment.
Sets & Sorted Sets in Redis;Redis CLI & Basic Commands
How would you design a leaderboard system that resets periodically, such as weekly, while still preserving historical rankings for past periods?
AdvancedYou would create a new sorted set key for each period, such as including the week number in the key name, letting the current week's leaderboard start fresh while previous weeks' completed leaderboards remain fully intact and queryable, and you could set an expiration on very old period leaderboards to automatically clean them up after they are no longer needed.
ZADD leaderboard:week37 100 'player1'
ZADD leaderboard:week38 50 'player1' -- fresh start for the new week
EXPIRE leaderboard:week30 2592000 -- clean up very old weeks
Real-world example
A competitive mobile game maintains a separate sorted set for each week's leaderboard, letting players compete fresh every week while still allowing the game to display historical results from previous weeks upon request.
Expiration & Eviction;Sets & Sorted Sets in Redis
How would you implement a leaderboard that supports multiple different ranking categories, such as by region or by game mode, efficiently in Redis?
AdvancedYou would create a separate sorted set for each distinct combination of category you need to rank by, such as leaderboard:region:us or leaderboard:mode:ranked, updating a player's score in every relevant category specific sorted set whenever they earn points, letting each category be queried independently and efficiently without any complex filtering logic.
ZADD leaderboard:global 1000 'player1'
ZADD leaderboard:region:us 1000 'player1'
ZADD leaderboard:mode:ranked 1000 'player1'
Real-world example
A competitive game maintains separate leaderboards for global rankings, regional rankings, and specific game mode rankings, updating a player's score across all the relevant sorted sets simultaneously whenever they complete a match.
Sets & Sorted Sets in Redis;Redis Memory Optimization
How do you retrieve all players within a specific score range on a leaderboard, such as everyone between a thousand and two thousand points?
IntermediateYou use the ZRANGEBYSCORE command, specifying the leaderboard key along with a minimum and maximum score, which returns all members whose score falls within that specified range, letting you build features like showing players in a similar skill bracket to a specific user.
ZRANGEBYSCORE leaderboard 1000 2000 WITHSCORES
Real-world example
A matchmaking system finds all players within a similar skill range to a specific user by querying a score range on the leaderboard, helping to pair players with comparable skill levels for a fair match.
Sets & Sorted Sets in Redis;Data Types