Interservice communication in a microservices setup

Status:: 🟩
Links:: Data Consistency in Distributed Systems Google Cloud Architecture Center

Metadata

Authors:: Google
Title:: Interservice communication in a microservices setup
Date:: 2023
URL:: https://cloud.google.com/architecture/microservices-architecture-interservice-communication
DOI::

Notes & Annotations

πŸ“‘ Annotations (imported on 2024-01-22#21:24:35)

google.2023.interservicecommunicationmicroservices (pg. 2)

In the monolithic application, the database system ensures that the local transactions are atomic. However, by default, the microservice-based system that has a separate database for each service doesn't have a global transaction coordinator that spans the different databases. Because 3/10 transactions aren't centrally coordinated, a failure in processing a payment doesn't roll back changes that were committed in the order service. Therefore, the system is in an inconsistent state.

google.2023.interservicecommunicationmicroservices (pg. 3)

Two-phase commit protocol (2PC): Part of a family of consensus protocols, 2PC coordinates the commit of a distributed transaction and it maintains atomicity, consistency, isolation, durability (ACID) guarantees. The protocol is divided into the prepare and commit phases. A transaction is committed only if all the participants voted for it. If the participants don't reach a consensus, then the entire transaction is rolled back.

google.2023.interservicecommunicationmicroservices (pg. 3)

Saga: The Saga pattern consists of running local transactions within each microservice that make up the distributed transaction. An event is triggered at the end of every successful or failed operation. All microservices involved in the distributed transaction subscribe to these events. If the following microservices receive a success event, they execute their operation. If there is a failure, the preceding microservices complete compensating actions to undo changes. Saga provides a consistent view of the system by guaranteeing that when all steps are complete, either all operations succeed or compensating actions undo all the work.

google.2023.interservicecommunicationmicroservices (pg. 3)

We recommend Saga for long-lived transactions. In a microservices-based application, you expect interservice calls and communication with third-party systems. Therefore, it's best to design for eventual consistency: retry for recoverable errors and expose compensating events that eventually amend non-recoverable errors.

google.2023.interservicecommunicationmicroservices (pg. 3)

There are various ways to implement a Sagaβ€”for example, you can use task and workflow engines such as Apache Airflow,, Apache Camel, or Conductor. You can also write your own event handlers using systems based on Kafka, RabbitMQ, or ActiveMQ.

google.2023.interservicecommunicationmicroservices (pg. 3)

The Online Boutique application uses the checkout service to orchestrate the payment, shipping and email notification services. The checkout service also handles the business and order workflow. As an alternative to building your own workflow engine, you can use third-party component such as Zeebe. Zeebe provides a UI-based modeler. We recommend that you carefully evaluate the choices for microservices orchestrator based on your application's requirements. This choice is a critical part of running and scaling your microservices.

google.2023.interservicecommunicationmicroservices (pg. 3)

To enable distributed transactions in the refactored application, the checkout service handles the communication between the payment, shipping, and email service.

google.2023.interservicecommunicationmicroservices (comment) (pg. 3)

An order workflow that helps ensure distributed transactions in typical microservices.