Hacker News new | ask | show | jobs
by tirumaraiselvan 2675 days ago
How does CQRS solve the problem of referential integrity?
1 comments

With a distributed system, there is no such thing as referential integrity. You can only mitigate the issue, and CQRS is good at that.
Can you give me a concrete example of using CQRS in such a setting and the problem it is solving?
You have to use event sourcing as well, CQRS alone does not solve it.

First you have to decide what trade offs you want. Ideally you will not expose any events from a service, but that is not realistic. And since the two services have some degree of connection, let's make the trade off that we want to expose events of creation and deletion of customers so other systems can keep track of a current list of customers. We utilize an at-least-once delivery mechanism of events.

The orders service would subscribe to the two events, and maintain an internal list of currently active customers. You cannot create orders for customers that does not exists, and when a customer is deleted, you can do what you need to do with the orders.