0

You have table A and table B in your SQL data model, with table A having a foreign key towards table B. You now want to migrate your CRUD architecture to CQRS. Imagine the following scenario:

  • You receive a command to persist a new entry of A
  • You write to your append log and process it updating your state
  • The read side picks it up and fail to insert in the database

In this situation, your write side and read side will never reach consistency. How do you handle read-side constraints in CQRS? Should validation occurs in the write side instead?

Edmondo
  • 19,559
  • 13
  • 62
  • 115

1 Answers1

1

I believe you can get some inspiration from this:

How to handle set based consistency validation in CQRS?

The short of it is, the new A aggregate needs to be created by another aggregate, let's say C (creator). In this case C holds a list of all A's IDs, so that the check can be done at the aggregate level instead of the view model.

If your question is more generic and you ask "how do you handle view model update failures", then it really depends. It could be a design issue (e.g. incorrect sequence of events, lack of pre-conditions or invariant checks), a bug, or else. What I normally do is that I lock those entities and send them to a list of entities requiring manual intervention, if there is no automatic remedial.

Alessandro Santini
  • 1,943
  • 13
  • 17