errorception / redis-lock

Implements a locking primitive using redis. An implementation of the algorithm described at https://redis.io/commands/setnx
211 stars 48 forks source link

Redis is single threaded server. Why we need lock ? #5

Closed gkrcode closed 11 years ago

gkrcode commented 11 years ago

Hi,

http://redis.io/topics/benchmarks says 'Redis is single threaded' meaning that no two operation happens at the same time so I am confused about the need for lock ? or am I missing something ?

Thanks

rakeshpai commented 11 years ago

This is a lock implementation using redis, not for redis. It uses the commands that redis provides to build a reliable lock that can be used in node. The lock is useful for concurrency control in node.js where you want to avoid the problems due to high concurrency. The fact that the lock is externalised to redis means that the lock works across different node processes, or even across processes of different programming languages.

Note, it's not as simple as "single threaded = no need for locks". Node appears to be single threaded to programmers, but because it's asynchronous (spawning threads behind the scenes) the main thread might be waiting on multiple things that are going on simultaneously, bringing up the need for concurrency control.

Flickr recently published a post about how they are using a locking mechanism for a node app using redis - exactly the same as this module - for one of their highly concurrent systems. Hopefully that will explain the need for locks better.