Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Consensus in Distributed Systems

Daniel Upton
September 25, 2018

Consensus in Distributed Systems

We rely ever more heavily on distributed systems in our daily lives, from spending money on a debit card to posting a tweet to our followers (tweeple?). We’ll dive into the challenges in building such systems identified by the CAP theorem, and take a look at a solution offered by “Raft” the consensus algorithm at the core of projects such as Consul, etcd and CockroachDB.

Image Credits:

Photo of L Peter Deutsch - Parma Recordings (source: https://parmarecordings-news.com/the-inside-story-coro-del-mundo-moto-bello-and-l-peter-deutsch/)

Photo of Eric Brewer - CC BY-SA 4.0 (source: https://en.wikipedia.org/wiki/Eric_Brewer_(scientist)#/media/File:TNW_Con_EU15_-_Eric_Brewer_(scientist)-2.jpg)

Raft Logo - CC 3.0 (source: https://raft.github.io/)

Rest of the Owl Meme - (source: https://www.reddit.com/r/funny/comments/eccj2/how_to_draw_an_owl/)

Daniel Upton

September 25, 2018
Tweet

Other Decks in Programming

Transcript

  1. CAP theorem (paraphrased) Eric Brewer When operating in a catastrophically

    broken or unreliable network a distributed system must choose to either risk returning stale/outdated data or refuse to accept writes/updates
  2. CAP theorem (paraphrased) Eric Brewer When operating in a catastrophically

    broken or unreliable network (Partition Tolerance) a distributed system must choose to either risk returning stale/outdated data (Availability) or refuse to accept writes/updates (Consistency)
  3. best_programming_language = Go current_year = 2018 linux_on_desktop = Maybe State

    Machine best_programming_language = Go SET current_year = 2018 SET
  4. every node starts off as a Follower if a follower

    doesn’t hear from a leader for a while (random timer) it becomes a Candidate if the candidate receives votes from a majority of nodes it will become the Leader
  5. 1. Client sends a command to the Leader. 2. Leader

    appends an entry to its own log. 3. Leader issues an RPC (AppendEntries) to each Follower. 4. Follower appends the entry to its log and responds to the Leader to acknowledge the entry. 5. Once the entry has been acknowledged by a majority of Followers the Leader responds to the Client. 6. Leader issues a heartbeat RPC (AppendEntries) to each Follower which “commits” the entry and applies it to each Follower’s state machine.
  6. Safety Guarantees Election Safety Only a single leader will be

    elected in each term. Append Only Leaders The leader will never delete or overwrite entries. Log Matching Any two logs with an entry of the same index and term, will contain the same value. Leader Completeness An entry committed in an earlier term will be present in the logs of leaders in later terms. State Machine Safety If a log entry at a given index has been applied to a server’s state machine, no other server will ever apply a different log entry at the same index.
  7. 2 2 2 1 Preventing Split-Brain X=1 X=1 X=1 X=2

    1 X=2 AppendEntries Term: 1 X = 2
  8. 2 2 2 1 Preventing Split-Brain X=1 X=1 X=1 X=2

    2 AppendEntries Term: 2 X = 1