A Raft leader answers a read from its local state. Can the answer be stale, and what does it take to serve a linearizable read?
Sketch your own answer first, then compare it with the one below.
A strong answer
Yes. A leader that has been cut off from the majority does not know it until it hears a higher term. In that window it will answer reads from a log that the real cluster has moved past. Serving reads locally trades correctness for latency.
To make a read linearizable the leader has to confirm it is still the leader at the time of the read. The direct way is to exchange a round of heartbeats with a majority before answering. The cheaper way is a lease: if the leader heard from a majority within the last election timeout, less a margin, then no one else can have won in the meantime, it can answer without a new round, with a margin for clock drift between nodes.
The leader must also have committed at least one entry in its current term before serving reads, so that it knows where the committed prefix ends. A new leader does this with a no-op. Followers can serve linearizable reads too. They ask the leader for its current commit index and wait until they have applied up to it.
Sources
- Diego Ongaro, John Ousterhout (2014). In Search of an Understandable Consensus Algorithm.
- Cary G. Gray, David R. Cheriton (1989). Leases, an efficient fault-tolerant mechanism for distributed file cache consistency.