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?
假设我正在为我的微服务使用关系数据库。我有CustomersMService,它有自己的数据库与表Customer,然后我有OrdersMService,它也有自己的数据库,但有表Order和该表有列CustomerId。我的问题是如何确保数据库之间的数据完整性,Orders表不会指向不存在的客户?
1 个解决方案
#1
8
My question is how can I ensure data integrity between databases, that Orders table won't point to non-existent Customers?
我的问题是如何确保数据库之间的数据完整性,Orders表不会指向不存在的客户?
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).
如果您问:“我怎样才能确保我的所有数据始终保持100%一致?” - 答案是你不能。如果你想要,你需要通过外键约束(在数据库之间不可用)或者确保你永远不会写入一个数据库而不是某个分布式事务之外的其他数据库来执行它(这是荒谬的并且会打败使用面向服务的目的)。
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.
如果你问“我怎样才能确保我的所有数据在合理的时间段内100%保持一致?”,那么你可以做些事情。一种常见的方法是在服务之间实现持久的异步事件。这可确保可以在本地写入更改,然后以可靠但离线的方式远程调度。您可以做的另一件事是拥有预定的看护人流程,该流程可定期修复数据中的不一致。
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.
然而,必须要说的是,在交易之外,即使在合理的时间范围内,也绝不能保证一致性。如果绝对一致性是您的应用程序的要求,那么面向服务可能不适合您。
#1
8
My question is how can I ensure data integrity between databases, that Orders table won't point to non-existent Customers?
我的问题是如何确保数据库之间的数据完整性,Orders表不会指向不存在的客户?
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).
如果您问:“我怎样才能确保我的所有数据始终保持100%一致?” - 答案是你不能。如果你想要,你需要通过外键约束(在数据库之间不可用)或者确保你永远不会写入一个数据库而不是某个分布式事务之外的其他数据库来执行它(这是荒谬的并且会打败使用面向服务的目的)。
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.
如果你问“我怎样才能确保我的所有数据在合理的时间段内100%保持一致?”,那么你可以做些事情。一种常见的方法是在服务之间实现持久的异步事件。这可确保可以在本地写入更改,然后以可靠但离线的方式远程调度。您可以做的另一件事是拥有预定的看护人流程,该流程可定期修复数据中的不一致。
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.
然而,必须要说的是,在交易之外,即使在合理的时间范围内,也绝不能保证一致性。如果绝对一致性是您的应用程序的要求,那么面向服务可能不适合您。