How to ensure the order of messages in the CQRS template

I distorted the Greg Young application example a bit and stumbled upon a problem in a multi-threaded environment that the order of messages on the bus might not be guaranteed or event processing might not end until the next arrival.

Because of this, an ItemCreated message may appear after an ItemChangedSomething message, or at least the first message will not be fully processed. This leads to problems in the "reading side" because I want to update data that is not yet available.

How to get around this? (Assuming CQRS is appropriate for the case of creating a domain.)

Is there a need to create a saga or is there a way to do this?

+4
source share
1 answer

You must choose a messaging infrastructure that ensures event delivery is in order for each consumer, even if multiple threads are delivered in parallel with different consumers. Ie, if you feed events in the order on the sending side, consumers will receive them in order.

Then, there are two main approaches to handling this situation:

  • : CQRS . , . . . , ( , ItemCreated ItemChanged ).

  • . ( ) /, . ItemCreated ItemChanged , , , (/ ) . , (, ), .

+3

Source: https://habr.com/ru/post/1618311/


All Articles