Real Async Microservices | SAGA Microservice Pattern.
Due to its synchronous nature, the HTTP protocol is inappropriate for microservices backends, instead, asynchronous communication is needed between the microservices. Adding some data to the database while excluding others from uploading to other databases. The Saga pattern will be used to resolve the issue.
It is a non-microservice implementation design that is adaptable as long as the repercussions are taken into account. The Saga design pattern is used to manage data consistency across microservices in distributed transaction environments. In a saga, each service is updated and a message or event is issued to start the subsequent transaction stage. The saga executes compensating transactions that reverse the previous transactions if a step fails.
Saga can implement in two different ways,
- choreographic :- choreographic means event based.
- orchestration :- command based.
Both can use on different use cases and has own problems.
choreography pattern and consider about a use case,
There is a new connection service, and whenever a request for a connection comes in, it must be verified to see if the consumer is reputable or has a television connection. Each successful verification notifies the finance service, which may subsequently produce the customer’s bill. The operating service will communicate the connection’s start if the charging process is successful. Services communicate with the Kafka topic through the event broker, which acts as a Kafka broker. The verification services simultaneously listen to the requests and broadcast the message the event to the topic when a new connection joins the service. There are publications for verification success as well as a topic serving as a verification answer. The new connection service is listening to the verification response topic as it waits for a response. The new connection service sends a separate event to the new connection topic informing it that the verification was successful when it judges that the verification was successful. A payment success message is sent to the verification response subject by the financial service after listening to the topic and determining whether the payment was successful. The phrase “payment is successful” is broadcast in the new connection topic whenever a new connection receives a payment success answer. When the payment is successful, the operation service monitors the new connection subject and establishes a new connection. It once again publishes as an activation success in the topic for the verification answer. Everything is ideal now that the issue has been resolved and there is no longer a service that is awaiting a response. Microservices cannot communicate if the messaging system is down, however they can quickly start up if MSK or another Kafka cluster is employed and there are many replicants. Since any service that is down can produce an alert, this strategy is helpful for service monitoring. The monitoring service can alert the services if a request has been published but no one is waiting.
In the orchestration model, there is a distinct command model service in addition to the identical new connection service. It notifies the command service when a new connection request comes in and then checks the user’s information to make sure they are a legitimate user and have a television connection. That procedure can also be run concurrently. Visit the finance service and request payment after confirming success. Inform the command service to activate the new connection when it is finished. The system can employ a hybrid strategy with an event driver, and the command service controls the process. A new request is then made and published by the command service to the message broker with a target service.
The disadvantage of choreography,
A deadlock could happen for a variety of reasons. A deadlock condition is challenging to fix and necessitates integration testing for hundreds of events. Because it makes its own decisions and doesn’t affect anything, having a central command module is advantageous.
Krishantha Dinesh, Lets do it right..! Real Async Microservices | SAGA Microservice Pattern | example inside, Jul 9, 2022.