redis distributed lock RedLock


Redis first talk at a normal distributed lock, with

1. Single-node master-slave distributed lock / Sentinel mode, safe?

Perhaps you find out about, locking the following manner:

When setting a lock set command, because it contains setnx, expire function, the effect of atomic functions operate, a random value is provided to the key, and provided only when successful return True, and key expiration time set key does not exist (preferably in milliseconds)

1 the SET key_name my_random_value NX PX # 30,000 NX represent if not exist and is set to return True, otherwise return False PX and not set the expiration time represented by milliseconds, after which represents 30,000 milliseconds this key expired

 

2. After acquiring the lock, and the completion of related businesses, need to remove the lock set up their own (you must delete the lock is only set up their own, others can not remove the lock set);

Delete reasons: to ensure high efficiency of server resources, do not wait until it locks automatically expire deleted;

 

Method Delete: delete preferably used Lua script (Redis do not guarantee this script executes other operations, to ensure atomicity operation), as follows; key acquisition logic is to, if present, and this value is a key to delete their setting; otherwise, skip;

1 if redis.call("get",KEYS[1]) == ARGV[1] then
2     return redis.call("del",KEYS[1])
3 else
4     return 0
5 end

 

Redis defect single point locks: This defect is very obvious, if only a Redis instance, this hung up, he's all dependent services are hung up. Obviously not suitable for large-scale applications.

2. Upgrade the main problem from an architectural model

In order to avoid a single point of failure, we do Redis to master a Master / Slave architecture from a Master, a Slave. The following will encounter such a problem. Here is the scenario.

 

  1. A client obtains a lock on the Master.
  2. The data is synchronized to the Master Slave when hung (as Master and Slave synchronization between asynchronous).
  3. Slave becomes Master.
  4. Client B acquires the lock by the same key, and value. Distributed Lock Failure
  5. Release the lock operation, set free to add their own locks.

3.redisRedlock

 Full name is called Redis Distributed Lock; that the use of distributed lock redis implemented;    RedLock official website explained  Distributed Locks with Redis , English is not good junior partner, you can refer to this article, translated sitting pretty good " Redis Distributed Lock RedLock "

   Personal summarized as follows:

Redlock algorithm

Suppose that we have N (assuming 5) Redis master example, all the nodes are independent, the system is simple and the service call, and without the aid of any other system of similar message retransmission or the like. Let's look at the simulation algorithm:

  1. Client server to obtain the current time t0, the number of milliseconds.
  2. Using the same key value, and sequentially acquire the lock to five instances. Client timeout set itself a business is far less than the duration of lock required at the time of acquiring the lock. For example, assume lock 10 seconds, the timeout period may be provided, such as 5-50 ms. This avoids a Redis itself has been hung up, but the client has been trying to acquire the lock case. After a timeout the direct jump to the next node.
  3. The client by subtracting the current time t0 (t1), is calculated to obtain a lock consumed t2 (= t1-t0). Only lock t2 is less than the effective time traffic (i.e., the second step for 10 seconds), and, at least at the client 3 (5/2 + 1) to acquire the lock table we believe lock acquisition success.
  4. If the lock has been acquired, then the lock of effective business time is 10s-t2.
  5. If the client is not acquired lock, may not be greater than or equal to N a / 2 + 1 example to acquire the lock, may also be effective time (10s-t2) is negative, we will try to release the lock, even if not in to get on that node.

Lock release

   Release is relatively simple, direct delete the corresponding key on all instances like. To determine the release of their own value. Do not release the others.

 

 

 

 

 

 

These are RedLock release the lock of the source code, use the

Under Future mode, Lua script, this part can you know, look at the source code is always a headache, but Daniel wrote the source code is like that. He would later write a special analysis redLock source article.

Guess you like

Origin www.cnblogs.com/amberJava/p/12593648.html