0

Trying to understand concurrency with CQRS, I came across that question: https://softwareengineering.stackexchange.com/a/349575/286881

But there is something I don't understand when it is said that:

it do so by appending the new events to the Event stream if and only if the version of the Aggregate is still the one that was when the Aggregate was loaded.

To check the current version of the Aggregate against the one I have, I first need to retrieve it. Then, if it matches, I can append the events to the Event Store. Maybe I'm missing something but should not these steps be done atomically? If so, how is it achieved?

4
  • 1
    Did you read the accepted answer in the question that you posted? The answer is in there. The thing to remember is that the persisted data isn't a conventional data model, the only persisted data at all is the event store and the Aggregate is created every time by looking at all of the events in the persistent store for that aggregate. So the only thing that needs to be atomic is updating the event store. Oct 26, 2017 at 16:47
  • Of course I did read the accepted answer, I’m very new to all this and should be missing something obvious but what if the command handler starts processing a command and gets interrupted just after having compared the versions (before writing to the events store) to process another withdraw command? No event is written to the store yet so the version has not changed and both withdraws may be accepted...
    – jroy
    Oct 26, 2017 at 17:39
  • No. Both withdrawals aren’t accepted because, until the event is persisted, the withdrawal hasn’t actually happened yet. As long as the event store is ACID compliant and the insert takes place inside a transaction that will rollback if there is a version number mismatch, you won’t have a double withdrawal. Oct 26, 2017 at 17:52
  • Ok this is the point I was missing: the event store is responsible for checking the version number, not the command handler. Thank you for clarifying.
    – jroy
    Oct 26, 2017 at 18:18

1 Answer 1

1

Quoting RibaldEddie's comment as this is the answer to my question:

As long as the event store is ACID compliant and the insert takes place inside a transaction that will rollback if there is a version number mismatch

1
  • The most scalable Event stores do not use transactions. For example, in my implementation on MongoDB, I insert a document with all the events that were generated by a command and we know that MongoDB has document level guaranties. This is called a commit. Feb 15, 2018 at 7:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.