CAP does not directly provide out-of-the-box MS DTC or 2PC-based distributed transactions, instead we provide a solution that can be used to solve problems encountered in distributed transactions.
In a distributed environment, using 2PC or DTC-based distributed transactions can be very expensive due to the overhead involved in communication, as is performance. In addition, since distributed transactions based on 2PC or DTC are also subject to the CAP theorem, it will have to give up availability (A in CAP) when network partitioning occurs.
A distributed transaction is a very complex process with a lot of moving parts that can fail. Also, if these parts run on different machines or even in different data centers, the process of committing a transaction could become very long and unreliable.
This could seriously affect the user experience and overall system bandwidth. So one of the best ways to solve the problem of distributed transactions is to avoid them completely.1
For the processing of distributed transactions, CAP uses the “Eventual Consistency and Compensation” scheme.
By far, one of the most feasible models of handling consistency across microservices is eventual consistency.
This model doesn’t enforce distributed ACID transactions across microservices. Instead, it proposes to use some mechanisms of ensuring that the system would be eventually consistent at some point in the future.
For example, suppose we need to solve the following task:
The second task is to ensure, for example, that this user wasn’t banned from our servers for some reason.
But it could take time, and we’d like to extract it to a separate microservice. It wouldn’t be reasonable to keep the user waiting for so long just to know that she was registered successfully.
One way to solve it would be with a message-driven approach including compensation. Let’s consider the following architecture:
The messaging platform could ensure that the messages sent by the microservices are persisted. Then they would be delivered at a later time if the receiver weren’t currently available
In this architecture, a happy scenario would be:
After we’ve gone through all these steps, the system should be in a consistent state. However, for some period of time, the user entity appeared to be in an incomplete state.
The last step, when the user microservice removes the invalid account, is a compensation phase.
Now let’s consider some failure scenarios:
Even if some of the messages were issued multiple times, this wouldn’t affect the consistency of the data in the microservices’ databases.
By carefully considering all possible failure scenarios, we can ensure that our system would satisfy the conditions of eventual consistency. At the same time, we wouldn’t need to deal with the costly distributed transactions.
But we have to be aware that ensuring eventual consistency is a complex task. It doesn’t have a single solution for all cases.
This chapter is quoted from: https://www.baeldung.com/transactions-across-microservices ↩︎