Redis: Maximum score size for sorted sets? Score + Unique ids = Unique Scores?

时间:2015-06-25 18:28:22

标签: redis

I'm using timestamps as the score. I want to prevent duplicates by appending a unique object-id to the score. Currently, this id is a 6 digit number (the highest id right now is 221849), but it is expected to increase over a million. So, the score will be something like 1407971846221849 (timestamp:1407971846 id:221849) and will eventually reach 14079718461000001 (timestamp:1407971846 id:1000001).

My concern is not being able to store scores because they've reached the max allowed.

I've read the docs, but I'm a bit confused. I know, basic math. But bear with me, I want to get this right.

Redis sorted sets use a double 64-bit floating point number to represent the score. In all the architectures we support, this is represented as an IEEE 754 floating point number, that is able to represent precisely integer numbers between -(2^53) and +(2^53) included. In more practical terms, all the integers between -9007199254740992 and 9007199254740992 are perfectly representable. Larger integers, or fractions, are internally represented in exponential form, so it is possible that you get only an approximation of the decimal number, or of the very big integer, that you set as score.

There's another thing bothering me right now. Would the increase in ids break the chronological sort sequence ?

I will appreciate any insights, suggestions, different prespectives or flat out if what I'm trying to do is non-sense.

Thanks for any help.

2 个答案:

答案 0 :(得分:0)

No, it won't break the "chronological" order, but you may loose the precision of the last digits, so two members may end up having the same score (i.e. non-unique).

答案 1 :(得分:0)

There is no problem with duplicate scores. It is just maintaining a sorted set in memory. Members are unique but the scores may be the same. If you want chronological processing I would just rely on the timestamp without adding an id to it. Appending an id would break the chronological sort if your ids are mixed such that you could have timestamps 1, 2, 3 (simple example) and ids 100, 10, 1, you won't get the correct sort. If your ids will always be added monotonically then you should just use the id as the score.