How to ensure data entry and update in different databases

Asked

Viewed 33 times

2

I’m developing an application with a microservice architecture and a question has arisen. Each application functionality will have an isolated API, how can I ensure that when making an Insert/update in the x functionality database, it also makes an Insert/update in the y functionality database? * Assuming these features can be separate locations as well as databases.

1 answer

1

It is a complicated problem to ensure a perfect solution. Look for the "Problem of the Byzantine Generals".

One way to solve this is to use the "two-Phase commit" technique, or 2-phase commit. In the first phase, you invoke a pre-commit API. If all services return success in the pre-commit phase, a second API (in each of them) is invoked effecting the commit.

There could still be a glitch in the caller, so that the ultimate commit is invoked on only a portion of remote services, but mitigating techniques can be adopted for this (e.g. note down all operations pending in a Journal or disk record before invoking, and remove from the Journal only when all operations are invoked. If the caller breaks in the middle of the commit list, it redoes the commits when it runs again.)

Anyway, the fact is that remote Apis need to provide the basic tool, which is the 2-phase commit. Remote services need to be thought of in such a way that once the pre-commit has been approved, the final commmit can never fail.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.