How do you handle foreign-keys read-side failures in CQRS?

不羁岁月 提交于 2019-12-11 18:46:09

问题


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?


回答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.



来源:https://stackoverflow.com/questions/46878730/how-do-you-handle-foreign-keys-read-side-failures-in-cqrs

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!