Closed JustSteveKing closed 2 years ago
Repo, if required: https://github.com/JustSteveKing/phponline.dev-new
Here's a whole discussion on concurrency: https://github.com/spatie/laravel-event-sourcing/discussions/214
There are two ways to solve your problem: use the command bus with the retry middleware (mentioned in that discussion), or manually refresh the ARs on places where these errors arise.
PS: please reopen this issue if the linked discussion doesn't solve the problem!
@brendt is this part in the v7 documentation at all? It sounds like it would solve the issue - but wouldn't mind reading your recommended approach š
I don't think it isā¦ I added it as a kind of experimental feature, but maybe it's time to add it to the docs now.
Have you tried it out?
Not yet @brendt - if it is in the laravel event sourcing couese I could probably figure it out though
@JustSteveKing I'm wondering if you've been able to follow the discussion on #214 and resolved it. I haven't been able to follow through and implement the suggestions but we have worked out a way to make concurrent persists work for us in the mean time. Solution seems hacky and thus haven't bothered to create a PR to this package to have it added.
Found ourselves in a situation where you need the aggregate to persist no matter what. When dealing with transactions (payments), after processing all validation rules and ensuring this is a valid request that needs to be processed by an external microservice, it is bad to throw an error such as CouldNotPersistAggregate
. It is not really the fault of the merchant/requestor making the request to the gateway that another process has already persisted the aggregate to a higher version.
Find below the link to a sample repository created, (on a separate branch), to enable concurrent persists. I am really hoping it helps someone and or someone improves upon it in a way that would also help us very much.
https://github.com/anabeto93/spatie-es-auto-discovery/pull/new/feature/allow-concurrent-persists
Helper function here is allowConcurrentPersists()
and the tests in AllowsConcurrentPersistsTest
might help explain the challenge.
When stress testing the current implementation of the gateway with event-sourcing using locustio
, the majority of the errors that occur apart from http 429
mostly has to do with CouldNotPersistAggregate
. Still playing around with $tries
value to settle between a higher value (slower response times) or lower value (faster response times but with more CouldNotPersistAggregate
errors).
Hey @anabeto93 I haven't tried this recently if I am honest, however I am about to start another event sourcing project soon - so will be able to see where this is in terms of ability to achieve.
I had issues following the discussion if I am honest, as the docs did not have anything about actually using the command bus. I will dig into this and see what I can figure out
My first time using v7 of the package, and had some issues with aggregates last night.
Using Laravel 9.2.0 PHP 8.1
Aggregate:
Event:
Service Provider:
Projector:
The event is persisted in the database during testing with no errors, but when I interact with my code from the web UI - I get this issue. The aggregate is being triggered by a Livewire component that uses a service class to trigger the aggregate:
Livewire Component:
Service class:
As you can see the service class tries to persist the aggregate, and this is where it fails. In my test code all I am doing is making sure that the event is stored: