Microservices Saga Pattern with Spring State machine

Microservices Saga Pattern with Spring State machine

What are sagas in microservices ?

A?database-per-microservice?model provides many benefits for microservices architectures. Encapsulating domain data lets each service use its best data store type and schema, scale its own data store as necessary, and be insulated from other services' failures. However, ensuring data consistency across service-specific databases poses challenges. Two answers for this chanllenge : two phase commit , Sagas

The Saga design pattern is a way to manage data consistency across microservices in distributed transaction scenarios through:

  1. A saga is a sequence of transactions that updates each service and publishes a message or event to trigger the next transaction step.
  2. It also provides transaction management using a sequence of?local transactions which is the atomic work effort performed by a saga participant that updates the database and publishes a message or event to trigger the next local transaction in the saga.
  3. If a local transaction fails, the saga executes a series of?compensating transactions?that undo the changes that were made by the preceding local transactions.

What is Spring state machine ?

State machine?is a?model of computation based on the finite states .Usually there are workflows to go with the states. The transitions between these states are limited by the rules. The Spring framework?has a whole library called Spring State Machine.Please see my previous article

How to Model Sagas as state machine ?

Modelling a saga orchestrator as a state machie is an effective way to manage distributed transactions :

  1. Use a state machine that consists of a set of states and a set of transitions.Transitions between states that are triggered by events. Each transition can have an action, which for a saga is the invocation of a saga participant. The transitions between states are triggered by the completion of a local transaction performed by a saga participant.The current state and the specific outcome of the local transaction determine the state transition and what action, if any, to perform.
  2. ?The saga orchestrator is linked to a state machine, which is responsible for managing transaction states through a state manager API. In addition to that, it is also responsible for storing transaction states in a persistent data store to ensure recovery when system faults happen.
  3. The saga state machine thus has the responsibility to either get the overall business transaction completed or to leave the system in a known state, so that it can determine the order in which to potentially execute the next state of actions, or compensation activities, whether transactions occurring are distributed in nature or, long lived.

What are the best practices for Saga Orchestrator state machine ?

  1. Orchestrator should only be responsible for managing transactions and states, and there should not be any business logic added here. Business logic should be defined in individual service participants.
  2. All events and commands to and from the orchestrator should be carrying only transaction data, not reference data.
  3. Use asynchronous style messaging to communicate between services.
  4. Implement idempotency and state checks for resiliency, if using message brokers like Kafka.

What are the advantages of this Model ?

  • Simpler dependencies — The orchestrator depends on the participants but not vice versa, and so there are no cyclic dependencies.
  • Less coupling —?Each service implements an API that is invoked by the orchestrator.
  • Separation of concerns —?The saga coordination logic is localized in the saga orchestrator.
  • Data Consistency —?Data consistency across multiple microservices
  • Developer experience?- Focus Only on business logic

References:

[1] https://learn.microsoft.com/en-us/azure/architecture/reference-architectures/saga/saga

[2] https://www.youtube.com/watch?v=vx8a0Wrvo64

[3] https://levelup.gitconnected.com/modelling-saga-as-a-state-machine-cec381acc3ef

要查看或添加评论,请登录

Marwa Ali的更多文章

  • Spring Security 6 with Spring Boot 3 + KeyCloak

    Spring Security 6 with Spring Boot 3 + KeyCloak

    What is KeyCloak ? KeyCloak Open Source Identity and Access Management.It provides user federation, strong…

    1 条评论
  • Spring Security 6 with Spring Boot 3 + JWT

    Spring Security 6 with Spring Boot 3 + JWT

    In continuation to my article Spring security 6 and spring boot 3 , Next introducing JWT token. Learn Jwt token here .

  • Spring Security 6 with Spring Boot 3

    Spring Security 6 with Spring Boot 3

    Say goodbye to Old security , Say Hi to Spring Security 6 with Spring Boot 3 . it is easier and simpler.

  • SpringBoot batch framework

    SpringBoot batch framework

    Spring Batch is a lightweight, comprehensive batch framework designed to enable the development of robust batch…

  • Dockerizing Springboot Application

    Dockerizing Springboot Application

    Docker is a powerful tool that allows developers to package their applications into containers that can be easily…

  • Kafka Event sourcing in Event Driven Architecture

    Kafka Event sourcing in Event Driven Architecture

    What is Event Sourcing ? Event Sourcing is ensuring every change to the state of an application is captured in an event…

  • Istio addons

    Istio addons

    #devops #istio #grafana #promtheus #servicemesh Please see my previous artcile at Grafana An open source monitoring…

  • Istio service mesh

    Istio service mesh

    #devops #kubernets #istio #servicemesh What is a service mesh? Developers and operators face chanllenges with a…

  • Springboot Distributed State Machine

    Springboot Distributed State Machine

    #statemachine What is a distributed state? An application may exist in a finite number of states. when something…

  • SpringBoot State machine

    SpringBoot State machine

    The concept of a state machine is most likely older than any reader of this reference documentation and definitely…

社区洞察

其他会员也浏览了