惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

N
Netflix TechBlog - Medium
罗磊的独立博客
云风的 BLOG
云风的 BLOG
Last Week in AI
Last Week in AI
Y
Y Combinator Blog
小众软件
小众软件
Blog — PlanetScale
Blog — PlanetScale
T
The Blog of Author Tim Ferriss
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
月光博客
月光博客
博客园 - Franky
F
Fortinet All Blogs
D
Docker
博客园 - 司徒正美
腾讯CDC
Recent Announcements
Recent Announcements
The Cloudflare Blog
B
Blog RSS Feed
GbyAI
GbyAI
T
Tailwind CSS Blog
雷峰网
雷峰网
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
博客园 - 三生石上(FineUI控件)
阮一峰的网络日志
阮一峰的网络日志

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
The Raft Consensus Algorithm: Leader Election and Log Rep...
Dylan Dumont · 2026-04-23 · via DEV Community

Dylan Dumont

Raft solves the hardest problem in distributed systems: keeping replicas synchronized while nodes fail.

What We're Building

We are dissecting the Raft consensus protocol to understand how a cluster maintains a single source of truth. Unlike Paxos, Raft is designed to be human-readable and easier to implement correctly. Our scope is not building a complete key-value store, but modeling the core state machine of a Raft node. We will focus on the three core roles, the heartbeat mechanism, and the safety properties that prevent split-brain scenarios. We will use Go for examples because its interface definition and structuring closely mimic the RPC patterns found in production Raft libraries like etcd.

Step 1 — Defining Node Roles

Raft nodes operate in a finite state machine. A node transitions between Follower, Candidate, and Leader. The leader manages log replication, while followers maintain state consistency. This separation ensures that only one node writes to the log at any term, preventing conflicts.

type NodeState uint8

const (
    StateFollower NodeState = iota
    StateCandidate
    StateLeader
)

type RaftNode struct {
    State    NodeState
    Term     int
    VoteFor  NodeID
    LastLogIndex int
}

Enter fullscreen mode Exit fullscreen mode

Go’s type system handles the state transitions cleanly, ensuring type safety without external libraries.

Step 2 — Conducting Leader Elections

When a follower stops receiving heartbeats, it starts an election. It increments its term, sets itself to Candidate, and broadcasts a RequestVote RPC to all other nodes. Nodes only vote for a leader if they have newer log entries than the candidate. This prevents old terms from becoming leaders.

func (n *RaftNode) RequestVote(term int) (bool, error) {
    if term < n.Term { return false, nil }
    if n.State == StateLeader { return true, nil }
    // Simplified logic: vote if log matches
    return true, nil
}

Enter fullscreen mode Exit fullscreen mode

This logic enforces the requirement that a leader must have the most up-to-date logs, ensuring safety.

Step 3 — Log Replication via RPC

The leader persists commands by appending them to its log. It then replicates this entry to a majority of followers using AppendEntries. Followers append the entry, acknowledge the success, and the leader moves it to its commit index. If a majority acknowledges the entry, it is considered committed and safe.

func (l *RaftNode) AppendEntries(leaderTerm int, index int) (bool, error) {
    if leaderTerm != l.Term { return false, nil }
    l.Log[index] = data
    return true, nil
}

Enter fullscreen mode Exit fullscreen mode

This function demonstrates the AppendEntries RPC where a leader proposes a change, and followers store it locally.

Step 4 — Commit Safety and Stability

A log entry is considered committed once a majority of nodes store it. The leader sends commit indices down to followers during heartbeats. Crucially, a leader will never overwrite a committed log entry, even if its local log becomes stale during an election. This guarantees that the state machine remains consistent across the cluster even after node failures and recoveries.

func (l *RaftNode) ApplyCommit(index int) {
    if index > l.CommittedIndex {
        l.CommittedIndex = index
        l.Apply(index)
    }
}

Enter fullscreen mode Exit fullscreen mode

This ensures that only committed entries are executed by the state machine, preserving durability guarantees.

Key Takeaways

  • State Machine: Raft nodes transition between Follower, Candidate, and Leader based on received RPCs.
  • Election Safety: Leaders must have the most recent logs, preventing old terms from becoming leaders and maintaining order.
  • Log Consistency: Followers only accept entries from a current leader, ensuring global consistency.
  • Durability: Entries are durable once acknowledged by a majority before being applied to the state machine.
  • Safety First: Raft prioritizes data correctness over availability during partitions to prevent data loss.

What's Next?

Consider optimizing the heartbeat interval and election timeout to balance consistency and latency. Next, explore how to handle split-brain scenarios where multiple nodes elect themselves leader simultaneously. You should also investigate how Raft handles snapshot compression to manage large log sizes efficiently. Finally, compare Raft with other consensus algorithms to understand trade-offs in your specific architecture.

Further Reading

Part of the Architecture Patterns series.