Kafka Transactions & Read-Process-Write
Explore the internal 2-phase commit protocol of Kafka transactions, the Transaction Coordinator, zombie fencing, and read_committed isolation.
The most common architectural pattern in event-driven stream processing is the Read-Process-Write loop: an application reads an incoming event from Topic A, applies business transformations, produces one or more output events to Topic B, and commits its input offset to __consumer_offsets.
If the application crashes midway through this cycle—after producing to Topic B but before committing the input offset—the replacement instance will re-read the original event and produce a duplicate output record to Topic B.
Apache Kafka solves this with Kafka Transactions (KIP-98), enabling atomic multi-partition writes where output records and consumer offsets commit together in a single atomic transaction.
In this lesson, you will trace the internal mechanics of Kafka's 2-phase commit protocol, understand the Transaction Coordinator, and learn how isolation.level=read_committed prevents dirty reads.
01The Problem: Partial Failures in Stream Processing
Consider a payment processing microservice:
[Input: "orders"] -> [ShopFlow Payment Engine] -> [Output: "payments"]
-> [Offsets: "__consumer_offsets"]Without transactions, producing output records and committing consumer offsets are two separate network operations:
- Application reads
OrderPlacedat offset 100. - Application produces
PaymentDebitedto thepaymentstopic. The broker writes it to disk. - Before the application can commit offset 100 to
__consumer_offsets, the host server loses power. - When the service restarts, the consumer re-reads
OrderPlacedat offset 100 and sends a secondPaymentDebitedrecord to thepaymentstopic.
The downstream banking gateway now processes two debits for a single order.
Loading the rest of the lesson…