How to create a distributed lock with Redis?

前端 未结 5 1246
广开言路
广开言路 2020-12-28 17:37

On the redis documentation, I found a primitive lock can be implemented via SETNX:

http://redis.io/commands/setnx

  • C4 sends SETNX lo

5条回答
  •  一个人的身影
    2020-12-28 18:42

    The new arguments for SET are enough for setting the lock, but these only work on Redis >= v2.6.12 you also need to think about how the lock will be unset and expire etc.

    I've written a post on our Engineering blog about distributed locks using Redis. It covers scripting on how to set and release the lock reliably, with validation and deadlock prevention. I also include a module written in Node.js you can use for locking straight out of the box.

提交回复
热议问题