While Microservice applications are built as a set of modular components, they are easier to understand, simpler to test and effortless to maintain over the life of the application. It enables organizations to achieve agility and be able to improve the time it takes to get working enhancements to production.
On the downside, there are quite a bit of challenges as well, each service has its own database and wherever business transactions span multiple services you need a mechanism to ensure data consistency across services. For example, If you are building a web store application where customers have a credit limit. The application must ensure that a new order will not exceed the customer’s credit limit. Since Orders and Customers are in different databases the application cannot simply use a local ACID transaction.
In this post, let’s look at the available options to implement data consistency across Microservices.
Cross-Posted : ContainerJournal
Implement each business transaction that spans multiple services as a sequence of local transactions. Each local transaction updates the database and publishes a message or event to trigger the next local transaction. If a local transaction fails because it violates a business rule then executes a series of compensating transactions that undo the changes that were made by the preceding local transactions.
There are two ways to do it:
For example,If you’re building store application that uses this approach,it would create an order that consists of the following steps:
Order Service
creates an Order in a pending state and publishes an ORDER_CREATED eventCustomer Service
receives the event attempts to reserve credit for that Order. It publishes either a CREDIT_RESERVED
event or a CREDIT_LIMIT_EXCEEDED
event.Order Service
receives the event and changes the state of the order to either approved
or cancelled
Order Service
creates an Order in a pending state and creates a CreateOrderOrchestration
CreateOrderOrchestration
sends a ReserveCredit
command to the Customer Service
Customer Service
attempts to reserve credit for that Order and sends back a replyCreateOrderOrchestration
receives the reply and sends either an ApproveOrder
or RejectOrder
command to the Order Service
Order Service
changes the state of the order to either approved
or cancelled
To address these issues, eBay has developed GRIT protocol for globally consistent distributed transactions that combines ideas from optimistic concurrency control (OCC) , 2PC, and deterministic databases to achieve, for the first time, high-performance, globally consistent transactions across microservices with multiple underlying databases.
In the next section, we look at eBay’s new protocol to address the above issues.
The following diagram illustrates the GRIT protocol in a microservices application with two databases.
To understand better, let’s look at key components that make up globally consistent distributed transactions.
Now that we have understood the components, we look at the steps for a distributed transaction.
Following diagram shows the main steps for a distributed transaction
In GRIT, a distributed transaction goes through three phases:
Though there is no single approach that fits all, Cloud applications typically use data that is dispersed across data stores. Managing and maintaining data consistency in this environment can become a critical aspect of the system, particularly in terms of the concurrency and availability issues that can arise. You would need to trade strong consistency for availability. This means that you may need to design some aspects of your solutions around the notion of eventual consistency and accept that the data that your applications use might not be completely consistent all of the time.
Like this post? Don’t forget to share it!
Writing software backed by artificial intelligence can create everything from emails to blog articles. AI…
In today's interconnected digital realm, cybersecurity stands as a paramount concern for organizations, irrespective of…
The cryptocurrency market is famed for its volatility, presenting each opportunity and demanding situations for…
Games since time immemorial have been winning at captivating the users and teleporting them onto…
We are living within an innovation curve wherein cutting-edge technologies are making a hustle and…
Whether it’s the healthcare industry or the automobile sector, artificial intelligence has left its impact…
This website uses cookies.