Redis INCR concurrency

ε祈祈猫儿з 提交于 2019-12-10 22:02:43

问题


I am using Redis' INCR to generate an ID for objects. And then use ZADD to add the object using the ID as key.

Do I need to worry about if there are multiple connections executing this same block of code? Say after id:12 if two connections connect at the same time and both add object using id:13, then one of them would be lost.


回答1:


Since redis is single threaded, this can never happen - only one client can make a change to the database at a time.




回答2:


As Jonatan Hedborg stated, Redis is single threaded so you never need to worry about two clients doing something at the same time. If, on the other hand, your worry is that you want to run the INCR and ZADD commands sequentially, and want to make sure no other commands are run in between them, you can use transactions, and be guaranteed your commands are run as a single unit with nothing in between.



来源:https://stackoverflow.com/questions/16672355/redis-incr-concurrency

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!