How would you implement a transaction that spans multiple services?

A transaction that spans multiple services is implemented using the Saga pattern, which replaces a single distributed transaction with a coordinated sequence of local transactions and compensations.

Key Points

Example

For a multi-service checkout, the Payment service charges the card, then the Inventory service reserves stock; if inventory reservation fails, a compensating transaction refunds the charge, ensuring the customer is never charged for an item that couldn't actually be reserved.

// Orchestrated saga: each step has a compensating action
@Service
public class CheckoutSaga {

    public void checkout(Order order) {
        String paymentId = paymentClient.charge(order.id(), order.total());
        try {
            inventoryClient.reserve(order.id(), order.items());
            shippingClient.schedule(order.id());
            orderRepository.markConfirmed(order.id());
        } catch (Exception e) {
            // Undo the steps that already succeeded, in reverse order
            inventoryClient.release(order.id());
            paymentClient.refund(paymentId);
            orderRepository.markCancelled(order.id(), e.getMessage());
        }
    }
}
// In production, store the saga state in a table so it can resume after a
// crash, and make every step and compensation idempotent.

Interview Tip

A concise interview answer is

I'd implement it with the Saga pattern: split the transaction into local steps owned by each service, and define a compensating action for every step that changes state. If a later step fails, the saga runs compensations in reverse order to unwind what already succeeded, giving eventual consistency without a distributed transaction.

Last updated: 1 November 2026

Previous question: What is eventual consistency?

Next question: What are the trade-offs of using eventual consistency vs. strong consistency?

More Microservices - Level I interview questions

All Microservices - Level I questions