Redis : Sorted Sets

I recently used Redis SortedSets quite heavily. I had a few pre-requisites:

  1. Should be extremely fast.
    • Think usage in the context of  a near real-time web API
  2. Should scale to billions of entities
  3. Should be able to give me the “rank” of an item.
    • Assume each item has a numeric value that is used to determine rank
  4. Should be able to give me the count of the total number of items in the set
  5. Should be able to give me the sum of the values in the set**
  6. Should be able to give me the cumulative sum of the values from the 0th item to kth item based on their ranks**

**I am yet to figure out how to do 5-6. Perhaps there is a way to do this in Redis as well.  Else, I am planning to do some form of Reservoir Sampling to get an approximation of the sums.

Observations:

  • Redis SortedSets support 1-4 out of the box!
  • In general I am amazed at the advanced internal data structures that are used in Redis.
  • Sorted Sets for example use SkipLists internally.
    • This serves as a great motivation to do a blog post on SkipLists actually.
    • Also, RangeQueries maybe. Sorted Sets support range operations which are quite handly.  Need to understand how those are supported internally.

Code:

 

References:

 

Leave a comment