Raft(Strong Consistent System)
What is Raft?
- Raft is a
consensus algorithm, where multiple nodes agree before commiting a value to system. Has
same fault tolerance as
Paxos
- Raft can be used writing content to DB. Leader & Followers agree to
commit and then data is committed to DB.
| Traditional Replication(Directly Written) | Raft Protocol (Agree then Commit) | |
|---|---|---|
| Write to DB Sequence | DB executes on Leader ➔ streamed to Slaves. | Written to Log ➔ Quorum Agreed ➔ Executed into DB. |
| Split-Brain Risk | High. If the network splits, two nodes might think they are the leader and write conflicting data to disk. | Zero. A network minority partition can never get a majority agreement, preventing conflicting writes. |
| Data Loss | Risk of Loss. If the leader writes to its DB and crashes before sending it to replicas, that data is lost or trapped. | No Loss. A write is only confirmed to the client if a majority safely saved the log. |
| Failover Orchestration | Requires external orchestrators or human intervention to promote a new slave. | Built-in & Instant. Nodes notice a missing leader and elect a new one in milliseconds automatically |
How Raft works
Leader election — one node becomes Leader; others are Followers.
There are no Slaves in Raft, its followers
Log replication — client writes Leader; Leader appends to its
Raft log and sends log copies to Followers. Yet none, Leader or Follower
writes to DB
Commit — Once majority (N/2+1) followers Acknowledge, Leader
sends message to followers to commit into DB
Safety — if the Leader dies, a new election runs; committed
entries are never lost.
sequenceDiagram
participant Client
box Leader(Node L)
participant L as RaftLayer
DBMgr
participant LDB@{ "type" : "database" } as DB
end
box Follower(Node A)
participant A as RaftLayer
DBMgr
participant ADB@{ "type" : "database" } as DB
end
box Follower(Node B)
participant B as RaftLayer
DBMgr
participant BDB@{ "type" : "database" } as DB
end
box Follower(Node C)
participant C as RaftLayer
DBMgr
participant CDB@{ "type" : "database" } as DB
end
Note over L,CDB: L wins election (3 of 4 votes) and become leader
Client->>L: INSERT INTO users...
note over L: Append into WAL Log
(Log Entry #101: INSERT INTO users...)
note over L: NOTE: Value is not
written to DB
L->>A: AppendEntries WAL Log(INSERT INTO users...)
Note over A: Value not written to DB
A-->>L: OK
L->>B: AppendEntries WAL Log(INSERT INTO users...)
Note over B: Value not written to DB
B-->>L: OK
L->>C: AppendEntries WAL Log(INSERT INTO users...)
Note over C: Value not written to DB
C does not send Ok
Note over L: Majority N/2+1
4/2+1=3 acked → commit
L ->> LDB: Apply WAL Log
(INSERT INTO users...)
L->>A: Commit
A ->> ADB: Apply WAL Log
(INSERT INTO users...)
L->>B: Commit
B ->> BDB: Apply WAL Log
(INSERT INTO users...)
L-->>Client: Success
Raft System Design
Requirements
Functional
1. Write into Raft logs(ie WAL log): System should accept commands from
client, write in Raft logs before writing to DB
2. Leader Election:
3. Consensus: N/2 + 1 nodes to send ACK and then value is written to
DB
4. Recovery: Master/Follower fails, system should recover
5. Serving Reads to clients
Non Functional
1. Fault Tolerance: Continue accepting writes until live nodes >
N/2
2. Consistent: Nodes agree on committed value and system returns the
same
3. Available