Sobes.tech
Middle — Middle+

How to organize the rollback of a transaction or operation in a distributed system using microservices?

sobes.tech AI

Answer from AI

In distributed systems with microservices, transaction rollback is organized using patterns for managing distributed transactions, as traditional ACID transactions do not work across service boundaries.

Main approaches:

  • Sagas — a sequence of local transactions in different services, each with a compensating operation for rollback. If one transaction fails, compensating operations are executed to undo previous steps.

  • Saga orchestration — a centralized controller manages the execution and rollback of steps.

  • Saga choreography — each service independently reacts to events and performs its steps and compensations.

Example: when placing an order, the order service creates an order, the payment service deducts money, and the delivery service plans delivery. If the money deduction fails, the order service initiates a compensating operation — canceling the order.

This approach ensures data consistency without global locks and distributed transactions.