cph-cachet / carp.core-kotlin

Infrastructure-agnostic framework for distributed data collection.
https://carp.cachet.dk/core/
MIT License
21 stars 3 forks source link

Implement unit of work pattern for repositories #417

Open Whathecode opened 2 years ago

Whathecode commented 2 years ago

To safeguard against concurrent repository updates overwriting each other's changes, a version field was introduced in all snapshots that are expected to be updated atomically in repository updates.

With these modifications in place, infrastructures using CARP core can now verify on repository writes whether the version of the snapshots in the DB match those of the expected fromSnapshotVersion from the AR which is loaded in memory and on which edits were performed. If not, this means a concurrent incoming requests manipulated the same AR, and an exception can be thrown. This exception can be handled uniformly by retrying the whole request.

Could even more guidance be given, and potentially specific database technologies for transaction management be used, by implementing a unit of work pattern on repository interfaces?

For example, MongoDB has read isolation, which if units of work get communicated to repository calls, could be used to achieve the same as verifying the version field.

Whathecode commented 9 months ago

This may also be important to safeguard that a repository update and matching domain event which is published occur as one transaction.

Currently, if a repository call succeeds, but an event bus call fails before it reaches a messaging bus, the system will end up in an inconsistent state.

https://blog.codingmilitia.com/2020/04/13/aspnet-040-from-zero-to-overkill-event-driven-integration-transactional-outbox-pattern/