

{"id":21227,"date":"2018-07-26T04:04:13","date_gmt":"2018-07-26T04:04:13","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=21227"},"modified":"2021-05-19T18:24:01","modified_gmt":"2021-05-19T12:54:01","slug":"zookeeper-locks","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/zookeeper-locks\/","title":{"rendered":"Zookeeper Locks &#8211; Shared &amp; Recoverable Shared Locks"},"content":{"rendered":"<p><span style=\"font-weight: 400\">In our last <strong>ZooKeeper tutorial<\/strong>, we discussed the <strong>ZooKeeper Data Model<\/strong>. Today, we will see Zookeeper Locks. Moreover, we will discuss shared locks and recoverable shared locks in ZooKeeper. Also, we will see how to implement shared locks in ZooKeeper.\u00a0<\/span><\/p>\n<p>So, let&#8217;s start ZooKeeper Locks.<\/p>\n<h2><span style=\"font-weight: 400\">What is Zookeeper Locks?<\/span><\/h2>\n<p><span style=\"font-weight: 400\"><em>Zookeeper Locks are fully distributed locks in ZooKeeper which are globally synchronous<\/em>. However, globally synchronous means at any snapshot in time no two clients think they hold the same lock. <\/span><\/p>\n<p><span style=\"font-weight: 400\">Though these we can implement these locks by using ZooKeeper. Moreover, we can say first define a lock node, as with priority <strong>ZooKeeper Queues<\/strong>.<\/span><\/p>\n<p><span style=\"font-weight: 400\">Now do the following, to obtain a lock in ZooKeeper:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">With the sequence, ephemeral flags set and a pathname of &#8220;_locknode_\/lock-&#8220;, Call create( ).<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">Further, without setting the watch flag, Call getChildren( ) on the lock node. <\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">Moreover, the client has the lock and the client exits the protocol if the pathname created in step 1 has the lowest sequence number suffix.<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">Also, with the watch flag set on the path in the lock directory, the client calls exists( ) with the next lowest sequence number.<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">Else, go to step 2, if exists( ) returns false. Or rather, before going to step 2 wait for a notification for the pathname from the previous step.<\/span><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400\">However, clients wishing to release a lock in ZooKeeper simply delete the node they created in step 1, to unlock.<\/span><\/p>\n<p><span style=\"font-weight: 400\">Also, notice these few things in ZooKeeper Locks:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">Since each node is watched by exactly one client, the removal of a node will only cause one client to wake up. In this way, you avoid the herd effect.<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">Moreover, there is no polling or timeouts.<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">Also, it is easy to see the amount of lock contention, debug locking problems, break locks, etc, because of the way\u00a0we implement locking.<\/span><\/li>\n<\/ul>\n<h2><span style=\"font-weight: 400\">Shared Locks<\/span><\/h2>\n<p><span style=\"font-weight: 400\">By\u00a0making some changes to the ZooKeeper Lock protocol, it is possible to implement shared locks in ZooKeeper:<\/span><\/p>\n<div id=\"attachment_21283\" style=\"width: 1210px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/Zookeeper-Shared-Locks-01.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-21283\" class=\"wp-image-21283 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/Zookeeper-Shared-Locks-01.jpg\" alt=\"ZooKeeper Locks\" width=\"1200\" height=\"628\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/Zookeeper-Shared-Locks-01.jpg 1200w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/Zookeeper-Shared-Locks-01-150x79.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/Zookeeper-Shared-Locks-01-300x157.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/Zookeeper-Shared-Locks-01-768x402.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/Zookeeper-Shared-Locks-01-1024x536.jpg 1024w\" sizes=\"auto, (max-width: 1200px) 100vw, 1200px\" \/><\/a><p id=\"caption-attachment-21283\" class=\"wp-caption-text\">ZooKeeper Locks &#8211;<\/p><\/div>\n<h3>a. Obtaining a Read lock<\/h3>\n<ol>\n<li><span style=\"font-weight: 400\">At first, to create a node with pathname &#8220;_locknode_\/read-&#8220;, Call create( ). Although,\u00a0don&#8217;t forget to set both the sequence and ephemeral flags.<\/span><\/li>\n<li><span style=\"font-weight: 400\">Then without setting the watch flag, call getChildren( ) on the lock node. Basically, that helps to avoid the herd effect.<\/span><\/li>\n<li><span style=\"font-weight: 400\">However, make sure the client has the lock and can exit the protocol if somehow there are no children with a pathname starting with &#8220;write-&#8221; and having a lower sequence number than the node created in step 1.<\/span><\/li>\n<li><span style=\"font-weight: 400\">Else, call exists( ), with &#8220;write-&#8221; having the next lowest sequence number set on the node in lock directory with pathname starting, also with watch flag.<\/span><\/li>\n<li><span style=\"font-weight: 400\">Go to step 2, if exists( ) returns false.<\/span><\/li>\n<li><span style=\"font-weight: 400\">Else, before going to step 2, wait for a notification for the pathname from the previous step.<\/span><\/li>\n<\/ol>\n<h3>b. Obtaining a Write Lock<\/h3>\n<ol>\n<li><span style=\"font-weight: 400\">Again, with pathname &#8220;_locknode_\/write-&#8220;, call create( ) to create a node.\u00a0And, remember to set both sequence and ephemeral flags.<\/span><\/li>\n<li><span style=\"font-weight: 400\">Further, call getChildren( ) on the lock node &#8211;\u00a0because it helps to avoid the herd effect.<\/span><\/li>\n<li><span style=\"font-weight: 400\">However, the client has the lock and the client exits the protocol if there are no children with a lower sequence number than the node created in step 1.<\/span><\/li>\n<li><span style=\"font-weight: 400\">Further, with watch flag set, call exists( ), on the node with the pathname which has the next lowest sequence number.<\/span><\/li>\n<li><span style=\"font-weight: 400\">Goto step 2, if exists( ) returns false. Else, before going to step 2, wait for a notification for the pathname from the previous step.<\/span><\/li>\n<\/ol>\n<h2><span style=\"font-weight: 400\">Recoverable Shared Locks<\/span><\/h2>\n<p><span style=\"font-weight: 400\">Further, we can make ZooKeeper Shared Locks revocable by modifying the shared lock protocol, with minor modifications to the Shared Lock Protocol in ZooKeeper:<\/span><\/p>\n<p><span style=\"font-weight: 400\">Just call getData( ) with watch set, in step 1, of both obtain reader and writer lock protocols, just after the call to create( ). <\/span><\/p>\n<p><span style=\"font-weight: 400\">So, it created in step 1, if the client subsequently receives notification for the node, moreover, with watch set and looks for the string &#8220;unlock&#8221;, it does another getData( ) on that node,\u00a0that signifies the client that now it must release the lock.<\/span><\/p>\n<p><span style=\"font-weight: 400\"> The reason behind this is, as per this shared lock protocol,\u00a0writing &#8220;unlock&#8221; to that node, we can request the client with the lock give up the lock by calling setData() on the lock node.<\/span><\/p>\n<p>So, this was all in ZooKeeper Locks. Hope you like our explanation.<\/p>\n<h2><span style=\"font-weight: 400\">Conclusion<\/span><\/h2>\n<p><span style=\"font-weight: 400\">Hence, we have seen the whole about Zookeeper Locks along with Shared Locks and Recoverable Shared Locks in detail. So, we hope this article will resolve all your doubts regarding Locks in ZooKeeper. Still, if any query, ask in the comment tab.<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In our last ZooKeeper tutorial, we discussed the ZooKeeper Data Model. Today, we will see Zookeeper Locks. Moreover, we will discuss shared locks and recoverable shared locks in ZooKeeper. Also, we will see how&#46;&#46;&#46;<\/p>\n","protected":false},"author":7,"featured_media":21276,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[77],"tags":[8371,11431,12828,16396,16418,16422],"class_list":["post-21227","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-zookeeper","tag-locks-in-zookeeper","tag-recoverable-shared-locks","tag-shared-locks","tag-zookeeper-locks","tag-zookeeper-shared-locks","tag-zookeeper-tutorial"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.0 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Zookeeper Locks - Shared &amp; Recoverable Shared Locks - DataFlair<\/title>\n<meta name=\"description\" content=\"In this article, we will learn the whole about Zookeeper locks along with Shared Locks and Recoverable Shared Locks in detail.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/zookeeper-locks\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Zookeeper Locks - Shared &amp; Recoverable Shared Locks - DataFlair\" \/>\n<meta property=\"og:description\" content=\"In this article, we will learn the whole about Zookeeper locks along with Shared Locks and Recoverable Shared Locks in detail.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/zookeeper-locks\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2018-07-26T04:04:13+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-05-19T12:54:01+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/Zookeeper-Locks-01-1-1.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"DataFlair Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DataFlair Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Zookeeper Locks - Shared &amp; Recoverable Shared Locks - DataFlair","description":"In this article, we will learn the whole about Zookeeper locks along with Shared Locks and Recoverable Shared Locks in detail.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/data-flair.training\/blogs\/zookeeper-locks\/","og_locale":"en_US","og_type":"article","og_title":"Zookeeper Locks - Shared &amp; Recoverable Shared Locks - DataFlair","og_description":"In this article, we will learn the whole about Zookeeper locks along with Shared Locks and Recoverable Shared Locks in detail.","og_url":"https:\/\/data-flair.training\/blogs\/zookeeper-locks\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2018-07-26T04:04:13+00:00","article_modified_time":"2021-05-19T12:54:01+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/Zookeeper-Locks-01-1-1.jpg","type":"image\/jpeg"}],"author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/zookeeper-locks\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/zookeeper-locks\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/beb0cab24b7aa54423a3b50e669a9dcd"},"headline":"Zookeeper Locks &#8211; Shared &amp; Recoverable Shared Locks","datePublished":"2018-07-26T04:04:13+00:00","dateModified":"2021-05-19T12:54:01+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/zookeeper-locks\/"},"wordCount":764,"commentCount":0,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/zookeeper-locks\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/Zookeeper-Locks-01-1-1.jpg","keywords":["Locks in ZooKeeper","Recoverable Shared Locks","Shared Locks","ZooKeeper Locks","ZooKeeper Shared locks","zookeeper tutorial"],"articleSection":["Zookeeper Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/zookeeper-locks\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/zookeeper-locks\/","url":"https:\/\/data-flair.training\/blogs\/zookeeper-locks\/","name":"Zookeeper Locks - Shared &amp; Recoverable Shared Locks - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/zookeeper-locks\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/zookeeper-locks\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/Zookeeper-Locks-01-1-1.jpg","datePublished":"2018-07-26T04:04:13+00:00","dateModified":"2021-05-19T12:54:01+00:00","description":"In this article, we will learn the whole about Zookeeper locks along with Shared Locks and Recoverable Shared Locks in detail.","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/zookeeper-locks\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/zookeeper-locks\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/zookeeper-locks\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/Zookeeper-Locks-01-1-1.jpg","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/Zookeeper-Locks-01-1-1.jpg","width":1200,"height":628,"caption":"ZooKeeper Locks"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/zookeeper-locks\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"Zookeeper Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/zookeeper\/"},{"@type":"ListItem","position":3,"name":"Zookeeper Locks &#8211; Shared &amp; Recoverable Shared Locks"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/beb0cab24b7aa54423a3b50e669a9dcd","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/c322416204232f4dd97ef3901b0a499a5d34d7ba7fe333f4bfe53a907873d293?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/c322416204232f4dd97ef3901b0a499a5d34d7ba7fe333f4bfe53a907873d293?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/c322416204232f4dd97ef3901b0a499a5d34d7ba7fe333f4bfe53a907873d293?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"DataFlair Team specializes in creating clear, actionable content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Backed by industry expertise, we make learning easy and career-oriented for beginners and pros alike.","url":"https:\/\/data-flair.training\/blogs\/author\/dfteam3\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/21227","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=21227"}],"version-history":[{"count":6,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/21227\/revisions"}],"predecessor-version":[{"id":94215,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/21227\/revisions\/94215"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/21276"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=21227"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=21227"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=21227"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}