how raft achieve strong consistency when they don't require fsync on every write
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Raft is a consensus algorithm designed for managing a replicated log. It ensures strong consistency across a distributed system, even without requiring fsync on every log entry write. This article delves into how Raft achieves this, the role of fsync, and potential impacts on performance and reliability.
Understanding Raft's Basics
The Raft consensus algorithm is built around a few key concepts: Leader Election, Log Replication, and Safety. It operates by maintaining a replicated log among multiple servers, ensuring that each log contains the same commands in the same order. The central purposes of Raft include:
- Leader Election: Raft elects a leader which manages the log replication across the followers and handles all client requests.
- Log Replication: Once the leader is elected, it begins accepting client commands and appends them to its log. These commands are then replicated to the follower nodes.
- Safety: Raft ensures that the committed entry is durable and will not be lost after acknowledging to the client, thereby providing strong consistency.
How Raft Works without Forced fsync
In many distributed systems, consistency often requires that any changes to the log be flushed to durable storage immediately using fsync. This approach ensures that even in the event of a crash, data has been persistently written to disk. Raft, however, allows more flexibility which can lead to performance improvements:
Log Replication and Durable State
- Efficient Logging: Instead of using
fsyncafter every write, Raft batches log entries. It replicates these entries across the cluster before they are committed and frees itself from the need tofsyncimmediately after every write. - Commit Process: Entries are considered committed when they have been safely replicated on a majority of the servers. It is only after this point that the client receives an acknowledgment. This means the system can recover even if some nodes fail.
Handling Failure without fsync
- Recovery Mechanism: If a leader crashes before flushing the entries to disk, the logs might be ahead on some servers and behind on others. However, since a majority of replicas is needed to commit entries, the new leader elected will have the most up-to-date committed entries, ensuring consistency.
- Log Matching Property: Raft's log matching property ensures that if two logs contain an entry with the same index and term, then the logs are identical in all entries up to that index. Thus, inconsistencies are prevented even without
fsync.
Example Scenario
Consider a situation where a leader appends two log entries but crashes before fsync. The followers might have one, both, or none of these entries. If a leader crash occurs:
- During new leader election, candidates request votes from other nodes.
- Each node checks its log; commits are recognized by comparing the term and log length.
- The new leader starts its term with logs that are consistent among a majority. Missing entries can be re-sent to lagging followers.
Performance Considerations
Using fsync sparingly improves performance due to reduced I/O waiting times. Here's a comparative view:
| Perspective | Forced fsync Usage | Skipped fsync in Raft |
| I/O Operations | High | Low |
| Speed of Log Replication | Slower | Faster |
| Data Safety | Very High | High |
| System Throughput | Lower | Higher |
Conclusion
Raft achieves strong consistency primarily through its careful design around log replication and the commit process. By allowing greater flexibility in the handling of fsync, Raft trades off some degree of immediate durability for significant gains in performance. The algorithm ensures that all committed changes are durable and consistent across the cluster even in the face of node failures, providing a reliable and efficient consensus mechanism.

