title: How to elect a master node among the nodes running in a cluster?
body: <p>I'm writing a managed cloud stack (on top of hardware-level cloud providers like EC2), and a problem I will face soon is:</p>
  
  <p>How do several identical nodes decide which one of them becomes a master? (I.e. think of 5 servers running on EC2. One of them has to become a master, and other ones have to become slaves.)</p>
  
  <p>I read a description of <a href="http://www.mongodb.org/display/DOCS/Replica+Set+Internals" rel="noreferrer">the algorithm used by MongoDB</a>, and it seems pretty complicated, and also depends on a concept of votes — i.e. two nodes left alone won't be able to decide anything. Also their approach has a significant delay before it produces the results.</p>
  
  <ol>
  <li><p>I wonder if there are any less complicated, KISS-embrasing approaches? Are they used widely, or are they risky to adopt?</p></li>
  <li><p>Suppose we already have a list of servers. Then we can just elect the one that is up and has a numerically smallest IP address. What are downsides of this approach?</p></li>
  <li><p>Why is MongoDB's algorithm so complicated?</p></li>
  </ol>
  
  <p>This is a duplicate of <a href="https://stackoverflow.com/questions/3085926/how-to-elect-new-master-in-cluster">How to elect new Master in Cluster?</a>, which gives less details and has not been answered for 6 months, so I feel it is appropriate to start a new question.</p>
  
  <p>(The stack I'm working on is open-source, but it's on a very early stage of development so not giving a link here.)</p>
  
  <p>UPDATE: based on the answers, I have designed a simple consensus algorithm, you can find a JavaScript (CoffeeScript) implementation on GitHub: <a href="https://github.com/andreyvit/majority.js" rel="noreferrer">majority.js</a>.</p>