Slide 1

Slide 1 text

Tim McGilchrist @lambda_foo lambdafoo.com Raft Implementing Distributed Consensus with Erlang

Slide 2

Slide 2 text

Outline ❖ Goals! ❖ The consensus problem! ❖ Outline RAFT algorithm! ❖ Implementing in Erlang

Slide 3

Slide 3 text

Goals What I want you to get out of this talk?! • Understand core ideas in RAFT! • Erlang / OTP as a tool for building systems! • Build your own implementation

Slide 4

Slide 4 text

Consensus In a distributed system, agreement among multiple processes on a single data value, despite failures. ! ! Once they reach a decision on a value, that decision is final.

Slide 5

Slide 5 text

Potential Use Case ❖ Configuration Management! ❖ Distributed Transactions! ❖ Distributed Lock Manager! ❖ DNS and Resource Discovery

Slide 6

Slide 6 text

RAFT ❖ Design for understandability! ❖ Strong leader! ❖ Practical to implement Goals Raft is a consensus algorithm that is designed to be easy to understand.

Slide 7

Slide 7 text

Messages ❖ RAFT only needs 2 messages.! ❖ RequestVote includes term! ❖ AppendEntries includes term and log entries! ❖ Term acts as a logical clock

Slide 8

Slide 8 text

States 3 states a node can be in. Follower Candidate! Leader

Slide 9

Slide 9 text

Leader Leader • Only a single leader within a cluster! • Receives commands from client! • Commits commands to the log

Slide 10

Slide 10 text

Follower Follower • Appends commands to log! • Votes for candidates! • Otherwise passive

Slide 11

Slide 11 text

Candidate Candidate! • Initiates Election! • Coordinates Votes

Slide 12

Slide 12 text

Leader Election Follower Candidate! Leader starts up timeout new election gets majority of votes step down step down timeout restart election

Slide 13

Slide 13 text

Log Replication add 1 F2 F1 Leader AppendEntries add 1, index 0 add 1, index 0

Slide 14

Slide 14 text

Log Replication add 1 add 1 add 1 F2 F1 Leader Ok OK

Slide 15

Slide 15 text

Log Replication add 1 add 1 add 1 F2 F1 Leader Executes command

Slide 16

Slide 16 text

Log Replication add 1 add 4 add 1 add 1 F2 F1 Leader AppendEntries add 4, index 1 add 4, index 1 Executes command Executes command

Slide 17

Slide 17 text

RAFT Summary ❖ 2 types of messages, RequestVote and AppendEntries! ❖ 3 states, Leader, Follower and Candidate! ❖ Save Entries to persistent log

Slide 18

Slide 18 text

Erlang ❖ Functional language! ❖ Fundamentally a concurrent language! ❖ Actor model as basic abstraction! ❖ No shared state between actors! ❖ OTP behaviours like supervisors and gen_fsm! ❖ Location independent message sending

Slide 19

Slide 19 text

Implementation Overview ❖ github.com/tmcgilchrist/sloop ! ❖ github.com/andrewjstone/ rafter! ❖ Each node has 2 supervised behaviours! ❖ gen_fsm implementing the consensus protocol! ❖ gen_server wraps the log store! ❖ passes erlang terms as messages

Slide 20

Slide 20 text

sloop_fsm ❖ state machine implements leader election and log replication! ❖ each state is a function with multiple clauses! !

Slide 21

Slide 21 text

Supervisors sloop_sup sloop_fsm sloop_store sloop_state sender

Slide 22

Slide 22 text

Implementations ! ❖ raftconsensus.github.io! ❖ github.com/tmcgilchrist/sloop ! ❖ github.com/andrewjstone/rafter ! ❖ github.com/goraft/raft

Slide 23

Slide 23 text

Summary ❖ Defined Distributed Consensus! ❖ Looked at core ideas of RAFT! ❖ Erlang suits distributed systems! ❖ Map Erlang to RAFT

Slide 24

Slide 24 text

Thanks!