Redis Sorted Sets are similar to Sets, with unique feature of values stored in Set called scores. The score is used in order to take the sorted set ordered, from the smallest to the greatest score. Just like in Sets, members are unique but scores can be repeated. Sorted Sets are ideal for storing index data in Redis, or using some of the Sorted Set commands to find out how many users have authenticated to a system, or top users using the system etc.

Redis Sorted Sets Datatype – Operations

  • ZADD: Adds or updates one or more members to a Sorted Set O(log (N))
  • ZRANGE: Gets the specified range by rank of elements in the Sorted Set O(log (N) +M).
  • ZRANGEBYSCORE: Gets elements from the Sorted Sets within the range by score given values are in ascending order O(log (N) +M)
  • ZREVRANGEBYSCORE: Gets elements from the Sorted Sets within the score given O(log (N) +M)
  • ZREVRANK: The rank of the member in the Sorted Set O (log (N))
  • ZREVRANGE: Returns the specified range of elements in the Sorted Set O(log (N) + M)
  • ZREM: Removes the specified members in the Sorted Set O(M*log (N))
  • ZREMRANGEBYRANK: Removes the members in a Sorted Set within the given indexes O(log (N) * M)
  • ZREMRANGEBYSCORE: Removes the members in a Sorted Set within the given scores O(log (N) * M)
  • ZCARD: Gets the number of members in a Sorted Set O(1)
  • ZCOUNT: Gets the number of members in a Sorted Set within the score boundaries O(log (N) * M)
  • ZINCRBY: Increases the score of an element in the Sorted Set O(log (N))
  • ZINTERSTORE: Calculates the common elements in the Sorted Sets given by the specified keys, and stores the result in destination Sorted Set O(N*K) + O (M*log (M))
  • ZRANK: Gets the index of the element in the Sorted Set O(log (N))
  • ZSCORE: Returns the score of the member O(1)
  • ZUNIONSTORE: Computes the union of keys in the given Sorted Set and stores the result in the resultant Sorted Set O(N) + O(M log (M))

C# code using Redis Sorted Set Datatype

So this covers the basic usage of Redish Sorted Sets Datatype, in the next blog post I will cover using Hyperloglog Datatype.

For the code please visit
https://github.com/taswar/RedisForNetDevelopers/tree/master/5.RedisList/RedisList

For previous Redis topics

  1. Intro to Redis for .NET Developers
  2. Redis for .NET Developer – Connecting with C#
  3. Redis for .NET Developer – String Datatype
  4. Redis for .NET Developer – String Datatype part 2
  5. Redis for .NET Developer – Hash Datatype
  6. Redis for .NET Developer – List Datatype
  7. Redis for .NET Developer – Redis Sets Datatype