Terms: Logical Time in Raft
What you'll learn: How Raft uses "terms" as a logical clock to organize time and detect outdated information in a distributed system.
The Core Idea
In Raft, time is divided into sequential periods called terms. Each term is identified by a monotonically increasing number (term 1, term 2, term 3, etc.). Every term begins with an election, and if a candidate wins, that server becomes the leader for that term. Crucially, each term has at most one leader — if an election fails, the term ends with no leader, and a new term begins immediately.
Think of terms like presidential terms in government: each term has a number, at most one president serves during that term, and when comparing two pieces of information, you trust the one from the more recent term.
Why Terms Matter
Terms serve as a logical clock to detect stale information. When servers communicate, they include their current term number. If a server receives a message with a higher term, it realizes its information is outdated and immediately updates its term and steps down to follower state. If it receives a message with a lower term, it rejects the message as stale.
This mechanism ensures that old leaders who were partitioned away cannot disrupt the cluster when they reconnect — their term number will be lower, and their commands will be rejected.
Term Lifecycle
- Term begins → Election starts
- Election succeeds → One leader for the term
- Election fails → Term ends, new term begins
- Leader fails → Term effectively ends, new election starts a new term
Key Takeaway: Terms in Raft act as logical timestamps that organize time into numbered periods, ensure at most one leader per term, and allow servers to immediately detect and reject stale information from older terms.