I started learning about event sourcing and CQRS, but i didn't get something right.
If i have two services that both process the same event but in different ways (for example in booking sample, one service checks timeslot and another checks user credit), and one of them accepts the event but the other one doesn't, what should the system do?
How should the services know if other services have accepted or declined the event?
As far as I can see, each of these processors will calculate something regarding of the others result.
What you can do for example, is have another service, upstream, that would collect the results of those services, aggregate them, and when it has all the processing results for an event, it can do whatever you want. It knows that processes accepted, what processes didn't, and based on the logic of your system decide what to do.
Example:
BookingService
emits event:
{
"bookingId": 123,
"creditCardDetails": ...,
"timeslot": ...
}
CreditCardService
receives and emits:
{
"service": "CreditCardService",
"bookingId": 123,
"accepted": false,
}
TimeslotService
receives and emits:
{
"service": "TimeslotService",
"bookingId": 123,
"accepted": true,
}
And in the BookingProcessingAggregatorService
you get all the responses and react accordingly.
I've created an example using Typescript and RxJS to simulate a messaging system with events, Hope this is clear enough (it was nice to write it anyhow, so it might as well help you 😅):