Failover
Automatic failure detection and recovery.
Leader Election
When partition leader fails:
- Detection - Controller detects broker failure
- Selection - Choose new leader from ISR
- Notification - Update metadata, notify clients
- Recovery - Clients connect to new leader
Before: Topic-0 Leader = Broker1 (ISR: 1,2,3)
↓ Broker1 fails
After: Topic-0 Leader = Broker2 (ISR: 2,3)
Detection
Heartbeat Monitoring
{
"Surgewave": {
"HeartbeatIntervalMs": 3000,
"HeartbeatTimeoutMs": 10000,
"MaxHeartbeatFailures": 3
}
}
Broker marked failed after:
HeartbeatTimeoutMswithout heartbeat, ORMaxHeartbeatFailuresconsecutive failures
Session Timeout
Consumer groups use session timeout:
var consumer = new SurgewaveConsumer<string, string>(options =>
{
options.SessionTimeoutMs = 10000;
options.HeartbeatIntervalMs = 3000;
});
Leader Election Types
Preferred Leader
Elect the preferred (first) replica:
surgewave partitions elect-leader --topic my-topic
surgewave partitions elect-leader --all
When the last in-sync replica fails
A partition whose leader fails is led by another in-sync replica. When the failed broker was the only in-sync
replica, no other replica is known to hold everything the partition acknowledged, and the controller elects none:
the partition has no leader, the failed broker stays in its ISR, and clients are answered LEADER_NOT_AVAILABLE
until that broker is back. It is then elected again, with nothing lost. This is what Kafka does.
Unclean Leader
An unclean election lets a live replica that is not in sync lead such a partition. What the in-sync replica had acknowledged and this replica had not fetched is lost, and the new leader becomes the whole ISR. It happens only where it is asked for:
- for a topic, with the topic config
unclean.leader.election.enable=true(defaultfalse): the controller then elects a live replica by itself when the last in-sync replica fails; - for single partitions, with an
ElectLeadersrequest of the unclean election type:
kafka-leader-election.sh --bootstrap-server localhost:9092 \
--election-type unclean --topic my-topic --partition 0
Only use when availability > consistency.
Graceful Shutdown
Controller coordinates shutdown:
- Broker sends ControlledShutdown request
- Controller moves leaders away
- Broker leaves cluster cleanly
# Broker initiates graceful shutdown
SIGTERM to broker process
Configuration:
{
"Surgewave": {
"ShutdownTimeoutSeconds": 30
}
}
Client Handling
Producer Failover
var producer = new SurgewaveProducer<string, string>(options =>
{
options.Retries = 3;
options.RetryBackoffMs = 100;
});
try
{
await producer.ProduceAsync("topic", "key", "value");
}
catch (NotLeaderException)
{
// Retry will redirect to new leader
}
Consumer Failover
Consumer group automatically rebalances:
Before: Consumer1 → Partitions 0,1
Consumer2 → Partitions 2,3
↓ Consumer1 fails
After: Consumer2 → Partitions 0,1,2,3
Controller Failover
When controller fails:
- KRaft triggers new election
- New controller elected by majority
- Metadata recovered from log
- Brokers register with new controller
Controller (Broker1) fails
↓
Election: Broker2 wins
↓
Broker2 becomes controller
Recovery Scenarios
Single Broker Failure
| Condition | Action |
|---|---|
| ISR > min.insync | Leader elected from ISR |
| ISR = min.insync | Writes blocked if leader fails |
| ISR < min.insync | Writes already blocked |
Minority Partition
Network partition isolating minority:
[Broker1] | [Broker2, Broker3]
↓ ↓
Isolated Majority continues
(read-only)
Full Cluster Restart
- Start brokers in any order
- Wait for quorum (majority)
- Controller elected
- Metadata recovered
- Leaders assigned
Monitoring
| Metric | Description |
|---|---|
surgewave_offline_partitions |
Partitions without leader |
surgewave_under_replicated_partitions |
Under-replicated count |
surgewave_active_controller |
Controller broker ID |
surgewave_leader_elections_total |
Total leader elections |
CLI Commands
# Check cluster health
surgewave cluster status
# Check for offline partitions
surgewave topics describe my-topic
# Force leader election
surgewave partitions elect-leader --topic my-topic --partition 0
Best Practices
- 3+ brokers - Tolerate single failure
- min.insync.replicas = 2 - Require majority for writes
- Monitor offline partitions - Alert immediately
- Test failover - Regular chaos testing
- Graceful shutdown - Use SIGTERM, not SIGKILL
Next Steps
- Security - Authentication and authorization
- Monitoring - Observability