Announcing Redis 7.2 unified release and enhanced vector DB

Announcement blog

Redis Sorted Sets

Back to Glossary

Sorted sets in Redis are a powerful data structure that combines the features of sets and sorted lists. They allow you to store a collection of unique elements while assigning a score or rank to each element. This score is used to determine the order of elements in the set, making sorted sets an excellent choice for applications that require ordered data.

In Redis, sorted sets are implemented using a combination of a hash table and a skip list data structure. The hash table provides fast access to elements based on their value, while the skip list maintains the sorted order of the elements based on their scores. This dual structure allows Redis to efficiently perform operations on sorted sets.

Sorted Set Features

One of the key features of sorted sets is the ability to add, remove, or update elements dynamically while maintaining their sorted order. You can insert elements into a sorted set with an associated score, which can be a floating-point number. Redis uses the score to position the element in the sorted set. If an element with the same value already exists, its score is updated accordingly.

Operations and Functionality of Sorted Sets in Redis

Sorted sets provide various operations for manipulating the data. You can retrieve elements within a specific range based on their scores, enabling efficient pagination or ranking functionality. Redis supports both inclusive and exclusive range queries, allowing you to fetch elements by their scores or their positions in the sorted set. Additionally, you can perform set operations like union, intersection, and difference on sorted sets, enabling you to combine or compare multiple sorted sets.

Redis also provides efficient methods to increment or decrement the score of an element in a sorted set. This feature is particularly useful in scenarios where you need to track rankings or keep a leaderboard. By incrementing or decrementing the score of an element, you can easily update its position in the sorted set without the need for complex operations.

Sorted Set Benefits

Sorted sets in Redis offer several benefits. They provide fast access to elements based on their values, thanks to the underlying hash table structure. The skip list ensures efficient ordering and range queries based on scores. Sorted sets are widely used for various applications, including leaderboards, real-time analytics, job scheduling, and more.