Skip to main content

Replication and Fault Tolerance

One of Kafka's most important features is its ability to continue operating even when machines fail. In a distributed system, failures are not exceptional events, they are expected events.

  • Machines crash.
  • Disks fail.
  • Networks become unavailable.
  • Processes stop unexpectedly.

If Kafka stored every partition on only one broker, the failure of that broker would result in data loss and service unavailability. To solve this problem, Kafka uses:

  • Replication
  • Leader-Follower Architecture
  • In-Sync Replicas (ISR)
  • Automatic Failover
  • Durable Storage

These mechanisms allow Kafka to provide high availability and strong durability guarantees.

Why Replication Exists​

Consider a Kafka cluster with three brokers.

Suppose a partition exists only on Broker 1.

Everything works normally.

Now Broker 1 crashes.

The partition becomes unavailable. Consumers cannot read. Producers cannot write. If the disk is permanently lost, all data is lost.

This is known as a Single Point of Failure (SPOF). Replication eliminates this problem.

Kafka Replication​

Replication means maintaining multiple copies of the same partition across different brokers.

Instead of storing a partition on a single machine:

Kafka stores multiple copies.

Now the failure of one broker does not result in partition loss.

Replication Factor​

The number of copies maintained by Kafka is called the Replication Factor (RF).

Example:

Replication Factor = 1

One copy exists.

No fault tolerance.

Example:

Replication Factor = 2

Two copies exist.

One broker can fail safely.

Example:

Replication Factor = 3

Three copies exist.

Two failures can potentially be tolerated depending on ISR state.

Replication Factor vs Number of Brokers​

Replication factor cannot exceed available brokers.

Example:

Brokers = 3
Replication Factor = 5

Impossible.

Kafka cannot place five replicas on only three brokers.

Rule:

Replication Factor <= Number of Brokers

Leaders and Followers​

Replication alone is not enough. Kafka also needs a mechanism for coordinating reads and writes. This is where Leaders and Followers come in.

Leader Replica​

Every partition has exactly one Leader.

Example:

The leader handles:

  • Producer writes
  • Consumer reads
  • Replica synchronization

All client traffic flows through the leader.

Follower Replicas​

The remaining replicas are Followers.

Followers do not serve normal client requests.

Their primary responsibility is to copy data from the leader.

Leader-Follower Architecture​

Consider:

Partition 0
Replication Factor = 3

Producer writes:

Consumer reads:

Replication:

This architecture simplifies consistency management.

Why Not Allow Writes to Followers?​

Imagine allowing writes to all replicas.

Potential problems:

  • Conflicting writes
  • Ordering issues
  • Complex synchronization

Kafka avoids these problems by making the leader the single source of truth.

Replication Workflow​

Consider a producer sending a record.

{
"orderId": 1001
}

Step 1: Producer Sends Record​

Step 2: Leader Appends Record​

New record appended.

Step 3: Followers Fetch Data​

Followers continuously pull updates from leader.

Step 4: Followers Append Record​

Followers write the same record.

Step 5: Acknowledgement​

Depending on producer configuration:

acks=0
acks=1
acks=all

Kafka may acknowledge immediately or wait for replica synchronization.

In-Sync Replicas (ISR)​

Not every replica is always healthy.

Some replicas may:

  • Fall behind
  • Experience network issues
  • Become overloaded

Kafka therefore maintains a special set called the In-Sync Replica Set (ISR).

ISR contains replicas that are sufficiently caught up with the leader.

Example ISR​

All replicas synchronized.

ISR:

ISR = {Leader, Follower1, Follower2}

Lagging Replica​

Suppose Follower 2 falls behind.

Kafka removes it from ISR.

ISR = {Leader, Follower1}

Follower 2 still exists.

It simply is not considered fully synchronized.

Why ISR Matters​

Kafka uses ISR for:

  • Durability decisions
  • Acknowledgements
  • Leader elections
  • Failure recovery

ISR is one of the most important concepts in Kafka.

Producer Acknowledgements and ISR​

Recall:

acks=all

This means:

Do not acknowledge until all ISR replicas have stored the record.

Example:

Only after ISR confirmation does producer receive success.

This provides maximum durability.

Leader Failure​

Now consider the leader crashing.

Initial state:

Producer writes normally.

Suddenly:

Without failover:

  • Partition unavailable
  • Consumers blocked
  • Producers blocked

Kafka solves this using leader election.

Leader Election​

When a leader fails: Kafka selects a new leader from ISR.

Example:

Before failure:

ISR = {Leader, Follower1, Follower2}

Leader crashes.

ISR = {Follower1, Follower2}

Kafka elects:

Clients continue operating.

The partition remains available.

Why Only ISR Members Become Leaders?​

Suppose a lagging replica becomes leader.

Some records may never have reached the lagging replica. Promoting it could cause data loss. Therefore Kafka elects leaders from ISR.

Fault Tolerance in Kafka​

Fault tolerance is the ability of a system to continue operating despite failures.

Kafka is designed with failure as a normal operating condition.

Scenario 1: Broker Failure​

Initial cluster:

Broker 1 crashes.

Replicas on Brokers 2 and 3 continue serving traffic. System remains operational.

Scenario 2: Disk Failure​

If one broker loses storage:

Replica copies still exist.

Partition survives.

Scenario 3: Network Partition​

A broker may become temporarily unreachable.

Kafka removes unhealthy replicas from ISR.

Cluster continues operating.

Scenario 4: Consumer Failure​

Consumer crashes.

Data remains safely stored in Kafka.

Another consumer can resume processing using offsets.

Data Durability​

Durability means:

Once Kafka acknowledges a record, the record should survive failures.

Durability is one of Kafka's most important guarantees.

How Kafka Achieves Durability​

Kafka combines:

  • Disk Storage
  • Replication
  • ISR
  • Acknowledgements
  • Leader Election

to provide durability.

Persistent Storage​

Kafka stores records on disk.

Unlike in-memory systems, data survives process restarts.

Append-Only Log​

Records are appended sequentially.

Sequential writes are:

  • Fast
  • Reliable
  • Durable

Durability Levels​

Lowest Durability​

Replication Factor = 1
acks = 0

Fastest. Highest risk of loss.

Medium Durability​

Replication Factor = 3
acks = 1

Good balance. Most common for non-critical workloads.

Highest Durability​

Replication Factor = 3
acks = all
min.insync.replicas = 2

Strong durability guarantees. Widely used in production systems.

Understanding min.insync.replicas​

This configuration defines the minimum number of ISR replicas required for writes.

Example:

Replication Factor = 3
min.insync.replicas = 2

Valid:

ISR = {Leader, Follower1}

Writes continue.

Invalid:

ISR = {Leader}

Writes rejected.

Kafka prioritizes durability over availability.

High Availability vs Durability​

Distributed systems always involve trade-offs.

Favor Availability​

acks=1

System continues serving writes more easily.

Potentially less durable.

Favor Durability​

acks=all
min.insync.replicas=2

Stronger guarantees.

May reject writes under severe failures.

Complete Replication Flow​