<!-- This is a (PRE) block. Make sure it's left aligned or your toc title will be off. -->
<b>SortedSets: Contents</b><br> <ahref="#Redis Sorted Set Type">Redis Sorted Set Type</a><br> <ahref="#Implementation details">Implementation details</a>
ï»¿#sidebar <ahref="SortedSetCommandsSidebar.html">SortedSetCommandsSidebar</a><h1><aname="Redis Sorted Set Type">Redis Sorted Set Type</a></h1>Redis Sorted Sets are, similarly to <ahref="Sets.html">Sets</a>, collections of <ahref="Strings.html">Redis Strings</a>. The difference is that every member of a Sorted Set hash an <b>associated score</b> that is used in order to take this member in order.<br/><br/>The <ahref="ZaddCommand.html">ZADD</a> command is used to add a new member to a Sorted Set, specifying the score of the element. Calling ZADD against a member already present in the sorted set but using a different score will update the score for the element, moving it to the right position in order to preserve ordering.<br/><br/>It's possible to get ranges of elements from Sorted Sets in a very similar way to what happens with <ahref="Lists.html">Lists</a> and the <ahref="LrangeCommnad.html">LRANGE</a> command using the Sorted Sets <ahref="ZrangeCommand.html">ZRANGE</a> command.<br/><br/>It's also possible to get or remove ranges of elements by score using the <ahref="ZrangebyscoreCommand.html">ZRANGEBYSCORE</a> and <ahref="ZremrangebyscoreCommand.html">ZREMRANGEBYSCORE</a> commands.<br/><br/>The max number of members in a sorted set is 232-1 (4294967295, more than 4 billion of members per set).<br/><br/>Note that while Sorted Sets are already ordered, it is still possible to use the <ahref="SortCommand.html">SORT</a> command against sorted sets to get the elements in a different order.<h1><aname="Implementation details">Implementation details</a></h1>Redis Sets are implemented using a dual-ported data structure containing a skip list and an hash table. When an element is added a map between the element and the score is added to the hash table (so that given the element we get the score in O(1)), and a map between the score and the element is added in the skip list so that elements are taken in order.<br/><br/>Redis uses a special skip list implementation that is doubly linked so that it's possible to traverse the sorted set from tail to head if needed (Check the <ahref="ZRevrangeCommand.html">ZREVRANGE</a> command).<br/><br/>When <ahref="ZaddCommand.html">ZADD</a> is used in order to update the score of an element, Redis retrieve the score of the element using the hash table, so that it's fast to access the element inside the skip list (that's indexed by score) in order to update the position.<br/><br/>Like it happens for Sets the hash table resizing is a blocking operation performed synchronously so working with huge sorted sets (consisting of many millions of elements) care should be taken when mass-inserting a very big amount of elements in a Set while other clients are querying Redis at high speed.<br/><br/>It is possible that in the near future Redis will switch to skip lists even for the element => score map, so every Sorted Set will have two skip lists, one indexed by element and one indexed by score.