Leader-follower replication is a pattern where one node (the leader) handles all writes and replicates data to multiple follower nodes that serve reads and provide fault tolerance. If the leader fails, a follower is promoted to become the new leader. This pattern achieves high availability, fault tolerance, and read scalability.
IN-SYNC REPLICAS (ISR):
Config: replication.factor=3, min.insync.replicas=2
Replicas:
- Leader (always in ISR)
- Follower 1 (in ISR if < 10s lag)
- Follower 2 (in ISR if < 10s lag)
- Follower 3 (NOT in ISR, lagging > 10s)
Write Flow:
1. Client →Write to Leader
2. Leader→ Write + send to all followers
3. Wait formin.insync.replicas=2 ACKs (Leader + 1 follower)
4. Acknowledge to ClientGuarantees:
✓At least 2 replicas have data before ACK
✓Fast writes (don't wait for slow follower 3)
✕ If ISR count < min.insync.replicas, writes fail (availability hit)
Best of both worlds!
Leader Election and Failover
When Does Failover Happen?
Failure Detection
Failure Detection
FAILURE DETECTION:
Heartbeat Mechanism:
Followers →Send heartbeat to Leader every 1s
Leader →Send heartbeat to Followers every 1s
Failure Scenarios:
1. Leader misses heartbeats→ Followers detectleader failure
2. Follower misses heartbeats→ Leader removes from ISR
3. Network partition →Split-brain prevention needed
Leader Election Process:
Leader Election Process
Leader Election Process
LEADER ELECTION (Simplified):
1. FAILURE DETECTIONLeader fails (no heartbeat for 10s)
2. ELECTION TRIGGER
Followers detect failureStart election process
3. CANDIDATE SELECTION
Criteria for new leader:
- ✓ In ISR (up-to-date replica)
- ✓ Highest offset (most data)
- ✓ Lowest broker ID (tie-breaker)
4. NEW LEADER ANNOUNCED
Controller broadcasts new leader
Followers connect to new leader
5. RESUME OPERATIONS
New leader accepts writes
Old leader (if recovers) becomes follower
Total Failover Time: ~5-30 seconds
Kafka’s Controller-Based Election:
Kafka Controller Architecture
Kafka Controller Architecture
KAFKA ARCHITECTURE:
┌─────────────────────────────────────────┐│ZooKeeper / KRaft│←Metadata store│ - Broker liveness ││ - Controller election ││ - Partition assignments │└────────────────┬────────────────────────┘│┌───────┴────────┐│Controller│←ONE broker elected as controller│ (Broker 2) │Manages all leader elections└───────┬────────┘│┌────────────┼────────────┐│││┌───┴───┐┌───┴───┐┌───┴───┐│Broker1││Broker2││Broker3│└───────┘└───────┘└───────┘Controller Responsibilities:
1. Monitor broker liveness
2. Elect partition leaders when failures occur
3. Update metadata in ZooKeeper/KRaft
4. Notify all brokers of leadership changes
Replication Lag and ISR Management
What is Replication Lag?
Replication Lag
Replication Lag
REPLICATION LAG:
Leader: [msg0][msg1][msg2][msg3][msg4][msg5] ←Offset 5
Follower 1: [msg0][msg1][msg2][msg3][msg4][msg5] ←Lag: 0 ✓IN ISRFollower 2: [msg0][msg1][msg2][msg3] ←Lag: 2 ⚠️ IN ISRFollower 3: [msg0][msg1] ←Lag: 4 ✕ OUT OF ISRISR Criteria:
- replica.lag.time.max.ms=10000 (10 seconds)
- If follower doesn't fetch within 10s →Removed from ISR
All reads go to Leader:
Clients→Leader (reads)
↓
[DATA v5] ← Always latest version
Pros:
✓Strong consistency (always up-to-date)
✓Simple (no staleness issues)
Cons:
✕ Leader bottleneck (all read traffic)
✕ Doesn't scale with more replicas
Read from Followers (Eventual Consistency):
Read from Followers
Read from Followers
Reads distributed across replicas:
Client A →Follower 1 [DATA v4] ←Slightly staleClient B →Follower 2 [DATA v5] ←Up-to-dateClient C →Leader [DATA v5] ← Always latest
Pros:
✓Read scalability (horizontal scaling)
✓Lower latency (geographically closer follower)
Cons:
✕ Eventual consistency (may read stale data)
✕ Monotonic read issues (read v5, then v4)
Hybrid: Read-Your-Writes Consistency:
Read-Your-Writes Consistency
Read-Your-Writes Consistency
Strategy: Track last write offset, read only from replicas >= that offset
1. Client writes to Leader → Receives offset 100
2. Client reads→ Request includes "minOffset=100"
3. Router →Send to follower with offset >= 100
4. If no follower caught up → Read from Leader
Result: Client always reads its own writes✓
Tradeoffs
Advantages:
✓ Fault tolerance (survive N-1 failures with N replicas)
✓ High availability (automatic failover)
✓ Read scalability (distribute reads to followers)
✓ Data durability (multiple copies)
Disadvantages:
✕ Write latency (replication overhead)
✕ Consistency complexity (sync vs async tradeoffs)
✕ Failover time (10-30s downtime during leader election)
Scenario: E-commerce platform requiring 99.99% uptimeSolution: 3 replicas, auto-failover on leader crash
Result: Survive single node failure with under 30s downtime
Read-Heavy Workloads
Read-Heavy Workloads
Read-Heavy Workloads
Scenario: News site with 10:1 read/write ratio
Solution: 1 leader + 5 followers, reads from followers
Result: 6x read throughput
Geo-Distributed Reads
Geo-Distributed Reads
Geo-Distributed Reads
Scenario: Global application with users in US, EU, Asia
Solution: Leader in US, followers in EU and Asia
Result: Low-latency reads for all regions
✕ When NOT to Use
Multi-Region Writes
Multi-Region Writes
Multi-Region Writes
Problem: Users in EU and Asia need to write locally
Issue: All writes go to single leader (high latency)
Alternative: Multi-leader replication or sharding
Need for Strong Consistency Reads
Need for Strong Consistency Reads
Need for Strong Consistency Reads
Problem: Bank balance must always be current
Issue: Follower reads may be staleAlternative: Read from leader or use quorum reads
Extremely High Write Throughput
Extremely High Write Throughput
Extremely High Write Throughput
Problem: 100K writes/sec overwhelming single leader
Issue: Leader bottleneckAlternative: Partition data across multiple leaders (sharding)
Interview Application
Common Interview Question 1
Q: “Design a highly available message queue. How would you handle broker failures?”
Strong Answer:
“I’d use leader-follower replication with in-sync replicas (Kafka’s model):
Architecture:
Each partition has replication.factor=3 (1 leader + 2 followers)
min.insync.replicas=2 (leader + at least 1 follower must ACK)
Controller broker manages leader elections
Normal Operation:
Producers write to partition leader
Leader replicates to followers in parallel
ACK to producer after min 2 replicas confirm
Consumers read from leader (or followers for lower priority)
Failure Handling:
Follower failure: Removed from ISR, writes continue with remaining ISR
Leader failure: Controller elects new leader from ISR within 10-30s
Network partition: Rely on ZooKeeper quorum to prevent split-brain
Trade-offs:
Synchronous to ISR = no data loss but slightly higher latency
Async to non-ISR replicas = fast writes but potential data loss on leader crash
This is exactly how Kafka achieves 99.99%+ availability at LinkedIn scale.”
Why this is good:
Specific configuration values
Handles multiple failure scenarios
Explains trade-offs clearly
References real-world implementation
Common Interview Question 2
Q: “What’s the difference between synchronous and asynchronous replication? When would you use each?”
Strong Answer:
“Synchronous Replication:
Leader waits for follower ACKs before responding to client
Guarantees: No data loss (all replicas have data)
Trade-off: Higher latency, lower availability (blocked if follower down)
Use case: Financial transactions, critical metadata
Asynchronous Replication:
Leader responds immediately, replicates in background
Guarantees: Low latency, high availability
Trade-off: Potential data loss if leader crashes before replication
Use case: Analytics logs, user activity streams
In Production:
Most systems use a hybrid like Kafka’s ISR:
Synchronous to a quorum (e.g., 2 out of 3 replicas)
Asynchronous to remaining replicas
Dynamically remove slow replicas from ISR to maintain availability
Result: Balance between durability and performance
For example, at Uber, we’d use sync replication for payment events (can’t lose money) but async for GPS location updates (can tolerate occasional loss).”
Why this is good:
Clear comparison of both approaches
Explains when to use each
Mentions hybrid approach (real-world)
Concrete examples for each use case
Red Flags to Avoid
✕ Not understanding the difference between sync and async replication
✕ Ignoring split-brain scenarios and how to prevent them
✕ Thinking failover is instant (it takes 10-30s typically)
✕ Not considering replication lag impact on read consistency
Quick Self-Check
Before moving on, can you:
Explain leader-follower replication in 60 seconds?