Redis SPOP不随机的问题

Redissets 结构有一个SPOP方法,方法描述为“Remove and return a random member from a set”,方法说明是随机的。但实际上通过测试表时此方法并不如说的这样随机,比如你将1,2,3,4四个数字用SADD添加到某一个sets里,那么你调用SPOP取出的数据顺序将是一定的。而非随机。

这一问题在Redis的Google Group上被一位Redis的用户提了出来,并怀疑sets里的值是在写数据时就确定了顺序。

Hey Redis Land-

I”m using a normal SET with 1000 members and using SPOP to pop random elements off the set push them onto an in-use set while I use the member. This is in a web app I”m writing. So during development of the app, I have a script that deletes the redis db and repopulates the sets so that I can start fresh again. The weird thing I”ve noticed is that spop docs say that it will return a random member of the set and it seems to do this. but when I recreate the set and casino online start from scratch the elements I start spop”ing are always in the same order. And the first element I SPOP is always the same member after I re-initialize the set.

Does redis seed its random number generator at startup time or do anything so that spop and other “random” commands are truly random and do not repeat the patterns of what elements get returned if you recreate the same set and start over again?

Thanks

后来Redis作者予以了解答,实际上SPOP函数确实不是随机的,也确实是在SADD时就确定了SPOP的顺序,在SADD时,会对value值进行Hash值的计算来确定其在sets中的排序位置,于是出现上面的相同的值被SADD会出现SPOP出的值顺序相同了。其实这个要改动起来确实不大,作者承诺在2.2版本发布时完成这个小改动。

Hello Ezra,

the problem is exactly the one you identified: the PRNG is not seeded
at startup.
The reason this was not changed in the past was to make the system
more predictable, that is, if you get a crash, reproducing it is
simple. But actually it is a bad limit from the point of view of the
distribution of random elements when Redis is restarted.

It”s a one line change, I guess we are just in time to add this for 2.2…

Just pushed on the 2.2 branch and in the unstable branch.

Cheers,
Salvatore

这并不是一个大问题,但是却可以提醒我们,不要被文档所迷惑,在将产品用在真实环境前,多做仿真测试以确定其真正特性。

相关地址:http://goo.gl/ByO9o

anyShare据说看到好文章不转的人,服务器容易宕机!
          

无觅相关文章插件,快速提升流量

分类 Redis · tag , , , ,