I recently used Redis SortedSets quite heavily. I had a few pre-requisites:
- Should be extremely fast.
- Think usage in the context of a near real-time web API
- Should scale to billions of entities
- Should be able to give me the “rank” of an item.
- Assume each item has a numeric value that is used to determine rank
- Should be able to give me the count of the total number of items in the set
- Should be able to give me the sum of the values in the set**
- 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:
- http://blog.carbonfive.com/2014/01/17/using-redis-sorted-sets-to-build-a-scalable-real-time-web-waiting-list/
- Very interesting read
- http://www.paperplanes.de/2010/11/29/a_simple_redis_use_case.html
-
- Very interesting read
-
- http://www.tutorialspoint.com/redis/redis_sorted_sets.htm
- https://matt.sh/introduction-to-redis-data-types
- http://openmymind.net/2011/11/8/Redis-Zero-To-Master-In-30-Minutes-Part-1/
- https://www.quora.com/Redis-How-does-memory-usage-compare-for-lists-and-sorted-sets
- http://rianjs.net/2014/04/how-to-iterate-over-redis-keys
- http://www.benbrouse.me/blog/2015/02/05/sortedsets-using-stackexchange-redis/