ZADD
Adds one or more members to a sorted set, or updates their scores. Creates the key if it doesn’t exist.Syntax
The sorted set key. Created if it doesn’t exist.
Only add new elements. Don’t update existing elements.
Only update existing elements. Don’t add new elements.
Only update if new score is greater than current score.
Only update if new score is less than current score.
Modify return value to be the number of changed elements.
Increment the score instead of setting it. Only one score-member pair allowed with this option.
The score for ordering. Can be a double precision floating point number.
The member to add or update.
- The number of elements added to the sorted set (not including updates)
- With
CH: The number of elements added or updated - With
INCR: The new score of the member (as string)
- 2.4.0: Accepts multiple elements.
- 3.0.2: Added
XX,NX,CHandINCRoptions. - 6.2.0: Added
GTandLToptions.
Examples
ZRANGE
Returns members in a sorted set within a range.Syntax
The sorted set key.
Starting position (0-based index) or minimum score.
Ending position (inclusive) or maximum score.
Interpret start and stop as scores instead of indices.
Interpret start and stop as lexicographical ranges (for elements with same score).
Return results in reverse order.
Limit results (similar to SQL LIMIT).
Return scores along with members.
List of members in the specified range, optionally with scores.
Examples
ZRANK
Returns the rank (position) of a member in a sorted set.Syntax
The sorted set key.
The member to get rank for.
Also return the score of the member.
The rank (0-based) of member, or
nil if member doesn’t exist.
With WITHSCORE: array containing rank and score.Examples
ZSCORE
Returns the score of a member in a sorted set.Syntax
The sorted set key.
The member to get score for.
The score of member (as string), or
nil if member doesn’t exist.Examples
Related Commands
Additional Sorted Set Commands
- ZREM: Remove members
- ZCARD: Get cardinality (number of members)
- ZCOUNT: Count members within score range
- ZINCRBY: Increment member score
- ZREVRANGE: Range in reverse order (deprecated, use ZRANGE REV)
- ZRANGEBYSCORE: Range by score (deprecated, use ZRANGE BYSCORE)
- ZREVRANGEBYSCORE: Reverse range by score (deprecated)
- ZRANGEBYLEX: Range by lexicographical order
- ZPOPMIN: Remove and return lowest scored member(s)
- ZPOPMAX: Remove and return highest scored member(s)
- BZPOPMIN: Blocking ZPOPMIN
- BZPOPMAX: Blocking ZPOPMAX
- ZMSCORE: Get scores of multiple members
- ZRANDMEMBER: Get random member(s)
- ZREMRANGEBYRANK: Remove by rank range
- ZREMRANGEBYSCORE: Remove by score range
- ZREMRANGEBYLEX: Remove by lexicographical range
- ZLEXCOUNT: Count in lexicographical range
- ZSCAN: Iterate members
- ZINTER: Intersection of sorted sets
- ZUNION: Union of sorted sets
- ZDIFF: Difference of sorted sets
- ZINTERSTORE: Store intersection
- ZUNIONSTORE: Store union
- ZDIFFSTORE: Store difference
- ZMPOP: Pop from multiple sorted sets
- ZREVRANK: Reverse rank
Use Cases
Leaderboard
Implement a game leaderboard:Priority Queue
Implement priority-based task queue:Time Series Data
Store time-stamped events:Rate Limiting (Sliding Window)
Implement sliding window rate limiter:Autocomplete
Implement prefix-based autocomplete:Best Practices
Performance Considerations
- Indexing: ZADD, ZREM are O(log N) - efficient even for large sets
- Range Queries: ZRANGE is O(log(N) + M) - efficient for small ranges
- Score Lookups: ZSCORE is O(1) - very fast
- Avoid: Full scans on very large sorted sets
Memory Optimization
- Use integer scores when possible
- Trim old entries periodically with ZREMRANGEBYSCORE
- Consider ziplist encoding for small sorted sets
- Monitor memory usage with MEMORY USAGE
Score Range Queries
- Inclusive Range
- Exclusive Range
- Infinite Range
Use regular scores:
Patterns
Top-N Pattern
Maintain only top N elements:Composite Score
Combine multiple factors into score:Tag Cloud
Implement weighted tags:Geospatial Index
Store locations with geohash scores:Sorted Set vs Other Data Structures
| Feature | Sorted Set | Set | List |
|---|---|---|---|
| Order | Score | None | Insertion |
| Duplicates | No | No | Yes |
| Access | O(log N) | O(1) | O(N) |
| Range Query | Yes | No | Yes |
| Use Case | Rankings | Uniqueness | Sequences |