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
Unclean Leader
Elect non-ISR replica (may lose data):
surgewave partitions elect-leader --topic my-topic --unclean
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