Sorted Set in Redis
Sorted Set Data Types in Redis is an ordered-collection of unique strings stored in a sorted form ordered by associated key. when two or more key having same value then, the strings are ordered in lexicographical order. It is similar to Redis Set , the only difference is that Sorted set represents the data in a sorted or ordered way.
Basic Commands used in Set Data types in Redis
- ZADD: This command will add an element to the ordered- set, if the element is already present then the key value is updated.
- ZRANGE : This command will returns elements of a sorted set within a given range.
- ZRANK : This command will returns the Rank of a specified element in the sorted (Ascending order) list
- ZREVRANK: This command will returns the Rank of a specified element in the sorted (Descending order) list
Example:
ZADD user: GFG 0 DSA
ZADD user : GFG 1 WEB_TECH
ZADD user: GFG 2 SYSTEM_DESIGN
ZADD user: GFG 2 PHYSICS
//command 1
ZRANGEWITHSCORE user: GFG 0 100Output:
1. DSA
2. WEB_TECH
3. SYSTEM_DESIGN
How Redis Stores Sorted Set Data Types?
- Sorted sets combine the features of sets and ordered lists.
- Each element in a sorted set is associated with a score, and elements are stored in ascending order of their scores.
- Sorted sets are useful for ranking and leaderboard applications.
Complete Guide on Redis Data Types with Commands and Storage
Redis is an open-source, in-memory data store that supports various data types: such as String, Sets, Lists, Hashes, etc. to store data according to the needs of the user. These diverse data types make Redis versatile for caching, messaging, real-time analytics, and more.
Different Data Types in Redis
- 1. String Data Type in Redis
- 2. Hash Data type in Redis
- 3. List Data Type in Redis
- 4. Set Data type in Redis:
- 5. Sorted Set in Redis:
- 6. Streams in Redis:
- 7. HyperLogLog in Redis:
- 8. Bitmaps in Redis:
- 9. Bitfields in Redis
- 10. Probabilistic in Redis:
- 11. Geospatial Indexes in Redis:
- 12. Time-Series in Redis
Contact Us