Parallel event handling by 2 services

Hello everyone!

I work on a project with microservices.
Imagine I have an ItemCreatedEvent which is handled by 2 services

  • in first service Item is saved to the view data base,
  • second service must do some work on it’s own model based on that item.

Supposed ItemCreatedEvent is handled in parallel by 2 services and when it comes to second service it’s not guaranteed that in first service item has been saved to view DB, because there can be some exceptions, etc.
And second service handles events from first service with subscribing mode. (we use RabbitMQ).
axon.eventhandling.processors.item.mode=subscribing

What is an optimal way for second service to know that the ItemCreatedEvent has been handled correctly (saved to DB in our case) by the first service?

I’m thinking about subscription query where 1st service emits an event about finishing its work correctly and 2nd service subscribes to that event.

I may be missing something here, but it sounds like you should have either two events or only one service in this case.

From what you describe, it seems like your second service depends on the outcome of the first service and not the event. If that is the case, then your first service should do the work and emit a separate event when the job is done. Then your second service should listen to those events, not the original ones.

Alternatively, if both services are too tightly coupled and always work together in a predefined way, you may be better off simply merging them into a single service.

It’s hard to point out the “optimal way” without knowing the actual domain and the details regarding the part(s) of it you are modeling. Generally speaking, though, if two or more services handle the same event, they should be completely independent of each other. Failure(s) in any of them should not prevent others from working and should not leave the system in an undesired state. If that is not the case, then you need to introduce something to manage the process. That could be done via

  • dedicated events/commands
  • Sagas
  • external process/workflow managers

You could also use queries as you suggest, but I’d advise against it. The reason is you are then reacting to a state change of a projection and not on an actual event. As the projection can change at any time, it will be problematic later on to reason about (debug) what has happened and based on what data your second service made a decision.

1 Like