What is the Distributed Transaction Problem?

Topic: microservices, Data Management. Level: Intermediate.

Answer

With database-per-service, a business operation spanning multiple services (place order → charge payment → reserve stock) cannot use a single ACID transaction, because each service has its own DB. Classic two-phase commit (2PC/XA) provides atomicity but is slow, locks resources, and reduces availability (blocking if the coordinator fails) — it does not fit highly available microservices. The pragmatic answer is to give up immediate consistency and use **Sagas** with compensating actions plus idempotency, accepting eventual consistency. ```java // This is IMPOSSIBLE across separate service databases: @Transactional void placeOrder() { orderDb.save(order); // order-service DB paymentDb.charge(payment); // payment-service DB <-- different DB, not in this tx inventoryDb.reserve(items); // inventory-service DB <-- different DB } // Solution: Saga (Q12) — a sequence of local transactions + compensations. ```