zookeeper (3) Distributed Lock

In a distributed system, how to ensure an operation, at the same time only one thread can execute, which is distributed lock usage scenarios, the same time, only one thread can obtain the right to use the lock.

How to implement a distributed lock?

Implement a distributed lock, you can have the following three methods.

First, based on the implementation of distributed database lock.

1, in MySQL, using pessimistic locking "SELECT from T WHERE ID = for Update" may be locked to the data lines, distributed lock is achieved.
2, within the same time, there will only be a success thread lock, other threads must wait.
3, to ensure that the database is a global line data corresponding to each lock also unique.

advantage:

1, simple and convenient.

Disadvantages:

1, based on pessimistic locking database, the performance is relatively poor.
2, the thread is waiting for a spin or a wait state, waiting for thread holding the lock processed, multiple threads together again to compete for the same lock.
3, can not handle an exception, when the thread holding the lock has not released the lock, quit unexpectedly, resource lock will not be released, the application will not proceed.

Second, based on distributed lock redis implementation.

1, using the redis SETNX command can simulate distributed lock, a key value SETNX ensure that the operator, if no returns true, if there is false is returned.
2, in order to solve the program unexpectedly quits lock may not release resources, you need to add a key timeout.
3, redis command provides parameters to be set to ensure that a key value and set the timeout atomic these two operations

SET key value [EX seconds] [PX milliseconds] [NX|XX]

4, if the following situation:

A lock is acquired, a timeout period is 10 seconds, but A is performed for 15 seconds, 10 seconds when the lock fails.
B acquires the lock time 11 seconds, performed for 5 seconds, 15 seconds when A is mistakenly deleted the B lock.

To solve this problem, each locking threads are required to set their own value value, but also the time when deleted check their own lock, it can be deleted.

advantage:

1, simple and convenient.
2, High Performance redis high efficiency.

Disadvantages:

1, can not solve the lock timeout failures, logical processing time exceeds the timeout, then this time will lead to another can also get a lock to continue.

To solve this problem, the general approach is to start a daemon thread, always monitor the expiration time, when the lock for more than a certain proportion of execution time, automatically renew some time, of course, the total time is the maximum threshold limit.

2, the thread is waiting for the latter spin wait state, waiting for thread holding the lock processed, multiple threads together again to compete for the same lock.

Three, zookeeper achieve distributed lock

Mode 1: pessimistic locking

1, the use of temporary node implements a distributed lock, first create a temporary node thread successfully acquires the lock success.
2, other threads will create a temporary node fails, the temporary node listens lock.
3, when the lock is released, remove the temporary node will be notified to monitor the thread, the thread continues to receive notification of attempts to create a temporary lock node, who create successful who gets the lock.

advantage:

Failure to solve the lock problem notification mechanism can be the perfect solution, even create a temporary node thread hang, temporary node will be automatically deleted.

Disadvantages:

1, when a large number of threads wait for a lock resource, lock resource release would involve a lot of notice, and a large number of threads need to compete with lock resources.

Mode 2: Optimistic Locking

1, in a directory, each thread to create a temporary order node, the node number 1, 2, and so on.
2, the smallest of threads created under the directory node to acquire the lock.
3, threads wait for a lock resources, no longer listening lock together all the nodes, but only listen smaller than their previous node.
4, when the monitor is deleted smaller than their lock node, continue to read on a monitor smaller than its own node.
4, when the lock is released, you only need to inform listeners of a node lock won several threads, to avoid a lot of notice.

Zookeeper achieve optimistic locking, distributed lock is more reasonable way, interested friends can use code to achieve it.

Guess you like

Origin blog.51cto.com/janephp/2438552