We are faced with an issue related to concurrent requests/transactions that leads to a problem with consistency in our view model. Data in the aggregate is fine and we have all events saved, but Event Handlers don’t work like we’d expect.
- We send two commands of the same type to the same Aggregate
- Axon handles them sequentially, so we have remains consistent within the Aggregate
- First Event get’s processed by the Event Handler and data was committed
- Second event gets processed, but the Event Handler reads data without changes from step 3. Which means changes from step 3 get overwritten.
Normally this flow is fine, but in our case we have a non-idempotent operation. The order of the events doesn’t really matter, but we shouldn’t overwrite data.
We found that flow looks like:
- Send Command
- Start Hibernate transaction
- Start processing of the Command
- Apply the Event
- Handle the Event in the EventSourcingHandler
- End processing for the Command
- Start handling the Event in the Event Handler
- End handling the Event in the Event Handler
- Commit the Hibernate transaction
For two threads flow looks like
Looks like we have this problem, because of the length of the transaction and second transaction started before first was committed, it means second transaction doesn’t use the updated data. Could you please give us some comments about the behaviour? Currently we are not sure how to solve this issue properly.
I also attached some logs. Maybe they will be helpful.
Axon version: 3.4.1
Spring Boot: 2.0.3.RELEASE
DB: MySQL with isolation level READ_COMMITTED
Thanks in advance,
concurrency_logs.txt (158 KB)