Overview

5 Reliability

This chapter frames Kafka reliability around three intertwined pillars: durability, availability, and consistency. It explains how replication and the leader–follower model underpin durability and availability, while consistency requires additional controls and trade-offs. Readers are guided to think in failure scenarios—broker loss, recovery, and partial outages—and to balance the ability to read/write through disruptions against the need for strict correctness. The chapter previews the main mechanisms Kafka offers to shape that balance: producer acknowledgments, in-sync replicas (ISR) with configurable minimums, idempotent writes, transactions, and operational leader election behavior.

The producer acknowledgment modes define delivery guarantees and performance: acks=0 favors low latency but risks loss (at-most-once), acks=1 improves durability on the leader but can still lose data on fast failover, and acks=all coordinates with ISR to maximize safety. The topic’s min.insync.replicas setting is pivotal with acks=all; if not met, producers are rejected instead of silently weakening guarantees. The chapter then clarifies at-most-once, at-least-once, and exactly-once delivery, showing how idempotent producers (sequence numbers, producer IDs, NACKs for out-of-order records) remove duplicates and preserve per-partition ordering with minimal overhead. Practical guidance highlights keeping idempotence enabled, choosing sensible ISR and replication factors, and understanding how retries, in-flight limits, and delivery timeouts interact.

For end-to-end correctness across topics, Kafka transactions provide atomic writes to multiple partitions and allow coupling output records with consumed offsets for exactly-once processing. The producer initializes and bounds each transaction; a transaction coordinator writes commit/abort markers, and consumers must use isolation.level=read_committed to see only committed results. The chapter notes overhead characteristics (more partitions per transaction imply more coordination) and offers patterns for integrating databases (idempotent upserts or storing offsets with data). Finally, it deepens the leader–follower model: followers fetch from leaders and report progress to form the ISR; on leader failure, controllers elect a new leader from ISR, later restoring preferred leaders after recovery. Operational tools and cautions cover under-replicated diagnostics, preferred leader elections to rebalance, and the risks of unclean leader elections that trade availability for potential data loss.

Different ACK configurations. With acks=0 the producer sends messages and does not wait for confirmation if they have really arrived. With acks=1, the producer gets a response as soon as the leader has received the message. With acks=all (or acks=-1), the producer does not get feedback until all in-sync replicas have replicated the message.
With acks=0 we get at-most-once delivery guarantees.
Problems arise when the producer expects ACKs and these are lost on the way from the Leader to the producer, although the data was written successfully. The producer sends the message again in this case, and the message is in the log twice, possibly even in the wrong order (not shown).
If idempotency is enabled, the producer sends its producer ID with each message and also a sequence ID for each message. This allows the broker to detect if messages are missing, appear in the wrong sequence, or have been sent multiple times, and to respond accordingly.
If a message was received out of order, then the broker sends a NACK to the producer. This way, we can ensure the correct order of messages and prevent, for example, message B from arriving before message A.
Transactions in Kafka allow us to atomically write to two or more partitions. Either all messages are successfully written, or none are.
As soon as a message that belongs to an unfinished transaction is read by the consumer library, our code stops receiving new messages, even those that do not belong to that transaction. The ordering guarantee still holds. Internally, the messages are still buffered. When a transaction-commit is received, all messages are released from the buffer and sent to our code. If a transaction is aborted, our code does not receive messages belonging to the aborted transaction, but messages not belonging to the transaction are still received normally.
The partitions of our topic are distributed among our three brokers. Broker 2 is the leader for Partition 0, broker 3 is the leader for Partition 1 and Broker 1 is the leader for Partition 2. Since we have a replication factor of 3 and also three brokers, each partition is found on each broker.
Followers continuously fetch data from the leader to stay current and to be able to take over the leader's role if the leader fails.
The controller has detected the failure of Broker 3. Now that the leader for Partition 1 has failed, the controller picks another broker (e.g. broker 1) that is in-sync for this Partition to become the leader.
Although Broker 3 is accessible again, it does not immediately become the leader for Partition 1.
After the preferred leader selection (manual or automatic) is performed, Broker 3 again becomes the leader for Partition 1, and Broker 1 becomes the follower for that partition.

Summary

  • Acknowledgements are pillars of Kafka's reliability.
  • Acknowledgements are optional and configurable for each producer.
  • Producers can opt for no acknowledgement (acks=0) for speed, sacrificing resilience.
  • Producers can choose to receive ACKs after the leader receives the message (acks=1) or after replication to all ISR (acks=all), with the latter being now the default and most reliable.
  • Kafka offers three message delivery guarantees: at most once, at least once, and exactly once.
  • At most once is achieved with acks=0.
  • At least once is achieved with acks=all and sufficient minimum ISRs.
  • Exactly once requires enabling idempotence and setting acks=all.
  • Transactions in Kafka enable atomic writes across multiple partitions, ensuring all messages in a transaction are written together or not at all, maintaining data consistency.
  • Kafka uses idempotent producers for reliable message production and a variation of the 2-Phase-Commit protocol to manage transaction commit markers, ensuring messages are processed exactly once.
  • Consumers must set the isolation.level to read_committed to avoid reading uncommitted messages, ensuring only fully completed transactions are processed to maintain data integrity.
  • Kafka's transaction coordinator ensures reliability, even in the event of broker or producer crashes, though it introduces some performance overhead.
  • For each partition, there is a broker acting as the leader, handling all requests, while followers replicate data by fetching from the leader.
  • If a partition leader becomes unavailable, an in-sync replica (ISR) takes over the leader role.
  • When the previous leader is back in-sync, it can become the leader again, as Kafka aims to reinstate the original leader, known as the preferred leader.
  • In critical failures, Kafka can perform an unclean leader election, by allowing non-in-sync replicas to become leaders, which can lead to data loss.
  • Kafka's leader-follower replication ensures high availability and fault tolerance in the event of broker failures.

FAQ

What producer acknowledgment (acks) settings does Kafka support, and how do they affect reliability and latency?Kafka supports acks=0, acks=1, and acks=all (or -1). acks=0 gives the lowest latency but no delivery guarantees. acks=1 acknowledges as soon as the leader writes the record, improving latency but risking loss if the leader crashes before followers replicate. acks=all waits for all in-sync replicas (ISRs) to persist the record, maximizing durability at the cost of higher latency.
How does min.insync.replicas interact with acks=all?min.insync.replicas sets the minimum number of in-sync replicas that must acknowledge a write when using acks=all. If the number of ISRs drops below this threshold, producers receive errors (e.g., NOT_ENOUGH_REPLICAS), and writes are rejected until ISR recovers. This protects against data loss but can reduce availability during failures.
What delivery guarantees can Kafka provide, and which settings enable them?- At-most-once: acks=0; messages may be lost but are never duplicated. - At-least-once: acks=all and a sensible min.insync.replicas (e.g., 2 with RF=3); duplicates are possible. - Exactly-once: enable idempotence on the producer and, for end-to-end processing, use transactions plus read_committed consumers and transactional offset commits.
What is an idempotent producer, and which configurations are required?An idempotent producer ensures each message is written exactly once and in order per partition, even on retries. Requirements: enable.idempotence=true, acks=all, retries>0 (default is very high), and max.in.flight.requests.per.connection ≤ 5. This is enabled by default in newer Kafka clients (Kafka 3.3+).
Why can duplicates occur even if a message was successfully written?If the broker writes the message but the ACK back to the producer is lost, the producer assumes failure and retries, causing duplicates. Idempotent producers prevent this by attaching producer and sequence IDs so brokers detect and ignore duplicates while still returning the ACK.
What happens to reads and writes when ISR drops below the configured minimum?- Writes with acks=all are rejected with NOT_ENOUGH_REPLICAS. - Writes with acks=1 may still succeed but risk loss if the leader fails before replication. - Consumers may be unable to read messages that do not meet min.insync.replicas constraints until replicas catch up and rejoin ISR.
How do Kafka transactions work, and when should I use them?Transactions let you atomically produce to multiple partitions (and atomically commit consumer offsets with the produced results). You initialize a transactional producer (transactional.id), begin a transaction, produce records (and send offsets to the transaction if consuming), then commit (or abort). Use transactions for exactly-once processing in pipelines that read from topics and write to others.
How do I achieve exactly-once processing when my app both consumes and produces?Disable consumer auto-commit (enable.auto.commit=false). After processing a batch, use the producer’s send_offsets_to_transaction() to include the consumer offsets in the same transaction as your output records. Commit the transaction. Set consumer isolation.level=read_committed so downstream readers see only committed results.
What is the transaction coordinator and what are the performance implications?The transaction coordinator orchestrates transactional state, writes commit/abort markers, and ensures atomicity and recovery. Overhead scales with the number of involved partitions, not the number of records. Short transactions (e.g., ~10 ms) have proportionally higher overhead than longer ones; typical measured overhead was a few percent for ~100 ms transactions at high throughput.
How does leader–follower replication, ISR, and leader election work in Kafka?Each partition has one leader and one or more followers. Followers fetch from the leader and stay in the ISR when caught up. On leader failure, a new leader is elected from ISR (or, in emergencies, via unclean leader election from non-ISR—risking data loss). Use kafka-topics.sh --under-replicated-partitions to find partitions missing replicas. Preferred leader election can rebalance leaders back to their preferred replicas when brokers recover.

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Apache Kafka in Action ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Apache Kafka in Action ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Apache Kafka in Action ebook for free