Sobes.tech
Middle+

What is the Saga pattern?

sobes.tech AI

Answer from AI

The Saga pattern is used for managing distributed transactions in a microservices architecture, where traditional ACID transactions are not possible.

The idea is that a large business operation is broken down into a series of local transactions, each executed in a separate service. If one of the transactions fails, the previously successful transactions are compensated (rolled back) with compensating actions.

Thus, Saga ensures data consistency through a sequence of steps and compensating operations, instead of a single atomic transaction.

Example:

  1. An order is created in the order service.
  2. The product is reserved in the warehouse service.
  3. Money is deducted in the payment service.

If, for example, the money deduction fails, compensating operations are performed:

  • the product reservation is canceled,
  • the order is canceled.

This allows maintaining data integrity in a distributed system without locks and long transactions.