[21] pry(main)> redis.zadd('feed:home:99', 107243889697683480, 107241582127494398)
=> true
[22] pry(main)> redis.zrevrangebyscore('feed:home:99', '(+inf', '(-inf', with_scores: true)
=> [["107241582127494398", 1.0724388969768349e+17]]
[23] pry(main)> redis.zrangebyscore('feed:home:99', '(-inf', '(+inf', with_scores: true)
=> [["107241582127494398", 1.0724388969768349e+17]]
I put 107243889697683480 but I am getting 1.0724388969768349e+17
Is this issue or correct result?
Thank you
Comment From: madolson
Yes it is expected. Redis uses 64 precision doubles for storing the score within sorted sets. There isn't enough precision in a 64 bit double to store the value 107243889697683480, so you can see that it is rounding it slightly.
Comment From: adrianomic
@madolson Thanks for your reply.
Which solution do you recommend in this case? Decreasing the big integer could be big problem for me :(
Comment From: zuiderkwast
@adrianomic This is a limitation of the sorted set score, but it's not a problem for the other data structures.
For example, if you need the exact number, you can store it in a HASH, e.g. HSET feed:home:99:exact-score 107241582127494398 107243889697683480, in parallel with the sorted set. Then, after zrevrangebyscore, you can lookup the hash to get original score.
If there is a risk that rounding affects the ordering of members returned by zrangebyscore, you can fetch all values with the same score and then look them all up in the hash to be able to compare them exactly on the client side or in a Lua script.
What do you think? Can this solution work for you?
Comment From: monish-chhadwa
How about taking advantage of "lexicographic ordering of keys with same score". This won't be as optimal as sorting of scores; but you can consider the most significant digits in the score; and the lesser significant digits as a prefix for the member-key. For instance: Lets say you need to have sorting for
entry1 = 107243889697683480
entry2 = 107241582127494398
entry3 = 107243889697683481
so you take the most significant 15 digits(as these can be safely stored as integers) in the score part and modify the member keys
480:entry1 = 107243889697683
481:entry2 = 107243889697683
398:entry2 = 107241582127494
and redis will sort it for you as: (first basis the score, and then lexical)
398:entry2 = 107241582127494
480:entry1 = 107243889697683
481:entry2 = 107243889697683