免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3827 | 回复: 1

[Redis] Redis SPOP不随机的问题 [复制链接]

论坛徽章:
0
发表于 2012-02-16 18:33 |显示全部楼层
Redis SPOP不随机的问题







Redis 的 sets 结构有一个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 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

论坛徽章:
0
发表于 2012-02-17 22:53 |显示全部楼层
谢谢分享
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

DTCC2020中国数据库技术大会 限时8.5折

【架构革新 高效可控】2020年6月4日~6日第十一届中国数据库技术大会将在北京隆重召开。

大会设置2大主会场,20+技术专场,将邀请超百位行业专家,重点围绕数据架构、AI与大数据、传统企业数据库实践和国产开源数据库等内容展开分享和探讨,为广大数据领域从业人士提供一场年度盛会和交流平台。

http://dtcc.it168.com


大会官网>>
  

北京盛拓优讯信息技术有限公司. 版权所有 16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122
中国互联网协会会员  联系我们:huangweiwei@it168.com
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP