Terms and the Logical Clock in Raft
You'll learn how Raft divides time into logical periods called "terms" to detect outdated information and maintain consistency across distributed nodes.
What Are Terms?
In Raft, terms act as a logical clock that divides time into consecutive, numbered periods. Think of terms like presidential administrations—each has a sequential number, and everyone knows which "era" they're in. Each term begins with an election: if a candidate wins, it becomes leader for that term. If the election fails (a split vote), the term ends with no leader, and a new term begins immediately.
Why Terms Matter
Terms solve a critical problem: how do nodes recognize stale information in a distributed system without synchronized physical clocks? Each Raft message includes the sender's current term number. When nodes communicate, they compare term numbers:
- Higher term wins: If a node receives a message with a higher term, it immediately updates its own term and reverts to follower state. Its information is outdated.
- Lower term rejected: Messages with lower terms are ignored—they're from the past.
- Equal terms: Normal processing continues.
The Logical Clock Property
Terms function as Lamport timestamps within Raft. They establish a happens-before relationship: events in term 5 happened before events in term 6. This ordering is crucial for safety—it prevents an old leader from disrupting a new leader's decisions, even if network partitions temporarily isolate it.
For example, if a node partitioned away in term 3 rejoins during term 7, it immediately recognizes it's behind and won't corrupt the current state.
Key Takeaway: Terms are Raft's logical clock that monotonically increase with each election, allowing nodes to detect stale information, ensure only one leader per term, and maintain consistency without relying on synchronized physical clocks.