Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get same rank for same scores in Redis' ZRANK?

If I have 5 members with scores as follows

a - 1
b - 2
c - 3
d - 3
e - 5

ZRANK of c returns 2, ZRANK of d returns 3

Is there a way to get same rank for same scores?
Example: ZRANK c = 2, d = 2, e = 3
If yes, then how to implement that in spring-data-redis?

like image 348
Ankit Gajra Avatar asked Sep 03 '18 15:09

Ankit Gajra


2 Answers

Any real solution needs to fit the requirements, which are kind of missing in the original question. My 1st answer had assumed a small dataset, but this approach does not scale as dense ranking is done (e.g. via Lua) in O(N) at least.

So, assuming that there are a lot of users with scores, the direction that for_stack suggested is better, in which multiple data structures are combined. I believe this is the gist of his last remark.

To store users' scores you can use a Hash. While conceptually you can use a single key to store a Hash of all users scores, in practice you'd want to hash the Hash so it will scale. To keep this example simple, I'll ignore Hash scaling.

This is how you'd add (update) a user's score in Lua:

local hscores_key = KEYS[1]
local user = ARGV[1]
local increment = ARGV[2]
local new_score = redis.call('HINCRBY', hscores_key, user, increment)

Next, we want to track the current count of users per discrete score value so we keep another hash for that:

local old_score = new_score - increment
local hcounts_key = KEYS[2]
local old_count = redis.call('HINCRBY', hcounts_key, old_score, -1)
local new_count = redis.call('HINCRBY', hcounts_key, new_score, 1)

Now, the last thing we need to maintain is the per score rank, with a sorted set. Every new score is added as a member in the zset, and scores that have no more users are removed:

local zdranks_key = KEYS[3]
if new_count == 1 then
  redis.call('ZADD', zdranks_key, new_score, new_score)
end
if old_count == 0 then
  redis.call('ZREM', zdranks_key, old_score)
end

This 3-piece-script's complexity is O(logN) due to the use of the Sorted Set, but note that N is the number of discrete score values, not the users in the system. Getting a user's dense ranking is done via another, shorter and simpler script:

local hscores_key = KEYS[1]
local zdranks_key = KEYS[2]
local user = ARGV[1]

local score = redis.call('HGET', hscores_key, user)
return redis.call('ZRANK', zdranks_key, score)
like image 127
Itamar Haber Avatar answered Sep 23 '22 21:09

Itamar Haber


You can achieve the goal with two Sorted Set: one for member to score mapping, and one for score to rank mapping.

Add

  1. Add items to member to score mapping: ZADD mem_2_score 1 a 2 b 3 c 3 d 5 e
  2. Add the scores to score to rank mapping: ZADD score_2_rank 1 1 2 2 3 3 5 5

Search

  1. Get score first: ZSCORE mem_2_score c, this should return the score, i.e. 3.
  2. Get the rank for the score: ZRANK score_2_rank 3, this should return the dense ranking, i.e. 2.

In order to run it atomically, wrap the Add, and Search operations into 2 Lua scripts.

like image 40
for_stack Avatar answered Sep 22 '22 21:09

for_stack