

{"id":20668,"date":"2018-07-23T04:21:44","date_gmt":"2018-07-23T04:21:44","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=20668"},"modified":"2021-05-19T18:24:05","modified_gmt":"2021-05-19T12:54:05","slug":"zookeeper-leader-election","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/zookeeper-leader-election\/","title":{"rendered":"Zookeeper Leader Election: How is Leader Elected?"},"content":{"rendered":"<p><span style=\"font-weight: 400\">In our last Apache <strong>ZooKeeper tutorial<\/strong>, we discussed <strong>ZooKeeper Workflow<\/strong>. Today, we will see the ZooKeeper Leader Election. Basically, an ensemble of servers chooses a leader to order client requests that change the ZooKeeper state. <\/span><\/p>\n<p><span style=\"font-weight: 400\">Though there is a complete process for it. So, in this Zookeeper article, we will learn the whole process of Leader election in ZooKeeper in detail. Moreover, we will learn how is leader elected in ZooKeeper.<\/span><\/p>\n<p>So, let&#8217;s start ZooKeeper Leader Election.<\/p>\n<h2><span style=\"font-weight: 400\">What is Zookeeper Leader Election?<\/span><\/h2>\n<p><span style=\"font-weight: 400\"><em>A\u00a0server that has been chosen by an ensemble of servers, is what we call a Leader<\/em>.\u00a0Also, that leader continues to have support from that ensemble. Basically, to order client requests that change the ZooKeeper state is the main purpose of Leader. <\/span><\/p>\n<p><span style=\"font-weight: 400\">Zookeeper State includes create, setData, and delete.\u00a0Also, it transforms each request into a transaction. Furthermore, it proposes to the followers that the ensemble accepts and applies them in the order issued by the leader.<\/span><\/p>\n<p><span style=\"font-weight: 400\">However, it enters the ELECTION state, when a process starts. And, the process tries to elect a new leader or become a leader, in this state.\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400\">Hence, the process\u00a0moves to the FOLLOWING state and begins to follow the leader when it finds an elected leader. So, we can say the processes in the FOLLOWING state are followers. <\/span><\/p>\n<p><span style=\"font-weight: 400\">Moreover, the process moves to the LEADING state and becomes the leader, if the process is the leader after the election.\u00a0<\/span><\/p>\n<h2><span style=\"font-weight: 400\">How is Leader elected in Zookeeper? <\/span><\/h2>\n<p><span style=\"font-weight: 400\">While creating znodes that represent &#8220;proposals&#8221; of clients,\u00a0a simple way of doing leader election with ZooKeeper is to use the SEQUENCE|EPHEMERAL flags. Its basic concept is to have a znode, say &#8220;\/election&#8221;, such that each znode creates a child znode &#8220;\/election\/n_&#8221; with both flags SEQUENCE|EPHEMERAL. <\/span><\/p>\n<p><span style=\"font-weight: 400\">A\u00a0sequence number\u00a0which is\u00a0higher than anyone previously appended to a child of &#8220;\/election&#8221;, that is automatically appended\u00a0by Zookeeper, with the sequence flag. In other words, Leader is\u00a0a process\u00a0which created the znode with the smallest appended sequence number.<\/span><\/p>\n<p><span style=\"font-weight: 400\">Although make sure it is must watch for failures of the leader. Because if in the case the current leader fails, that helps a new client arises as the new leader. <\/span><\/p>\n<p><span style=\"font-weight: 400\">There is one other solution for that. It is\u00a0that to have all application processes\u00a0keeping an eye upon the current smallest Znode, and also keep checking that\u00a0when the smallest Znode goes away if they are the new leader.\u00a0 Although this results in a herd effect. <\/span><\/p>\n<p><span style=\"font-weight: 400\">That is\u00a0all other processes receive a notification and execute getChildren on &#8220;\/election&#8221; to obtain the current list of children of &#8220;\/election&#8221;, upon of failure of the current leader. Also,\u00a0it causes a spike in the number of operations that ZooKeeper servers have to process, if the number of clients is large.\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400\">The pseudo code for ZooKeeper Leader Election is:<\/span><\/p>\n<p>Let&#8217;s suppose ELECTION be a path of choice of the application. So, in order to volunteer to be a leader:<\/p>\n<pre class=\"EnlighterJSRAW\">with path \"ELECTION\/n_\", Create Znode z  with both SEQUENCE and EPHEMERAL flags;\r\nThen let's suppose i be the sequence number of z and C be the children of \"ELECTION\";\r\nAlso, where j is the largest sequence number such that j &lt; i and n_j is a Znode in C, watch for changes on \"ELECTION\/n_j\";<\/pre>\n<p><span style=\"font-weight: 400\">Further, upon receiving a notification of Znode deletions:<\/span><br \/>\n<span style=\"font-weight: 400\">Let&#8217;s suppose C be the new set of children of ELECTION;<\/span><br \/>\n<span style=\"font-weight: 400\">Then execute leader procedure, if z is the smallest node in C;<\/span><\/p>\n<p><span style=\"font-weight: 400\">Else, where j is the largest sequence number such that j &lt; i and n_j is a Znode in C, watch for changes on &#8220;ELECTION\/n_j&#8221;;<\/span><\/p>\n<p><span style=\"font-weight: 400\">However, make sure that the Znode having no preceding znode on the list of children. Though it says it does not imply that the creator of this znode is aware that it is the current leader. <\/span><\/p>\n<p><span style=\"font-weight: 400\">And, to acknowledge that the leader has executed the leader procedure, applications may consider creating a separate Znode.<br \/>\n<\/span><br \/>\nSo, this was all in Apache ZooKeeper leader election. Hope you like our explanation.<\/p>\n<h2><span style=\"font-weight: 400\">Conclusion<\/span><\/h2>\n<p>Hence, in this ZooKeeper leader election tutorial, we have seen the whole Leader election in Apache ZooKeeper in detail. Moreover, we discussed how is leader elected in ZooKeeper. Still, if any doubt occurs regarding the process of ZooKeeper election, feel free to ask in the comment section.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In our last Apache ZooKeeper tutorial, we discussed ZooKeeper Workflow. Today, we will see the ZooKeeper Leader Election. Basically, an ensemble of servers chooses a leader to order client requests that change the ZooKeeper&#46;&#46;&#46;<\/p>\n","protected":false},"author":7,"featured_media":20794,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[77],"tags":[986,5899,8125,10153,16395,16422],"class_list":["post-20668","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-zookeeper","tag-apache-zookeeper-tutorial","tag-how-is-leader-elected-in-zookeeper","tag-leader-election-in-zookeeper","tag-process-of-zookeeper-leader-election","tag-zookeeper-leader-election","tag-zookeeper-tutorial"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Zookeeper Leader Election: How is Leader Elected? - DataFlair<\/title>\n<meta name=\"description\" content=\"In this Zookeeper article, we will learn the whole process of Zookeeper Leader election(to order client requests that change the ZooKeeper state) 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-leader-election\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Zookeeper Leader Election: How is Leader Elected? - DataFlair\" \/>\n<meta property=\"og:description\" content=\"In this Zookeeper article, we will learn the whole process of Zookeeper Leader election(to order client requests that change the ZooKeeper state) in detail.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/zookeeper-leader-election\/\" \/>\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-23T04:21:44+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-05-19T12:54:05+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/Zookeeper-Leader-Election-01.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 Leader Election: How is Leader Elected? - DataFlair","description":"In this Zookeeper article, we will learn the whole process of Zookeeper Leader election(to order client requests that change the ZooKeeper state) 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-leader-election\/","og_locale":"en_US","og_type":"article","og_title":"Zookeeper Leader Election: How is Leader Elected? - DataFlair","og_description":"In this Zookeeper article, we will learn the whole process of Zookeeper Leader election(to order client requests that change the ZooKeeper state) in detail.","og_url":"https:\/\/data-flair.training\/blogs\/zookeeper-leader-election\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2018-07-23T04:21:44+00:00","article_modified_time":"2021-05-19T12:54:05+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/Zookeeper-Leader-Election-01.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-leader-election\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/zookeeper-leader-election\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/beb0cab24b7aa54423a3b50e669a9dcd"},"headline":"Zookeeper Leader Election: How is Leader Elected?","datePublished":"2018-07-23T04:21:44+00:00","dateModified":"2021-05-19T12:54:05+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/zookeeper-leader-election\/"},"wordCount":679,"commentCount":0,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/zookeeper-leader-election\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/Zookeeper-Leader-Election-01.jpg","keywords":["Apache zookeeper tutorial","how is leader elected in ZooKeeper","Leader election in ZooKeeper","process of ZooKeeper Leader election","Zookeeper Leader election","zookeeper tutorial"],"articleSection":["Zookeeper Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/zookeeper-leader-election\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/zookeeper-leader-election\/","url":"https:\/\/data-flair.training\/blogs\/zookeeper-leader-election\/","name":"Zookeeper Leader Election: How is Leader Elected? - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/zookeeper-leader-election\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/zookeeper-leader-election\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/Zookeeper-Leader-Election-01.jpg","datePublished":"2018-07-23T04:21:44+00:00","dateModified":"2021-05-19T12:54:05+00:00","description":"In this Zookeeper article, we will learn the whole process of Zookeeper Leader election(to order client requests that change the ZooKeeper state) in detail.","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/zookeeper-leader-election\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/zookeeper-leader-election\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/zookeeper-leader-election\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/Zookeeper-Leader-Election-01.jpg","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/Zookeeper-Leader-Election-01.jpg","width":1200,"height":628,"caption":"Zookeeper Leader Election: How is Leader Elected?"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/zookeeper-leader-election\/#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 Leader Election: How is Leader Elected?"}]},{"@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\/20668","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=20668"}],"version-history":[{"count":7,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/20668\/revisions"}],"predecessor-version":[{"id":94224,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/20668\/revisions\/94224"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/20794"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=20668"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=20668"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=20668"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}