title: ZooKeeper Recipes and Apache Curator
body: <p>I am trying to understand <em>exactly</em> what types of problems Apache ZooKeeper ("ZK") solves, and perhaps their <a href="https://zookeeper.apache.org/doc/r3.4.11/recipes.html" rel="nofollow noreferrer">Recipes page</a> is the best place to start.</p>
  
  <p>First off, I am making the following <em>assumptions</em>:</p>
  
  <ul>
  <li>The ZooKeeper API (available in both Java and C) exposes <a href="http://zookeeper.apache.org/doc/r3.4.6/zookeeperOver.html#Simple+API" rel="nofollow noreferrer">these 7 simple methods</a> which then allow you to build up your own usage patterns, known as "ZK Recipes"</li>
  <li>It is then up to you to use these ZK Recipes to solve problems in distributed programming yourself</li>
  <li>Or, instead of building up your own ZK Recipes, you could just use the ones that ship with <a href="http://curator.apache.org/index.html" rel="nofollow noreferrer">Apache Curator</a></li>
  <li>So either way, you're using ZK Recipes (again, homegrown or provided by Curator) to solve distributed computing problems</li>
  </ul>
  
  <p>I believe <a href="http://kafka.apache.org/" rel="nofollow noreferrer">Apache Kafka</a> is an example of this, where Kafka uses ZK to create a <a href="http://zookeeper.apache.org/doc/trunk/recipes.html#sc_recipes_Queues" rel="nofollow noreferrer">distributed Queue</a> (which is one of the listed ZK Recipes). So if my assumptions are correct, ZK exposes those API methods, and the creators of Apache Kafka either used ZK directly or used Curator to implement the "Queue" ZK Recipe.</p>
  
  <p>If any of my above assumptions are wrong, please begin by correcting me! Assuming I'm more or less on track:</p>
  
  <p>Looking at the list of ZK Recipes, I see the following (non-exhaustive):</p>
  
  <ul>
  <li>Barriers</li>
  <li>Locks</li>
  <li>Leader Election</li>
  </ul>
  
  <p>In order for me to appreciate these recipes and the solutions they present, I first need to appreciate the problem that they solve! I understand what a lock is from basic Java concurrency, but I'm just not seeing the use case for when a "distributed Lock" would ever be necessary. For leading election, all I can think of - as a use case for <em>needing</em> it in the first place - would be if you were building an application that you wanted to ship with a built-in master/slave or primary/secondary capability. Perhaps in that case you would use ZK to implement your own "Leader Election" recipe, or perhaps just use Curator's <a href="http://curator.apache.org/curator-recipes/leader-latch.html" rel="nofollow noreferrer">Leader Latch</a> out of the box. As for Barriers, I don't see how those are any different than Locks. So I ask:</p>
  
  <ul>
  <li>Is my master/slave or primary/secondary problem an accurate use case for ZK's Leader Election recipe?</li>
  <li>What would be an example of a distributed Lock? What problem(s) does it solve?</li>
  <li>Ditto for Barriers: and what's the difference between Locks and Barriers?</li>
  </ul>