How do you handle data consistency in microservices?

Data consistency in microservices refers to keeping data accurate and synchronized across services that each own their own database, without relying on a single shared transaction.

Key Points

Example

In an e-commerce checkout, the Order Service marks an order as "pending," the Inventory Service reserves stock, and the Payment Service charges the customer; if payment fails, a compensating event releases the reserved inventory and cancels the order.

// Transactional outbox: the order and the event are saved in ONE local
// transaction, so you never get an order without its event (or the reverse)
@Service
public class OrderService {

    private final OrderRepository orders;
    private final OutboxRepository outbox;

    public OrderService(OrderRepository orders, OutboxRepository outbox) {
        this.orders = orders;
        this.outbox = outbox;
    }

    @Transactional
    public Order placeOrder(CreateOrder cmd) {
        Order order = orders.save(Order.pending(cmd));
        outbox.save(new OutboxEvent("Order", order.getId(), "OrderPlaced", toJson(order)));
        return order;
    }
}
// A separate relay (a scheduled job or Debezium) publishes outbox rows to Kafka,
// and the Inventory and Payment services react to the event.

Interview Tip

A concise interview answer is

Since each microservice owns its own database, I rely on eventual consistency achieved through events, and use the Saga pattern to coordinate multi-service transactions — each step has a compensating action so that if one step fails, previous steps can be rolled back logically rather than through a distributed transaction.

Last updated: 1 November 2026

Previous question: How do microservices update their registration and discovery information?

Next question: What is eventual consistency?

More Microservices - Level I interview questions

All Microservices - Level I questions