Using zookeeper to implement distributed locks across JVMs (optimized ideation) for Java

Source: Internet
Author: User

Description : This article is based on the use of zookeeper to implement a distributed lock of Java across the JVM, without reading a friend please read the previous article after reading this article.

The previous article described how to use distributed locks and extended the original fair lock to achieve an unfair lock that already satisfies most of the cross-process (JVM) lock requirements.


problem: We all know that the mechanism to implement locks within a single JVM is convenient, and Java also provides a rich API to implement, such as synchronized keywords, reentrantlock, etc., but in a clustered environment, multiple JVMs work together, The distributed locks described above are used when some global locks are needed, but the disadvantage of this lock is that each client (which the client can understand as a thread in a different JVM) needs to interact with the Zook server to acquire the lock, create Znode, wait for itself to acquire the lock, This kind of network communication will undoubtedly bring some pressure to the server, then do we have any way to reduce this kind of pressure?


Scenario: One of the most common scenarios is to update the cache, so our general processing logic is as follows.

1, the preferred to access resources according to key, if the resources exist, use it.

2. If it does not exist, apply for a lock (using a shared lock).

3, get to the lock, again determine whether the resource exists (prevent repeated updates), if there is a description has been updated, the method exits, otherwise update the cache.

4, Release the lock.

Suppose there are now 10 (1-10) threads executing the appellate logic at the same time, if the resource does not exist, then all of them will take step (2) to acquire the lock, and at the same time, there will be only 1 threads acquiring the lock, the other 9 threads blocking, waiting for the lock. Now let's assume that thread 1 gets to the lock, starts with the (3-4) step, and in step (3), again determines whether the resource exists (it certainly does not exist because it is the first to go in), so it is responsible for loading the resources into the cache, then releasing the locks, and then other threads (2-10) They get to the lock, Then execute (3,4) the action, again determine whether the resources exist (already exist, because the Line 1 line has been put in), so they directly exit, release the lock. This shows that only the Line 1-way lock is meaningful, but they all need to communicate with Zook network, so it will bring pressure on the network.


If we have a A, a two server cluster, the 10 threads to obtain the lock request from the 2 servers, such as (1-5) line from a server, (6-10) line from the B server, then they interact with ZK 10 times, create 10 Znode to apply for the lock, But if we make some optimizations, they'll be enough to interact with ZK 2 times, so let's transform the logic above.

1, the preferred to access resources according to key, if the resources exist, use it.

2. If it does not exist, request a lock (a lock within the JVM process).

3, get to the lock (JVM process Lock), again determine whether the existence of resources, if the resources exist on the exit, there is nothing good.

4, if the resource does not exist, then request to acquire the lock (distributed lock).

5, get to the lock (distributed lock) again to determine whether the resource exists (prevent repeated updates), if there is a description has been updated, the method exits, otherwise update the cache.

6. Release the distributed lock and release the lock within the JVM process.


code: I put the implementation logic together, convenient for everyone to demonstrate, if there is a logic error welcome you to comment.

Package Com.framework.code.demo.zook;import Java.util.hashmap;import Java.util.map;import Java.util.concurrent.executorservice;import Java.util.concurrent.executors;import Java.util.concurrent.TimeUnit; Import Java.util.concurrent.locks.reentrantlock;import Org.apache.curator.framework.curatorframework;import Org.apache.curator.framework.curatorframeworkfactory;import Org.apache.curator.framework.recipes.locks.interprocessmutex;import Org.apache.curator.retry.exponentialbackoffretry;import Com.framework.code.demo.zook.lock.NoFairLockDriver; public class Main {///We use a static Map to simulate a third-party independent cache public static map<string, object> Redis = new hashmap<string, OBJ Ect> ();p ublic static final String key = "Rediskey";p ublic static void Main (string[] args) throws Interruptedexception { Two objects were created to simulate 2 processes redisprocess Processa = new redisprocess (); Redisprocess PROCESSB = new redisprocess ();//Each process is divided by 50 threads concurrent Requests Executorservice service = Executors.newfixedthreadpool ( for (int i = 0; i <; i++){Service.execute (Processa); Service.execute (PROCESSB);} Service.shutdown (); service.awaittermination (timeunit.seconds);} public static class Redisprocess implements Runnable {curatorframework Client;//zk distributed lock Interprocessmutex DISTRIBUTEDLOCK;//JVM internal lock Reentrantlock jvmlock;public redisprocess () {client = Curatorframeworkfactory.newclient (" 192.168.1.18:2181 ", New Exponentialbackoffretry (1000,3)), Client.start ();d istributedlock = new Interprocessmutex ( Client, "/mylock", New Nofairlockdriver ()), Jvmlock = new Reentrantlock (); @Overridepublic void Run () {//(1) first determine if the resource exists in the cache if (Redis.get (key) = = null) {try {//= here the delay of 1000 milliseconds is to prevent the thread from updating the resource too quickly, then the other threads in the step ( 1) returns true at the same place. Thread.Sleep (1000);//Get the JVM lock (valid within the same process) jvmlock.lock ();//(2) again determine if the resource already exists if (redis.get (key) = = null) { System.out.println ("Thread:" + thread.currentthread () + "get to the JVM lock, Redis.get (key) is empty, ready to acquire ZK lock");//The purpose of this delay of 500 milliseconds is to prevent the thread from updating the resource too quickly. The other thread returns True at Step (2). Thread.Sleep (+); try {//acquire ZK Distributed lock Distributedlock.acquire (); System.out.println ("Thread:" + thread.currentthread () + "get to JVMThe lock, Redis.get (key) is empty, gets to the ZK lock ");//Again, if it is empty then you can update the resource if (redis.get (key) = = null) {Redis.put (key, Thread.CurrentThread () + "Updated cache"); System.out.println ("Thread:" + thread.currentthread () + "cache updated");} else {System.out.println ("thread:" + thread.currentthread () + "Current resource already exists, no update required");}} catch (Exception e) {e.printstacktrace ();} finally {//release ZK lock try {distributedlock.release ();} catch (Exception e) { E.printstacktrace ();}}} else {System.out.println ("thread:" + thread.currentthread () + "get to the JVM lock, Redis.get (key) is not empty," + redis.get (key));}} catch (Interruptedexception e) {e.printstacktrace ();} finally {//releases the JVM lock Jvmlock.unlock ();}} else {System.out.println (Redis.get (key));}}}}



thread: Thread[pool-5-thread-2,5,main] gets to the JVM lock, Redis.get (key) is empty, ready to acquire ZK lock
Thread: Thread[pool-5-thread-3,5,main] gets to the JVM lock, Redis.get (key) is empty, ready to acquire ZK lock
Thread: Thread[pool-5-thread-3,5,main] gets to the JVM lock, Redis.get (key) is empty, gets to ZK lock
Thread: Thread[pool-5-thread-3,5,main] Updated cache

Thread: Thread[pool-5-thread-7,5,main] gets to the JVM lock, Redis.get (key) is not empty, Thread[pool-5-thread-3,5,main] updated the cache
Thread: Thread[pool-5-thread-1,5,main] gets to the JVM lock, Redis.get (key) is not empty, Thread[pool-5-thread-3,5,main] updated the cache
Thread: Thread[pool-5-thread-5,5,main] gets to the JVM lock, Redis.get (key) is not empty, Thread[pool-5-thread-3,5,main] updated the cache
Thread: Thread[pool-5-thread-9,5,main] gets to the JVM lock, Redis.get (key) is not empty, Thread[pool-5-thread-3,5,main] updated the cache
Thread: Thread[pool-5-thread-23,5,main] gets to the JVM lock, Redis.get (key) is not empty, Thread[pool-5-thread-3,5,main] updated the cache
Thread: Thread[pool-5-thread-19,5,main] gets to the JVM lock, Redis.get (key) is not empty, Thread[pool-5-thread-3,5,main] updated the cache
Thread: Thread[pool-5-thread-11,5,main] gets to the JVM lock, Redis.get (key) is not empty, Thread[pool-5-thread-3,5,main] updated the cache
Thread: Thread[pool-5-thread-31,5,main] gets to the JVM lock, Redis.get (key) is not empty, Thread[pool-5-thread-3,5,main] updated the cache
Thread: Thread[pool-5-thread-35,5,main] gets to the JVM lock, Redis.get (key) is not empty, Thread[pool-5-thread-3,5,main] updated the cache
Thread: Thread[pool-5-thread-15,5,main] gets to the JVM lock, Redis.get (key) is not empty, Thread[pool-5-thread-3,5,main] updated the cache
Thread: Thread[pool-5-thread-27,5,main] gets to the JVM lock, Redis.get (key) is not empty, Thread[pool-5-thread-3,5,main] updated the cache
Thread: Thread[pool-5-thread-25,5,main] gets to the JVM lock, Redis.get (key) is not empty, Thread[pool-5-thread-3,5,main] updated the cache
Thread: Thread[pool-5-thread-33,5,main] gets to the JVM lock, Redis.get (key) is not empty, Thread[pool-5-thread-3,5,main] updated the cache
Thread: Thread[pool-5-thread-37,5,main] gets to the JVM lock, Redis.get (key) is not empty, Thread[pool-5-thread-3,5,main] updated the cache
Thread: Thread[pool-5-thread-13,5,main] gets to the JVM lock, Redis.get (key) is not empty, Thread[pool-5-thread-3,5,main] updated the cache
Thread: Thread[pool-5-thread-17,5,main] gets to the JVM lock, Redis.get (key) is not empty, Thread[pool-5-thread-3,5,main] updated the cache
thread: Thread[pool-5-thread-2,5,main] gets to the JVM lock, Redis.get (key) is empty, gets to ZK lock
Thread: Thread[pool-5-thread-2,5,main] The current resource already exists and does not need to be updated

Thread: Thread[pool-5-thread-21,5,main] gets to the JVM lock, Redis.get (key) is not empty, Thread[pool-5-thread-3,5,main] updated the cache
Thread: Thread[pool-5-thread-29,5,main] gets to the JVM lock, Redis.get (key) is not empty, Thread[pool-5-thread-3,5,main] updated the cache
Thread: Thread[pool-5-thread-55,5,main] gets to the JVM lock, Redis.get (key) is not empty, Thread[pool-5-thread-3,5,main] updated the cache
Thread: Thread[pool-5-thread-59,5,main] gets to the JVM lock, Redis.get (key) is not empty, Thread[pool-5-thread-3,5,main] updated the cache
Thread: Thread[pool-5-thread-41,5,main] gets to the JVM lock, Redis.get (key) is not empty, Thread[pool-5-thread-3,5,main] updated the cache
Thread: Thread[pool-5-thread-67,5,main] gets to the JVM lock, Redis.get (key) is not empty, Thread[pool-5-thread-3,5,main] updated the cache
Thread: Thread[pool-5-thread-39,5,main] gets to the JVM lock, Redis.get (key) is not empty, Thread[pool-5-thread-3,5,main] updated the cache
Thread: Thread[pool-5-thread-43,5,main] gets to the JVM lock, Redis.get (key) is not empty, Thread[pool-5-thread-3,5,main] updated the cache
Thread: Thread[pool-5-thread-57,5,main] gets to the JVM lock, Redis.get (key) is not empty, Thread[pool-5-thread-3,5,main] updated the cache
Thread: Thread[pool-5-thread-47,5,main] gets to the JVM lock, Redis.get (key) is not empty, Thread[pool-5-thread-3,5,main] updated the cache
Thread: Thread[pool-5-thread-51,5,main] gets to the JVM lock, Redis.get (key) is not empty, Thread[pool-5-thread-3,5,main] updated the cache
Thread: Thread[pool-5-thread-63,5,main] gets to the JVM lock, Redis.get (key) is not empty, Thread[pool-5-thread-3,5,main] updated the cache
Thread: Thread[pool-5-thread-8,5,main] gets to the JVM lock, Redis.get (key) is not empty, Thread[pool-5-thread-3,5,main] updated the cache
Thread: Thread[pool-5-thread-69,5,main] gets to the JVM lock, Redis.get (key) is not empty, Thread[pool-5-thread-3,5,main] updated the cache
Thread: Thread[pool-5-thread-4,5,main] gets to the JVM lock, Redis.get (key) is not empty, Thread[pool-5-thread-3,5,main] updated the cache
Thread: Thread[pool-5-thread-6,5,main] gets to the JVM lock, Redis.get (key) is not empty, Thread[pool-5-thread-3,5,main] updated the cache
Thread: Thread[pool-5-thread-10,5,main] gets to the JVM lock, Redis.get (key) is not empty, Thread[pool-5-thread-3,5,main] updated the cache
Thread: Thread[pool-5-thread-22,5,main] gets to the JVM lock, Redis.get (key) is not empty, Thread[pool-5-thread-3,5,main] updated the cache
Thread: Thread[pool-5-thread-16,5,main] gets to the JVM lock, Redis.get (key) is not empty, Thread[pool-5-thread-3,5,main] updated the cache
Thread: Thread[pool-5-thread-20,5,main] gets to the JVM lock, Redis.get (key) is not empty, Thread[pool-5-thread-3,5,main] updated the cache
Thread: Thread[pool-5-thread-45,5,main] gets to the JVM lock, Redis.get (key) is not empty, Thread[pool-5-thread-3,5,main] updated the cache
Thread: Thread[pool-5-thread-24,5,main] gets to the JVM lock, Redis.get (key) is not empty, Thread[pool-5-thread-3,5,main] updated the cache
Thread: Thread[pool-5-thread-32,5,main] gets to the JVM lock, Redis.get (key) is not empty, Thread[pool-5-thread-3,5,main] updated the cache
Thread: Thread[pool-5-thread-36,5,main] gets to the JVM lock, Redis.get (key) is not empty, Thread[pool-5-thread-3,5,main] updated the cache
Thread: Thread[pool-5-thread-49,5,main] gets to the JVM lock, Redis.get (key) is not empty, Thread[pool-5-thread-3,5,main] updated the cache
Thread: Thread[pool-5-thread-28,5,main] gets to the JVM lock, Redis.get (key) is not empty, Thread[pool-5-thread-3,5,main] updated the cache
Thread: Thread[pool-5-thread-12,5,main] gets to the JVM lock, Redis.get (key) is not empty, Thread[pool-5-thread-3,5,main] updated the cache
Thread: Thread[pool-5-thread-14,5,main] gets to the JVM lock, Redis.get (key) is not empty, Thread[pool-5-thread-3,5,main] updated the cache
Thread: Thread[pool-5-thread-26,5,main] gets to the JVM lock, Redis.get (key) is not empty, Thread[pool-5-thread-3,5,main] updated the cache
Thread: Thread[pool-5-thread-53,5,main] gets to the JVM lock, Redis.get (key) is not empty, Thread[pool-5-thread-3,5,main] updated the cache
Thread: Thread[pool-5-thread-18,5,main] gets to the JVM lock, Redis.get (key) is not empty, Thread[pool-5-thread-3,5,main] updated the cache
Thread: Thread[pool-5-thread-61,5,main] gets to the JVM lock, Redis.get (key) is not empty, Thread[pool-5-thread-3,5,main] updated the cache
Thread: Thread[pool-5-thread-30,5,main] gets to the JVM lock, Redis.get (key) is not empty, Thread[pool-5-thread-3,5,main] updated the cache
Thread: Thread[pool-5-thread-65,5,main] gets to the JVM lock, Redis.get (key) is not empty, Thread[pool-5-thread-3,5,main] updated the cache
Thread: Thread[pool-5-thread-34,5,main] gets to the JVM lock, Redis.get (key) is not empty, Thread[pool-5-thread-3,5,main] updated the cache
Thread: Thread[pool-5-thread-97,5,main] gets to the JVM lock, Redis.get (key) is not empty, Thread[pool-5-thread-3,5,main] updated the cache
Thread: Thread[pool-5-thread-40,5,main] gets to the JVM lock, Redis.get (key) is not empty, Thread[pool-5-thread-3,5,main] updated the cache
Thread: Thread[pool-5-thread-91,5,main] gets to the JVM lock, Redis.get (key) is not empty, Thread[pool-5-thread-3,5,main] updated the cache
Thread: Thread[pool-5-thread-64,5,main] gets to the JVM lock, Redis.get (key) is not empty, Thread[pool-5-thread-3,5,main] updated the cache
Thread: Thread[pool-5-thread-42,5,main] gets to the JVM lock, Redis.get (key) is not empty, Thread[pool-5-thread-3,5,main] updated the cache
Thread: Thread[pool-5-thread-46,5,main] gets to the JVM lock, Redis.get (key) is not empty, Thread[pool-5-thread-3,5,main] updated the cache
Thread: Thread[pool-5-thread-50,5,main] gets to the JVM lock, Redis.get (key) is not empty, Thread[pool-5-thread-3,5,main] updated the cache
Thread: Thread[pool-5-thread-87,5,main] gets to the JVM lock, Redis.get (key) is not empty, Thread[pool-5-thread-3,5,main] updated the cache
Thread: Thread[pool-5-thread-85,5,main] gets to the JVM lock, Redis.get (key) is not empty, Thread[pool-5-thread-3,5,main] updated the cache
Thread: Thread[pool-5-thread-44,5,main] gets to the JVM lock, Redis.get (key) is not empty, Thread[pool-5-thread-3,5,main] updated the cache
Thread: Thread[pool-5-thread-75,5,main] gets to the JVM lock, Redis.get (key) is not empty, Thread[pool-5-thread-3,5,main] updated the cache
Thread: Thread[pool-5-thread-48,5,main] gets to the JVM lock, Redis.get (key) is not empty, Thread[pool-5-thread-3,5,main] updated the cache
Thread: Thread[pool-5-thread-71,5,main] gets to the JVM lock, Redis.get (key) is not empty, Thread[pool-5-thread-3,5,main] updated the cache
Thread: Thread[pool-5-thread-77,5,main] gets to the JVM lock, Redis.get (key) is not empty, Thread[pool-5-thread-3,5,main] updated the cache
Thread: Thread[pool-5-thread-52,5,main] gets to the JVM lock, Redis.get (key) is not empty, Thread[pool-5-thread-3,5,main] updated the cache
Thread: Thread[pool-5-thread-99,5,main] gets to the JVM lock, Redis.get (key) is not empty, Thread[pool-5-thread-3,5,main] updated the cache
Thread: Thread[pool-5-thread-93,5,main] gets to the JVM lock, Redis.get (key) is not empty, Thread[pool-5-thread-3,5,main] updated the cache
Thread: Thread[pool-5-thread-56,5,main] gets to the JVM lock, Redis.get (key) is not empty, Thread[pool-5-thread-3,5,main] updated the cache
Thread: Thread[pool-5-thread-60,5,main] gets to the JVM lock, Redis.get (key) is not empty, Thread[pool-5-thread-3,5,main] updated the cache
Thread: Thread[pool-5-thread-95,5,main] gets to the JVM lock, Redis.get (key) is not empty, Thread[pool-5-thread-3,5,main] updated the cache
Thread: Thread[pool-5-thread-89,5,main] gets to the JVM lock, Redis.get (key) is not empty, Thread[pool-5-thread-3,5,main] updated the cache
Thread: Thread[pool-5-thread-81,5,main] gets to the JVM lock, Redis.get (key) is not empty, Thread[pool-5-thread-3,5,main] updated the cache
Thread: Thread[pool-5-thread-73,5,main] gets to the JVM lock, Redis.get (key) is not empty, Thread[pool-5-thread-3,5,main] updated the cache
Thread: Thread[pool-5-thread-68,5,main] gets to the JVM lock, Redis.get (key) is not empty, Thread[pool-5-thread-3,5,main] updated the cache
Thread: Thread[pool-5-thread-58,5,main] gets to the JVM lock, Redis.get (key) is not empty, Thread[pool-5-thread-3,5,main] updated the cache
Thread: Thread[pool-5-thread-62,5,main] gets to the JVM lock, Redis.get (key) is not empty, Thread[pool-5-thread-3,5,main] updated the cache
Thread: Thread[pool-5-thread-66,5,main] gets to the JVM lock, Redis.get (key) is not empty, Thread[pool-5-thread-3,5,main] updated the cache
Thread: Thread[pool-5-thread-38,5,main] gets to the JVM lock, Redis.get (key) is not empty, Thread[pool-5-thread-3,5,main] updated the cache
Thread: Thread[pool-5-thread-54,5,main] gets to the JVM lock, Redis.get (key) is not empty, Thread[pool-5-thread-3,5,main] updated the cache
Thread: Thread[pool-5-thread-94,5,main] gets to the JVM lock, Redis.get (key) is not empty, Thread[pool-5-thread-3,5,main] updated the cache
Thread: Thread[pool-5-thread-83,5,main] gets to the JVM lock, Redis.get (key) is not empty, Thread[pool-5-thread-3,5,main] updated the cache
Thread: Thread[pool-5-thread-96,5,main] gets to the JVM lock, Redis.get (key) is not empty, Thread[pool-5-thread-3,5,main] updated the cache
Thread: Thread[pool-5-thread-79,5,main] gets to the JVM lock, Redis.get (key) is not empty, Thread[pool-5-thread-3,5,main] updated the cache
Thread: Thread[pool-5-thread-92,5,main] gets to the JVM lock, Redis.get (key) is not empty, Thread[pool-5-thread-3,5,main] updated the cache
Thread: Thread[pool-5-thread-90,5,main] gets to the JVM lock, Redis.get (key) is not empty, Thread[pool-5-thread-3,5,main] updated the cache
Thread: Thread[pool-5-thread-80,5,main] gets to the JVM lock, Redis.get (key) is not empty, Thread[pool-5-thread-3,5,main] updated the cache
Thread: Thread[pool-5-thread-82,5,main] gets to the JVM lock, Redis.get (key) is not empty, Thread[pool-5-thread-3,5,main] updated the cache
Thread: Thread[pool-5-thread-72,5,main] gets to the JVM lock, Redis.get (key) is not empty, Thread[pool-5-thread-3,5,main] updated the cache
Thread: Thread[pool-5-thread-78,5,main] gets to the JVM lock, Redis.get (key) is not empty, Thread[pool-5-thread-3,5,main] updated the cache
Thread: Thread[pool-5-thread-100,5,main] gets to the JVM lock, Redis.get (key) is not empty, Thread[pool-5-thread-3,5,main] updated the cache
Thread: Thread[pool-5-thread-70,5,main] gets to the JVM lock, Redis.get (key) is not empty, Thread[pool-5-thread-3,5,main] updated the cache
Thread: Thread[pool-5-thread-88,5,main] gets to the JVM lock, Redis.get (key) is not empty, Thread[pool-5-thread-3,5,main] updated the cache
Thread: Thread[pool-5-thread-84,5,main] gets to the JVM lock, Redis.get (key) is not empty, Thread[pool-5-thread-3,5,main] updated the cache
Thread: Thread[pool-5-thread-98,5,main] gets to the JVM lock, Redis.get (key) is not empty, Thread[pool-5-thread-3,5,main] updated the cache
Thread: Thread[pool-5-thread-86,5,main] gets to the JVM lock, Redis.get (key) is not empty, Thread[pool-5-thread-3,5,main] updated the cache
Thread: Thread[pool-5-thread-76,5,main] gets to the JVM lock, Redis.get (key) is not empty, Thread[pool-5-thread-3,5,main] updated the cache
Thread: Thread[pool-5-thread-74,5,main] gets to the JVM lock, Redis.get (key) is not empty, Thread[pool-5-thread-3,5,main] updated the cache

By observing the log, we found that only 2 times need to obtain the distributed lock, the other is blocked by the JVM lock to the outside, in this case can greatly improve the performance of the lock.


Using zookeeper to implement distributed locks across JVMs (optimized ideation) for Java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.