Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Data integrity across the databases of different Microservices

Let's say I am using relational databases for my microservices. I have CustomersMService which has its own database with table Customer, then I have OrdersMService which also has its own database but with table Order and that table has column CustomerId. My question is how can I ensure data integrity between databases, that Orders table won't point to non-existent Customers?

like image 889
uvytautas Avatar asked May 19 '17 08:05

uvytautas


People also ask

How do you ensure data integrity in microservices?

For transactions, use patterns such as Scheduler Agent Supervisor and Compensating Transaction to keep data consistent across several services. You may need to store an additional piece of data that captures the state of a unit of work that spans multiple services, to avoid partial failure among multiple services.

Can different microservices use same database?

Yes, it's possible to integrate a database for microservices. You can create a single shared database with each service accessing data using local ACID transactions.

Should microservices use different databases?

It means that we can use different database technologies for different microservices. So one service may use an SQL database and another one a NoSQL database. That's feature allows using the most efficient database depending on the service requirements and functionality.

How do I manage multiple databases in microservices?

Create a single database for different microservices is anti-pattern, then the correct way is to create a database for each microservice.


1 Answers

My question is how can I ensure data integrity between databases, that Orders table won't point to non-existent Customers?

It's a great question. There is an important dimension missing from it though, which is that of the span of time over which you wish to establish the referential integrity.

If you ask, "How can I ensure that all my data is 100% consistent at all times?" - well the answer is you can't. If you want that you will need to enforce it, either via foreign key constraints (which are unavailable across databases), or by making sure you never write to one database and not the other database outside of some distributed transaction (which is absurd and would defeat the purpose of using service orientation).

If you ask, "How can I ensure that all my data is 100% consistent after a reasonable span of time?", then there are things you can do. A common approach is to implement durable, asynchronous eventing between your services. This ensures that changes can be written locally and then dispatched remotely in a reliable, but offline manner. A further thing you can do is have a scheduled caretaker process which periodically remediates inconsistencies in your data.

However it has to be said that outside of a transaction, even over a reasonable span of time, consistency is impossible to guarantee absolutely. If absolute consistency is a requirement for your application then service orientation may not be the approach for you.

like image 137
tom redfern Avatar answered Sep 27 '22 23:09

tom redfern