I am studying on the programming in DDD and event source.
I saw one example that when a domain logic was called (e.g. Order.placeOrder()
) it would publish an event (e.g. OrderPlaced
). And the event would be sent to MQ as the event store.
The domain logic (Order.placeOrder()
) should be an atomic API, and it should have @Transactional
annotation if using Spring for the transaction manager.
And now my question is:
How to make sure the DB change and event sending are within the same transaction? i.e. If there any error when committing data into DB, the event should never send to MQ.
I know that there is solution like XA or 2 phase commit to force the DB update and sending MQ messages within the same transaction. But seems it is not widely used nowadays.
If still using Spring @Transactional
annotation and no XA, is it possible that we do some logic after the transaction is committed successfully? What is the best practice to do that?
The following two properties must hold to have a reliable system:
There are the following possibilities to achieve this, all of which I've either used myself or seen being used in a project:
Use a messaging infrastructure that uses the same database as your application, so that a single transaction can be used. This solution is viable when a very simple messaging infrastructure suffices, and the team decides to build it themselves.
Use 2 phase commits. I don't have the impression that this is not used anymore, but maybe it's less talked about, because it isn't fancy technology...
Use some clever trickery to ensure both conditions hold. E.g. with what I call the chicken and egg solution:
Solution 3 requires careful design and review of the guarantees each part of the system makes in terms of failure behavior, so it is probably the most difficult one to get right. But it is also a very elegant solution, once it works.
By the way, I don't agree that Spring annotations should be added to domain objects, but rather to the respective app services. This only as a side note.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With