2022-03-23

Raft

Server States:

At any given time, each server is is either:

Time is represented in terms:

Terms: - Election - Normal operation under a single leader

Heartbeats and Timeouts:

Election Basics:

#----------------------------------------------------------------------
#   Log Structure: 
#----------------------------------------------------------------------

        Leader
Term  ┌──────┬──────┬──────┬──────┬──────┬──────┐
─────►│  1   │  1   │  1   │  2   │  3   │  3   │
Cmd   │      │      │      │      │      │      │
─────►│ add  │ cmp  │ ret  │ mov  │ jmp  │ div  │
      └──────┴──────┴──────┴──────┴──────┴──────┘

         Followers
Term  ┌──────┬──────┬──────┬──────┬──────┬──────┐
─────►│  1   │  1   │  1   │  2   │  3   │  3   │
Cmd   │      │      │      │      │      │      │
─────►│ add  │ cmp  │ ret  │ mov  │ jmp  │ div  │
      └──────┴──────┴──────┴──────┴──────┴──────┘
Term  ┌──────┬──────┬──────┬──────┬──────┐
─────►│  1   │  1   │  1   │  2   │  3   │
Cmd   │      │      │      │      │      │
─────►│ add  │ cmp  │ ret  │ mov  │ jmp  │
      └──────┴──────┴──────┴──────┴──────┘
Term  ┌──────┬──────┬──────┐
─────►│  1   │  1   │  1   │
Cmd   │      │      │      │
─────►│ add  │ cmp  │ ret  │
      └──────┴──────┴──────┘
      │                    │              │     │
      └────────────────────┴──────────────┴─────┘
        Committed entries per follower

#----------------------------------------------------------------------
#   Normal Operation
#----------------------------------------------------------------------
#----------------------------------------------------------------------
#   Log Consistency
#----------------------------------------------------------------------
         1      2      3      4      5
      ┌──────┬──────┬──────┬──────┬──────┐
      │  1   │  1   │  1   │  2   │  3   │
      │ add  │ cmp  │ ret  │ mov  │ jmp  │
      └──────┴──────┴──────┴──────┴──────┘
      ┌──────┬──────┬──────┬──────┬──────┐
      │  1   │  1   │  1   │  2   │  4   │
      │ add  │ cmp  │ ret  │ mov  │ sub  │
      └──────┴──────┴──────┴──────┴──────┘
                                    ^^^^^ Different to leader! 

Raft maintains a high level of coherency between logs

AppendEntries Consistency Check

#----------------------------------------------------------------------
#   Leader Changes
#----------------------------------------------------------------------

At the beginning of the new leader's term, they have some responsibilities to fulfil:

#----------------------------------------------------------------------
#   Safety Requirement
#----------------------------------------------------------------------

Once a log entry has been applied to a state machine,no other state machine must apply a different value for that log entry.

This guarantees the safety requirement:

-> Exclude a server from becoming a leader if it does not have all the required log entries.

#----------------------------------------------------------------------
#   Picking the best leader
#----------------------------------------------------------------------
         1      2      3      4      5
      ┌──────┬──────┬──────┬──────┬──────┐
      │  1   │  1   │  1   │  2   │  3   │
      │ add  │ cmp  │ ret  │ mov  │ jmp  │
    s1└──────┴──────┴──────┴──────┴──────┘

      ┌──────┬──────┬──────┬──────┬
      │  1   │  1   │  1   │  2   │
      │ add  │ cmp  │ ret  │ mov  │
    s2└──────┴──────┴──────┴──────┴

    --------------------------------------------------------------

    s3 is not available at election time, so we cannot tell that the
    last entry to s1 is committed
      ┌──────┬──────┬──────┬──────┬──────┐
      │  1   │  1   │  1   │  2   │  3   │
      │ add  │ cmp  │ ret  │ mov  │ jmp  │
    s3└──────┴──────┴──────┴──────┴──────┘
      
        (lastTerm_v > lastTerm_c) || 
        (lastTerm_v == lastTerm_c) && (lastIndex_v > lastIndex_c) 
- Leader will have the "most complete" log among the electing
  majority
#----------------------------------------------------------------------
#   New Commitment Rules
#----------------------------------------------------------------------
#----------------------------------------------------------------------
#   Repairing Follower Logs
#----------------------------------------------------------------------
# -----------------------------------------------------------------------
# Raft -> append to main notes
# -----------------------------------------------------------------------
# -----------------------------------------
# Neutralising Old Leaders
# -----------------------------------------
# -----------------------------------------
# Client Protocol
# -----------------------------------------
# -----------------------------------------
# Configuration Changes
# -----------------------------------------

What is in the scope of 'configuration'?

# -----------------------------------------
# Joint Consensus
# -----------------------------------------

Raft uses a 2-phase approach: - Intermediate phase uses join consensus (need majority of both old and new configurations for elections, commitment) - Configuration change is just a log entry; applied immediately on receipt (committed or not) - Once joint consensus is committed, begin replicating log entry for final configuration Additionally:

# -----------------------------------------
# Raft Summary
# -----------------------------------------
  1. Leader election
  2. Normal Operation
  3. Safety and consistency
  4. Neutralise old leaders
  5. Client Protocol
  6. Configuration Changes

Return to Index