Why would one use sagas over 2PC and vice versa?

Technology CommunityCategory: MicroservicesWhy would one use sagas over 2PC and vice versa?
VietMX Staff asked 3 years ago

Here are two approaches which I know are used to implement distributed transactions:

  • 2-phase commit (2PC)
  • Sagas

2PC is a protocol for applications to transparently utilize global ACID transactions by the support of the platform. Being embedded in the platform, it is transparent to the business logic and the application code as far as I know.

Sagas, on the other hand, are series of local transactions, where each local transaction mutates and persist the entities along with some flag indicating the phase of the global transaction and commits the change. In the other words, state of the transaction is part of the domain model. Rollback is the matter of committing a series of “inverted” transactions. Events emitted by the services triggers these local transactions in either case.

  • Typically, 2PC is for immediate transactions.
  • Typically, Sagas are for long running transactions.

I personally consider Saga capable of doing what 2PC can do, but they have the overhead of implementing the redo mechanism. Opposite is not accurate. I think Sagas are universal, while 2PC involves platform/vendor lockdown and lacks platform independence.