Skip to main content

I broke my own cluster with a node that could never win

Updated 7 min readMarkdown

So I built Quorum — a distributed lock manager on top of a Raft consensus engine I wrote from scratch in Kotlin. Leader election, log replication, majority commit, the whole thing, over gRPC. No library doing the hard part for me.

I built it because I wanted to actually understand why systems like etcd — the consensus store sitting underneath Kubernetes — need this stuff, instead of just trusting one server's memory. Turns out the best way to understand distributed systems failure modes is to accidentally cause one yourself. So that's this post.

The mistake

I was testing dynamic cluster membership — adding a node to a live cluster without taking anything down. To test it, I spun up a fourth container, node-4, in Docker.

Except I did it in the wrong order. I started the container before I told the actual cluster about it through the proper channel (a real ADD_NODE command that goes through consensus like everything else). Node-4 came up already pointed at the other three nodes as its peers. The other three nodes had never heard of node-4.

Small mistake. Didn't think much of it at the time.

What node-4 did

Every node in the cluster runs an election timer — if it doesn't hear from a leader for 150-300ms, it assumes something's wrong and tries to become the leader itself. Node-4's timer ran exactly like everyone else's. Except node-4 was never going to get a heartbeat, because the real leader had no idea it existed.

So the timer fired. Here's what runs when that happens:

private suspend fun onElectionTimeout() {
    val termForThisElection: Long
    mutex.withLock {
        if (state.role == NodeRole.LEADER) return

        state.currentTerm += 1
        state.role = NodeRole.CANDIDATE
        state.votedFor = nodeId
        state.currentLeader = null
        termForThisElection = state.currentTerm
        log("election timeout -> became CANDIDATE for term $termForThisElection")

        electionTimer.reset()
        persistState()
    }

    runElection(termForThisElection)
}

state.currentTerm += 1. That line runs the second a node decides to try for leadership — it doesn't wait to see if the attempt goes anywhere. Node-4 bumped its own term on every single failed try, and it was going to fail every single try, because its log was completely empty and the real cluster already had a few hundred committed entries in its history. There's a check for exactly this:

val candidateLogIsAtLeastAsUpToDate =
    lastLogTerm > state.lastLogTerm() ||
        (lastLogTerm == state.lastLogTerm() && lastLogIndex >= state.lastLogIndex())

val grant = !alreadyVotedForSomeoneElse && candidateLogIsAtLeastAsUpToDate

Node-4 always failed this. Good — that part of the system was doing exactly what it was supposed to.

Except the vote-check wasn't the first thing that ran

Here's the part that got me. Before any node even bothers checking whether it should vote for a candidate, this runs first:

if (term > state.currentTerm) {
    stepDownTo(term)
}

If the incoming request has a higher term number than what you currently know, you step down — no questions asked, no waiting to see if the vote actually gets granted. And this rule has to exist, by the way. It's exactly what lets a real, legitimate new leader take over from a stale one. There's no way to write this rule as "only step down if the new leader deserves it," because by the time you'd know that, it's too late — the whole point of the rule is that a higher term always wins, immediately, no exceptions.

So every time node-4 tried and failed, it had already knocked the real leader off its perch before the failure was even evaluated. A brand new election would run among the three real nodes, someone legitimate would win — and then, a couple hundred milliseconds later, node-4 would try again, and knock that one down too.

Watching this happen live was genuinely funny and horrifying at the same time

I had my terminal open watching the logs and it was just leader, gone, leader, gone, leader, gone, on a loop, every few hundred milliseconds. The term number climbed fast — my README from that session says "climbing into the thousands," which is honestly the most precise thing I can tell you, because I never redirected the container output to a file and by the time I thought "oh I should save this," the moment had passed. I'm not going to pretend I have an exact number saved somewhere just to make this story sound more precise than it actually is.

What I can tell you for certain is the mechanism, because that part's sitting right there in the code: a retry loop firing every 150-300ms, for a few minutes, with nothing to stop it. Do the math and four figures isn't surprising at all.

The cluster wasn't crashing. That's what made it interesting instead of just annoying. Everything was working correctly — every individual decision was the right one — and the sum of all those correct decisions was a system that never got a stable enough moment to actually do anything useful.

Fixing it

The actual fix, in the moment, wasn't a code change. I killed node-4's container, added it properly this time through POST /cluster/add — which replicates a real ADD_NODE entry through the same consensus path as everything else — and then started node-4 back up. Once it was a real, known member, the leader started replicating its log to it, and by the time its timer fired again it actually had a shot at looking current.

The thing I didn't build, and I want to be upfront about that

There's a real extension to Raft for exactly this problem, called Pre-Vote. The idea is a candidate checks around informally first — "would you actually vote for me?" — before committing to bumping its own term. A failed pre-vote costs nothing, because no term ever changes. Node-4 would've failed that pre-vote check forever and just... sat there, harmlessly, instead of taking down a healthy leader every few hundred milliseconds.

I don't have it built. I went and checked the actual code before writing this instead of trusting my memory, because I genuinely wasn't 100% sure — and there's nothing there. No pre-vote RPC, nothing. It's in my README as a known gap, not something I'm quietly pretending isn't missing.

What actually stuck with me from this

The rule that caused all this chaos — always defer to a higher term, immediately — is correct. It's not a flaw. It's the exact same rule that makes the whole system trustworthy in the first place. The failure mode only exists because I created a situation the rule was never designed to handle: a node that could disrupt but could never actually win. That's a genuinely different category of problem than "my code has a bug," and I don't think I would've understood the difference nearly as well if I'd just read about it instead of watching my own terminal do this to itself for a few minutes.


Quorum's on GitHub if you want to poke at it — leader election, replication, a lock API that's actually conflict-safe (found and fixed a real bug there too, a different story), disk persistence, and the membership stuff that led to all of this. README's got the rest of the details if this kind of thing is your thing.

About the author

  • Backend Software Engineer who builds scalable, distributed systems. I write about the real bugs, tradeoffs, and lessons I run into while building things from scratch — mostly backend architecture, concurrency, and systems design.

No comments yet. Be the first to comment.