---
obj: concept
website: https://raft.github.io
wiki: https://en.wikipedia.org/wiki/Raft_(algorithm)
---

# Raft Consensus Algorithm <img src="data:image/avif;base64,AAAAHGZ0eXBhdmlmAAAAAGF2aWZtaWYxbWlhZgAAAPJtZXRhAAAAAAAAAChoZGxyAAAAAAAAAABwaWN0AAAAAAAAAAAAAAAAbGliYXZpZgAAAAAOcGl0bQAAAAAAAQAAAB5pbG9jAAAAAEQAAAEAAQAAAAEAAAEWAAAKeQAAAChpaW5mAAAAAAABAAAAGmluZmUCAAAAAAEAAGF2MDFDb2xvcgAAAABqaXBycAAAAEtpcGNvAAAAFGlzcGUAAAAAAAAAyAAAAMgAAAAQcGl4aQAAAAADDAwMAAAADGF2MUOBQGAAAAAAE2NvbHJuY2x4AAEAAgAGgAAAABdpcG1hAAAAAAAAAAEAAQQBAoMEAAAKgW1kYXQSAAoKWB3x8djQECBogDLoFBGQAkkkkUC0XrzYrNDsdi6ZtRqjzbMBUY3dKgPudKFya5aFnPVyAWaiMzdt1DPxRZJW5ck6rQfFdOKEK5nXOPURbQL8np9UQYyXioZsEEwkn4SjiPXZPBRX2X+l6MFYqVl2+9hq4vSHmpGAXfQGYzM5AOHpgUs+YBQdisxRolXzAN2inX5vwUtdXfk+Y1nnDWu697ovAWYI0Vmutx0zlhEZW7gyusQf58hCykGFN1PtYvRCyQ8VsELPI/tU0GP8rVj9YEGXqELZl5O6npqBiygssm9Z19Wxtz2ta0PpR1Osc0fZq88uxDMQ9tTVinPnpkzsJSbHQm+3O/YAazSQw7DeoYgKImenDLIuYK/YJW5PtyhKFT4kvGRlHJlMrxMZBMesMkHTiH4bmvxf14xW54B55pvxwnGsO7NwtF10rE5j2snEi5+/pf6sNjHHikGS6UhjNp6xt+6xI99RTzTBQ0hnytn2mqRUngancKPFIaUVi+aaxUyQFBuYqH60gAt/dwr74DidaQ7E7Exl6+FIyhcYkAnAlD4ZG4PbCdaFKJUlj9x2mySRAwBZou8z7Uv7JTB0cjcQSXhhDdMKfJr4S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jHREv220gQQ4tUn2yBhwA/BYVhIuBPAUN9JJsVghZw0uFevAhn7S62f+OqFR7YjTseNJM+66n2I+jpD6BiE/0H9O6rJm24jRqG0BZDmS/K9Om/pVcMRArcrN75P4FS7MDXZs1jv57z+tGfHvwgsao778t5dCvAI8MWxP1hqPc8SEp0dfdaL2xO1/jalJEFKnqKXWurzlDvF+rkngnnFuiIOM4tHyXYbKMxGu3qWjb1XzrUCCmpWLxLtxKd/n9+0JDRhjg/TVvQbtp5tfyAzJQMm52JJil9Bk2ols8dNHos/X8bLN5dYgk5Bd36NqnuLTyBU+RzzavATdXKFeUJI5c5+24H0IdSCeqfIEMXb7ybaB0rLkM5NpLqXZCA/zbWqae65hVVjkix2AzpPFCWHMyg3R2Mox31Y7JnDJu7JI3IoDN7Lt06dZ+uwM0LHUz6eOlBT2oYZZ2fE7csMrIubBVjcplhyNCshzbOIRtobPsEsxYBfN2x9VwAA1EHFnXXQx0R56DoKdXeyXvHYbcJMInNZ7K4vZyb/quHCqREdojxqJLmbeejdEBi4u2x2sZuBlS2J3Ydlo15yUtRbSbtFsMWGLi8tWluKTC1YVN1MRSoGdDteuzKrcrv+TcpXVQqDkj6TMSVpi2j+Tb/e8Qi+ud2iXWtc7XNliwT7OLC5PYkb818ayN+Otxh69TklDSHvKXOsgBu7+Ob3lRt+2+Sr5kjZEIWkLtE5FVW4YILisuv4t9YBUEjpehFGb8o6xCFHtD01axeXEduCaNQA2d8Dc3ufHu021ahli4vpQZJgA==" width="64"/>
Raft is a consensus algorithm designed to manage replicated logs in distributed systems. Developed as an alternative to Paxos, Raft prioritizes understandability and simplicity, making it an attractive choice for building fault-tolerant distributed systems.

## Concepts
### Leader Election
Raft employs a leader election mechanism where nodes contend to become the leader.
Nodes transition through follower, candidate, and leader states based on timeouts and votes.
## Log Replication
The leader manages log replication by sending heartbeat messages and append entries to followers.
Followers replicate the leader's log, ensuring consistency across the system.
## Term and Commit Index
Raft introduces the concept of terms to track changes in leadership.
Each leader's term is associated with a commit index, indicating the highest log entry replicated to a majority of nodes.

## How Raft Works
- Follower State:
    Nodes start as followers, waiting for incoming leader messages.
    If a follower doesn't receive a message within a specified timeout, it transitions to the candidate state.

- Candidate State:
    In the candidate state, a node requests votes from other nodes in the system.
    If a candidate receives votes from a majority, it transitions to the leader state.

- Leader State:
    The leader manages log replication by sending heartbeat messages to maintain its leadership.
    It appends new log entries and sends append entries messages to followers for replication.

- Log Replication:    
    Followers replicate the leader's log, responding with acknowledgments to confirm successful replication.
    Log entries are committed when they have been replicated to a majority of nodes.

- Handling Failures:
    Raft ensures fault tolerance by allowing nodes to detect leader failures and initiate new elections.
    Only the most up-to-date log entries are committed, preventing inconsistencies.

## Use Cases
- Distributed Databases: Raft is widely used in distributed databases to ensure data consistency and reliability.
- Distributed Systems: Applications requiring consensus for critical decision-making leverage Raft for its simplicity and reliability.
