We provide an overview on what Redis is, what are sorted sets, common use cases for sorted sets, sorted set operations in Redis, internal implementation, and a comparison of Redis hashes and Redis sorted sets.
2. What is Redis?
Open Source, NoSQL Database
Used by: Twitter, Pinterest, GitHub
Stores Advanced Data Structures
Client Support: Java, C, Node.js etc
Introduction to Redis Data Structures: Sorted Sets
3. What are Sorted Sets?
Redis’s most advanced data structure
A unique collection of ordered Redis Strings that have a numeric score
associated with them
Ordering is based on scores and the string lexicographical order
The average case insertion, removal and search in sorted sets are O(N), where
N is the number of elements in the set.
Introduction to Redis Data Structures: Sorted Sets
4. Common Use Cases for Sorted Sets
Used as a scoreboard: maintaining an
ordered list of unique members sorted by
their scores.
Sorted sets with identical scores are used as
indexes in Redis, from simple to
They have a relatively larger memory
footprint and exact numbers will depend on
the nature of the data set
Introduction to Redis Data Structures: Sorted Sets
5. Sorted Set Operations in Redis
ZADD ZRANK
ZCARD ZREM
ZCOUNT ZSCORE
ZRANGE ZSCAN
The complete list of set related Redis commands can be found here.
Introduction to Redis Data Structures: Sorted Sets
6. Internal Implementation
Implemented as a dual data structure: It is
a combination of both a hash and skip
list
Skip list ensures that searches are fast
Most operations on averages are O(log N)
Introduction to Redis Data Structures: Sorted Sets
7. Redis Hashes vs Redis Sorted Sets
Optimized for size when they are
small
Works well with most apps
Collections of strings
Introduction to Redis Data Structures: Sorted Sets
Sorted sets are stored as only
hashes until they grow to a
certain size
8. Summary
Sorted sets have fairly advanced use cases
The real value of sorted sets shines in it’s range based on queries within the set
Redis sorted sets support a variety of operations from simple set, get, member
count to complex lexicographic range calculations
Sorted sets are sorted by their score in an ascending order
Introduction to Redis Data Structures: Sorted Sets
9. Sign Up for a free 30 day Trial
Thanks for reading! Full Article here
Hosting & management for MongoDB® and Redis®. NoSQL management Simplified.
Click here for more information on Redis Hosting