Kafka
Data Corruption
Message Replication
Troubleshooting
System Architecture

Kafka message corrupted in master but replica looks good

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Apache Kafka is a distributed streaming platform that enables you to publish and subscribe to streams of records, store records in a fault-tolerant way, and process streams of records as they occur. Kafka is widely used due to its high-throughput, built-in partitioning, replication, and inherent fault tolerance. One common issue that can arise in the operation of a Kafka cluster is message corruption. To understand this, we first need to delve into how Kafka handles data replication and storage.

Understanding Kafka's Replication Mechanism

Kafka stores records in topics which are divided into partitions. Each partition is replicated across a configurable number of servers for fault tolerance. Typically, one server acts as the leader, and the rest are followers. The leader handles all read and write requests for the partition, while the followers passively replicate the leader’s log.

When a new record is published to a partition, the leader appends the record to its log and sends it to its followers. Only after all in-sync replicas (ISR) have confirmed the reception of the record does the leader consider the write operation successful. This ensures the availability and durability of messages.

Scenario: Corruption in the Leader but Not in Followers

It’s rare but possible for a corruption issue to occur on the leader while replicas remain unaffected. This could be related to a disk failure, a network error while writing to the leader’s log, or a software bug.

If the master (leader) log becomes corrupted but the follower replicas are still intact, the corrupted messages might only affect the leader. Consumers reading from the leader could encounter errors, or the leader might fail completely if the corruption is severe.

Technical Analysis of the Situation

Detection: Corruption can be detected in various ways, including checksum failures during log compaction, replication mismatches reported by followers, or even crashes of the Kafka server process if it encounters unmanageable inconsistency.

Resolution: Once corruption is detected, a typical resolution process would involve:

  1. Promoting a healthy replica to be the new leader. Kafka automatically handles leader election among ISR when the current leader fails or is deemed ineligible.
  2. Recreating the leader's log. This can be done by re-fetching all messages from the new leader or restoring from a backup, if available.

Example Scenario

Consider a Kafka cluster with one topic partition having one leader and two followers:

  1. The leader receives messages and appends them to its log.
  2. Due to a hardware issue, some of the messages in the leader’s log get corrupted.
  3. Followers continue to replicate the pre-corruption messages successfully.
  4. Corruption is detected on the leader through checksum failures.
  5. A follower is automatically promoted to leader.
  6. The former leader re-fetches the log from the new leader to repair its log.

Table: Summary of Key Points in Message Corruption and Resolution

AspectDetails
Fault ToleranceReplicas maintain additional copies of the data.
Corruption DetectionChecksum errors, follower mismatch, or server crashes.
Automatic RecoveryKafka’s leader election promotes a healthy replica.
Manual InterventionRarely needed unless automatic recovery fails.
PreventionRegular hardware checks, updated software, monitoring.

Preventive Measures

To reduce the likelihood of such issues:

  • Regular monitoring and alerts for hardware and network issues.
  • Frequent software updates and patches to Kafka and underlying system.
  • Thorough testing in staging environments especially for storage components.

Conclusion

Message corruption in Kafka can lead to significant issues in data integrity and availability. However, Kafka’s robust replication model generally allows automatic recovery from such scenarios without data loss. By understanding Kafka’s internals and maintaining a vigilant monitoring system, these issues can be minimized, ensuring continuous high availability and data integrity in your Kafka clusters.


Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.