Introduction
Sorted sets in Redis let you store unique items and keep them in order at the same time. This helps you quickly find items by their rank or score.
Sorted sets in Redis let you store unique items and keep them in order at the same time. This helps you quickly find items by their rank or score.
ZADD key [NX|XX] [CH] [INCR] score member [score member ...]
ZADD leaderboard 100 "Alice" 200 "Bob" 150 "Carol"
ZADD leaderboard 250 "Alice"
ZADD leaderboard NX 300 "Dave"
This adds three fruits with scores, updates banana's score, then lists all items sorted by score.
ZADD scores 10 "apple" 20 "banana" 15 "cherry" ZADD scores 25 "banana" ZRANGE scores 0 -1 WITHSCORES
Sorted sets keep members unique by name but order them by score.
Updating a member's score changes its position in the order.
Use ZRANGE to get members in order with their scores.
Sorted sets combine uniqueness and ordering by storing unique members with scores.
This allows fast ranking and retrieval of items in sorted order.
Updating scores reorders members without duplicates.
ZADD myset 5 appleZADD myset 10 bananaZADD myset 7 cherryZRANGE myset 0 -1 WITHSCORES return?ZADD myset 1 appleZADD myset 2 appleZRANGE myset 0 -1Pass the test and this pattern moves unknown