Distributed Consensus in System Designing
expert · System Designing
In a distributed system, network partitions will happen, servers will crash, and messages will be dropped. Despite these failures, machines must often agree on a single source of truth—such as who the current leader is, or whether a transaction was committed. Distributed Consensus is the protocol that allows a cluster of independent machines to agree on a state or a sequence of values safely, ensuring data consistency even when some nodes are offline. 1. The Core Rules of Consensus To be considered mathematically correct, a consensus algorithm must satisfy three foundational properties: Agreement: All non-faulty nodes must decide on the exact same value. Validity (Integrity): The value that is agreed upon must have been proposed by one of the nodes in the system (the system cannot invent data out of thin air). Termination (Liveness): Every non-faulty node must eventually reach a decision; the system cannot get stuck in an infinite loop forever. 2. Paxos vs. Raft While there are several consensus frameworks, Paxos and Raft are the two dominant protocols used to manage state machine replication. A. Paxos — The Classic Proposed by Leslie Lamport, Paxos is the foundational mathematical standard for distributed consensus. It breaks nodes into roles: Proposers , Acceptors , and Learners , navigating a multi-phase voting process to achieve agreement. The Problem: Paxos is notoriously abstract, difficult to conceptualize, and highly complex to implement in actual production software. Building a bug-free implementation of Multi-Paxos is considered one of the hardest engineering tasks in systems architecture. B. Raft — Deconstructed for Understandability Introduced by Diego Ongaro and John Ousterhout, Raft was designed specifically as an alternative to Paxos with a primary goal: Understandability . It achieves consensus by decomposing the problem into two distinct, highly managed phases: Leader Election and Log Replication . 3. How Raft Works Under the Hood In a Raft cluster (typically consisting of 3 or 5 nodes), a node can exist in one of three states: Follower , Candidate , or Leader . Step 1: Leader Election All nodes start as Followers . They expect to receive a continuous "heartbeat" from a Leader. If a follower stops hearing the heartbeat because the leader died, its internal Election Timeout expires. The follower transitions to a Candidate , increments the cluster's evolutionary timeline (called a Term ), votes for itself, and fires off "RequestVote" messages to all other nodes. If a candidate receives votes from a majority of nodes ( $N/2 + 1$ ), it is officially elected as the new Leader and begins broadcasting heartbeats to assert dominance. The Secret Weapon: Randomized Timeouts To prevent a split-vote deadlock (where two followers become candidates at the exact same millisecond and vote for themselves), Raft randomizes election timeouts (e.g., each node picks a random timeout between 150ms and 300ms). One node will naturally time out first, initiate the vote, and claim the leadership seat cleanly. Step 2: Log Replication Once a leader is established, all client requests must go directly through it. A client sends a command to the Leader (e.g., SET x = 10 ). The leader appends this command to its local, uncommitted log file. The leader broadcasts an AppendEntries message containing the log entry to all Follower nodes. Once a majority of followers successfully write the entry to their local disks and acknowledge it back to the leader, the entry is considered Committed . The leader officially applies the change to its local state machine, returns a success code to the client, and notifies the followers to commit the entry during the next heartbeat. 4. Real-World Implementations Distributed consensus is too complex to build from scratch for standard applications. Instead, systems integrate specialized, battle-tested consensus engines: etcd (Uses Raft): A strictly consistent, distributed key-value store that acts as the backbone brain of Kubernetes , tracking cluster state, secrets, and configuration details. Apache ZooKeeper (Uses Zab — a Paxos variant): A centralized service for maintaining configuration information, naming, and providing distributed synchronization across massive big-data ecosystems (like Hadoop and Kafka clusters). CockroachDB (Uses Raft + Multi-Raft): A next-generation, globally distributed SQL database that breaks tables into small shards and runs independent Raft consensus loops across those shards to ensure ACID compliance across global data centers. 5. The CAP Connection By design, distributed consensus engines choose Consistency (C) over Availability (A) under the CAP Theorem. If a 5-node cluster suffers a network partition that cuts it perfectly into a group of 2 nodes and a group of 3 nodes: The group of 2 cannot form a majority (
< 3$ ). It will refuse to elect a leader and will block all incoming write requests. The group of 3 can form a majority (
\geq 3$ ). It will operate normally. If the network breaks further and a majority can no longer be reached anywhere, the entire consensus layer locks down completely, preferring to reject writes rather than risk corrupting or fracturing your data truth.Careeroza — One-stop Zone for Aspirants
Study material, Careeroza mentorship, tech jobs, and career guidance on careeroza.com.
Public study materials
- Node.js — Server-side JavaScript (nodejs)
- Getting started · basic
- JavaScript on the server · basic
- CommonJS modules · basic
- ES modules (ESM) · basic
- npm & package management · basic
- Asynchronous JavaScript in Node · basic
- The event loop · basic
- Essential core utilities · basic
- process & configuration · basic
- File system basics · basic
- HTTP & HTTPS servers · medium
- Streams · medium
- Events & EventEmitter · medium
- Advanced filesystem · medium
- crypto · medium
- Compression & encoding · medium
- Child processes · medium
- net, dgram & DNS · medium
- readline, timers & scheduling · medium
- Testing & diagnostics (intro) · medium
- Worker threads · advance
- cluster & multi-process scaling · advance
- Performance & tuning · advance
- Debugging & observability · advance
- Security hardening · advance
- Native addons & N-API · advance
- Architecture patterns · advance
- Graceful shutdown · advance
- 100 Questions · interview questions
- Django (Django)
- What is Django · basic
- Installing Django · basic
- Features of Django · basic
- MVT Architecture · basic
- Django vs Flask · basic
- Creating Project & Creating App · basic
- Django Project Structure · basic
- URL Routing · basic
- Views · basic
- Templates · basic
- Static & Media Files · medium
- Models · medium
- ORM (Object Relational Mapping) · medium
- Model Relationships · medium
- Migrations · medium
- Django Admin · medium
- Forms · medium
- Authentication · medium
- Authorization · medium
- Middleware · medium
- Signals · medium
- Class Based Views Deep Dive · advance
- Generic Views · advance
- File Handling · advance
- Django REST Framework (DRF) · advance
- Advanced ORM · advance
- Caching · advance
- Asynchronous Django · advance
- Background Tasks · advance
- Interview Questions · interview-questions
- Python (Python)
- Python Fundamentals · basic
- Control Flow · basic
- Strings · basic
- Collections / Data Structures · basic
- Functions · basic
- Modules and Packages · basic
- File Handling · basic
- Exception Handling · basic
- Object-Oriented Programming (OOP · medium
- Advanced Python Concepts · advance
- Functional Programming · advance
- Multithreading & Multiprocessing · advance
- Async Programming · advance
- JavaScript (JavaScript)
- JS Introduction · basic
- Variables & Data Types · basic
- Operators · basic
- Control Flow · basic
- Functions · basic
- Scope & Execution · basic
- Closures · basic
- Objects · basic
- Arrays · basic
- Strings · basic
- DOM Manipulation · basic
- Browser APIs · medium
- Asynchronous JavaScript · medium
- Fetch & APIs · medium
- ES6+ Features · medium
- OOP in JavaScript · medium
- Prototype & Inheritance · advance
- Advanced Functions · advance
- Memory Management · advance
- Error Handling · advance
- Modules · advance
- Advanced Async Concepts · advance
- Functional Programming · advance
- JavaScript Internals · advance
- Performance Optimization · advance
- System Designing (System Designing)
- Day-1 : What is system Designing ? · basic
- Day-2 : Vertical vs. Horizontal Scaling · basic
- Day-3:How to do vertical scaling ? · basic
- Day4:How to do horizaontal scaling ? · basic
- Day:5TCP vs UDP · basic
- Day6:IP & DNS · basic
- Day7:Client-Server Model · basic
- Day8:HTTP & HTTPS · basic
- Databases (SQL vs NoSQL) · medium
- Caching · medium
- Day9:Latency & Throughput · basic
- Load Balancing · medium
- Indexes & Query Optimization · medium
- CDN · medium
- Proxies · medium
- Message Queues · medium
- Horizontal vs Vertical Scaling · medium
- Database Replication · advance
- Database Sharding · advance
- Consistent Hashing · advance
- CAP Theorem · advance
- Rate Limiting · advance
- Service Discovery · advance
- Event-Driven Architecture · advance
- API Gateway · advance
- Distributed Consensus · expert
- Microservices · expert
- Observability · expert
- Idempotency · expert
- PACELC Theorem · expert
- Two-Phase Commit · expert
- Back-of-Envelope Estimation · expert
- Designing for Failure · expert
- Angular (Angular)
- Angular Fundamentals · basic
- Project Structure · basic
- Components & Templates · basic
- Data Binding · basic
- Directives · basic
- Pipes · basic
- Component Communication · basic
- Lifecycle Hooks · basic
- Routing Basics · basic
- Routing · basic
- API Calls · basic
- Forms · medium
- Routing · medium
- Services & Dependency Injection · medium
- RxJS & Observables · medium
- Authentication & Security · medium
- Component Interaction · medium
- State Management Basics · medium
- Error Handiling · medium
- Perfomance Basic · medium
- Real World Features · medium
- Advanced Angular Architecture · advance
- Change Detection · advance
- Advanced RxJS · advance
- State Management · advance
- Dynamic Rendering · advance
- Perfomance Optimization · advance
- Modern Angular · advance
- Express.js — Web APIs & middleware (expressjs)
- Application setup · basic
- Routing deep dive · basic
- 1. MVC / Layered Architecture · medium
- Validation · medium
- File uploads · medium
- Sessions & auth (stateful) · medium
- Passport & strategies · medium
- Templating & SSR · medium
- WebSockets & SSE · medium
- Security middleware · advance
- Reverse proxies & trust · advance
- Performance · advance
- API design & versioning · advance
- Testing with Supertest · advance
- GraphQL & tRPC (overview) · advance
- Deployment checklist · advance
- Middlewares · basic
- Request & Response · basic
- SQL (SQL)
- SQL Fundamentals · basic
- Database Operations · basic
- Table Operations · basic
- CRUD Operations · basic
- Filtering & Operators · basic
- SQL Functions · basic
- GROUPING Data · basic
- Joins · medium
- Constraints · basic
- Subqueries · medium
- Set Operators · medium
- Views · medium
- Indexes · medium
- Normalization · advance
- Transactions · advance
- Stored Procedures & Functions · advance
- Triggers · advance
- Advanced SQL · advance
- Query Optimization · advance
- Database Design · advance
- SQL Security · advance
- Backup & Recovery · advance
- Questions · interview questions
- STAR (Situation, Task, Action, and Result) (Situation Based Questions)
- System Architecture (System Architecture)
- Fundamentals of System Architecture · basic
- Distributed System Basics · basic
- System Reliability Concepts · basic
- Scaling Concepts · basic
- Networking Basics · basic
- Web Communication · basic
- API Communication · basic
- Proxy & Delivery Systems · basic
- Web Architecture Basics · basic
- Rendering Architectures · basic
- Frontend Advanced Concepts · basic
- Message Queue Basics · medium
- What is Load Balancer · medium
- Load Balancing Algorithms · medium
- API Design Basics · medium
- API Protection · medium
- Authentication Basics · medium
- Security Tokens · medium
- Security Threats · medium
- Encryption & Security · medium
- SQL Database Basics · medium
- SQL Scaling Concepts · medium
- NoSQL Databases · medium
- Database Optimization · medium
- Replication Strategies · medium
- Caching Basics · medium
- Cache Storage Systems · medium
- Cache Strategies · medium
- Event-Driven Systems · advance
- Queue Reliability · advance
- Microservices Basics · advance
- Microservice Communication · advance
- Distributed Transactions · advance
- DevOps Basics · advance
- Automation Tools · advance
- Deployment Strategies · advance
- Monitoring Basics · advance
- Monitoring Tools · advance
- Distributed System Concepts · advance
- Distributed Algorithms · advance
- MongoDB — Documents & data modeling (MongoDB)
- Introduction · basic
- Shell, Compass & tools · basic
- Databases & collections · basic
- CRUD operations · basic
- Indexes deep dive · medium
- Explain plans & performance · medium
- Aggregation framework · medium
- Schema design patterns · medium
- Mongoose basics · medium
- Mongoose advanced · medium
- Drivers & connection · medium
- Operators for updates & arrays · medium
- Replication & read preferences · advance
- Write concern & read concern · advance
- Multi-document transactions · advance
- Change streams · advance
- Sharding (overview) · advance
- Atlas Search & full-text · advance
- GridFS & large files · advance
- Backup, restore & ops · advance
- AWS Crash Course (AWS)
- What is Cloud ? · basic
- What is AWS ? · basic
- If not cloud ? · basic
- Cloud Computing · basic
- AWS Pricing · basic
- AWS Shared Responsibility Model · basic
- AWS Management Console · basic
- AWS SDKs · basic
- AWS IAM · medium
- Users, Groups, Roles · medium
- Policies · medium
- AWS Organizations · medium
- AWS Cognito · medium
- AWS Directory Service · medium
- AWS KMS (Key Management Service) · medium
- AWS Secrets Manager · medium
- AWS Shield · medium
- AWS WAF · medium
- AWS Inspector · medium
- AWS GuardDuty · medium
- EC2 · advance
- Launching EC2 Instances · advance
- EBS Volumes · advance
- Security Groups · advance
- Key Pairs · advance
- Elastic IP · advance
- User Data Scripts · advance
- Auto Scaling · advance
- Load Balancers · advance
- ALB · advance
- NLB · advance
- Serverless Compute ,AWS Lambda, Lambda Layers · advance
- Event-Driven Architecture · advance
- ECS · advance
- EKS · advance