How is saga design pattern different from pub sub model in micro service architecture?

197 Views Asked by At

In saga one component calls another using a topic in between ,same thing happens in pub shub model .Then how both are different ?

1

There are 1 best solutions below

0
Peter Csala On

Saga pattern

This pattern can be used to deal with distributed transactions inside a microservice ecosystem. There are other well-known techniques like 2PC. While 2PC considered as single (distributed) commit, SAGA is a sequence of separate commits.

Happy path

Let's suppose we have 2 microservices which are participating in a distributed transaction

  1. Microservice #1 executes a local transaction and puts an entity into a creating state.
  2. Microservice #1 publishes an event about it which is consumed by Microservice #2
  3. Microservice #2 executes another local transaction and publishes an event about it
  4. Microservice #1 is interested about that event which was published by Microservice #2
  5. Microservice #1 process it by moving the 1st step's entity into a created state

Unhappy path

  1. Microservice #1 executes a local transaction and puts an entity into a creating state
  2. Microservice #1 publishes an event about it which is consumed by Microservice #2
  3. Microservice #2 executes another local transaction but it fails. It publishes a failure event
  4. Microservice #1 is interested about that event which was published by Microservice #2
  5. Microservice #1 process it by executing a compensation action by moving the 1st step's entity into a failedCreating state

Choreography vs Orchestration

In the previous example Microservice #1 and Microservice #2 knew what would be the next action after its local change. In other words they knew who should be informed about the transaction change. This type of coordination is called choreography.

In case of orchestration there is a centralized place/subsystem which coordinates the participants. In other words each microservice should know only about the orchestrator. The orchestrator knows who should do what when a given event happens.

Pub-Sub vs Saga

Pub-Sub is a mechanism to make two or more subsystems more loosely coupled by allowing asynchronous messaging and message processing.

The Saga pattern can be implemented by utilizing pub-sub to inform others (either via choreography or via orchestration) about an advancement of the transaction.

  • The message can be positive to move forward
  • Or can be negative and requires compensation actions to rollback

The communication can be done by utilizing other mechanisms like web-hooks or websocket, or ... but they are less common than using pub-sub (based on my experience).


For further information please check this websites: