How Distributed Transactions Work in Microservices: 2PC, Saga, and Kafka Explained

When an order spans multiple microservices — such as OrderService, PaymentService, and InventoryService — a standard local ACID transaction is insufficient because each service operates on its own database. Two-Phase Commit (2PC) attempts to enforce atomicity across services via a coordinator, but risks performance bottlenecks due to resource locking. The Saga pattern offers an alternative by breaking the workflow into independent local transactions, using compensating actions to reverse completed steps if a later stage fails. Saga coordination can be implemented via choreography, where services react to events published on a message broker like Kafka, or via orchestration, where a central coordinator directs each step. Because compensations are new business transactions rather than true rollbacks, Sagas provide eventual consistency rather than the strong consistency of a single atomic transaction.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.


Discussion (0)
Log in to join the discussion and vote.
Log in