4

In many examples I see that process managers are started when some event happened.

PlaceOrder -> Order Aggregate -> OrderPlaced -> Purchase Process Manager -> ...

But is it ok to start process manager by command?

Start Process -> Purchase Process Manger -> PlaceOrder -> Order Aggregate -> Order Placed -> Purchase Process Manger -> Charge credit card -> Payment Service -> ...

The problem I see with 1st approach is that when Order Aggregate published OrderPlaced event, the event could be lost and Purchase Process Manager would never start and I would have corrupted state in the system.

But in the second approach, I can retry PlaceOrder command if after some time Process Manger did not receive OrderPlaced event.

Teimuraz
  • 8,795
  • 5
  • 35
  • 62

1 Answers1

3

Yes, you can (re)start a Saga/Process manager manually. So you could combine the two methods.

... the event could be lost and Purchase Process Manager would never start and I would have corrupted state in the system.

This depends a lot on your architecture. You should have mechanisms in place that detect missed events and restart those Saga instances. This is doable because the Saga should be able to recover from any situation like missed events, reordered events, duplicate events and so on.

So, the Saga could have, beside the event handlers, a start method that you should be able to call anytime you want, without any negative consequences. If the Saga did not miss any events then it should do nothing. If you design your Aggregates with the idempotency in mind (as you should do!) then the Saga could be even simpler as it can resend the commands to the Aggregates and nothing (bad) would happen as they would hit the wall of idempotency.

Constantin Galbenu
  • 16,951
  • 3
  • 38
  • 54