minos-python icon indicating copy to clipboard operation
minos-python copied to clipboard

`Aggregate.create` and `Aggregate.update` could reach concurrency issues related with validation

Open garciparedes opened this issue 3 years ago • 3 comments

Issue by garciparedes Wednesday Sep 22, 2021 at 15:51 GMT Originally opened as https://github.com/Clariteia/minos_microservice_aggregate/issues/6


Suppose and aggregate like Foo:

class Foo(Aggregate):
    bar: str

    async def validate(self) -> None:
        return not Foo.exists_bar(self.bar)

    @classmethod
    async def exists_bar(cls, bar: str) -> bool:
        try:
            await cls.find(Condition.EQUAL("bar", bar)).__anext__()
            return True
        except StopAsyncIteration:
            return False

If concurrent creation with same bar value is performed:

len([foo async for foo in Foo.find(Condition.EQUAL("bar", "example"))])  # 0

await gather(Foo.create("example"), Foo.create("example"))

len([foo async for foo in Foo.find(Condition.EQUAL("bar", "example"))])  # 2

Then, it could be possible that both validate calls are performed before storing any information on the database, so that when both of them call the find method, they do not obtain any instances, so both insertions seems fine, but when gather sentence finishes, more than one Foo instance with same "bar" value exists.

garciparedes avatar Jan 26 '22 10:01 garciparedes

Comment by garciparedes Wednesday Sep 22, 2021 at 16:54 GMT


Here is a Stack Overflow question discussing mostly the same trouble: https://stackoverflow.com/questions/31386244/cqrs-event-sourcing-check-username-is-unique-or-not-from-eventstore-while-sendin

garciparedes avatar Jan 26 '22 10:01 garciparedes

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Apr 27 '22 00:04 stale[bot]

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Aug 09 '22 08:08 stale[bot]