Home > Back-end >  Questions about Redis SETNX realize distributed lock
Questions about Redis SETNX realize distributed lock

Time:11-14

If SETNX returns 1 the client acquired the lock, setting the lock. The foo key to the Unix time at which the lock should no longer be considered valid. The client will later use DEL lock. The foo in order to release the lock.

If SETNX returns 0, the key is already locked by some other client. We can either return to the caller If it 's a non blocking the lock, or enter a loop retrying to hold the lock until We succeed or some kind of a timeout expires.

Redis. IO about setnx has so a description, in which he said the If setnx returns 1 the client acquired the lock, setting the lock. The foo key to the Unix time at which the lock should no longer be considered valid. Is there a big explain this sentence what mean, mean when the client get the lock, this command expiration time is no longer valid

CodePudding user response:

If SETNX returns 1 the client acquired the lock, setting the lock. The foo key to the Unix time at which the lock should no longer be considered valid. The client will later use DEL lock. The foo in order to release the lock.
If SETNX returns 1 (representative) have the client lock, set the lock. The foo keys to represent a lock after failure of Unix time, after the customer can use DEL lock. The foo release the lock,
Is to obtain the lock failure time, the client can set a lock lock before this time has been effective, but the client can also use DEL lock. The foo releases the lock ahead of time,
Unix is the 1970-01-01 starts at 0 seconds count,

CodePudding user response:

There are two ways to get the lock is released, if you use the correct end lock, by del releases the lock; There is a situation in which the connection is broken, thread hang up and no way to release the lock, another thread to request at this time the lock is not available, enter a deadlock state, in order to avoid the above problems, the time used to indicate the valid time of the lock, after a time, with thread lock even though no release, other threads will release and compete for the lock,
  • Related