SAGA ==== - long running transactions (distributed) across multiple microservices/DBs. way to manage data consistency across microservices in distributed transaction scenarios. - alternative to the two-phase commit protocol for managing distributed transactions. - atomic : all-or-nothing - durable: the state of the saga is persisted after each transaction. - responsive: each local transaction is executed within a bounded time interval. - on activity/step failure: - fault tolerance : retry policies; timeouts - compensating actions are executed to undo the changes. here compensation does not necessarily mean rollback, it can be anything that can bring the system to a consistent state. - compensating actions are executed in reverse order. - being able to resume an execution from where it left off after a failure (system crash, network partition, etc.) Orchestration ------------- central component (orchestrator - the orchestrator is responsible next local transaction to invoke. - the orchestrator is responsible failure. - the orchestrator is responsible - the orchestrator is responsible system failure. | state machine) that coordinates the saga. for invoking the local transactions and deciding the for invoking the compensating actions in case of a for persisting the state of the saga. for resuming the saga from where it left off after a Choreography ------------ sequence of local transactions. each local transaction updates the database and publishes a message or event to trigger the next local transaction in the saga. - each service is responsible for listening to events and deciding whether to react to the event by invoking local transactions and emitting new events. - each service is responsible for invoking the compensating actions in case of a failure. - each service is responsible for persisting the state of the saga. - each service is responsible for resuming the saga from where it left off after a system failure. SAGA vs 2PC ----------- 2PC is a blocking protocol, whereas saga is non-blocking. - 2PC is a binary protocol (all-or-nothing), whereas saga is a multi-phase protocol. - 2PC is a synchronous protocol, whereas saga is an asynchronous protocol. - 2PC is a pessimistic protocol, whereas saga is an optimistic protocol. - 2PC is a centralized protocol, whereas saga is a decentralized protocol. -----------------------------------------------------------------------------------------------------------------------