Handling commands in a guaranteed order

Hi all,

It’s my first time implementing an application using CQRS and/or Axon so bear with me if this question is either misguided or just plain stupid ;-).

The system we’re building communicates with multiple other external systems. These systems use a certain protocol to communicate with our system but we do not want to tightly couple our commands and domain layer to the semantics of this protocol (this is a must-have requirement). Because of this we’re running into certain situations in which a single protocol message translates into multiple commands. For the most part, we don’t need to handle these commands in any specific order but in some cases a single message leads to the creation of an aggregate and the following commands need to be handled after the command which creates the aggregate (naturally).

For example:

  • A protocol message is received for a non-existent aggregate.
  • This should cause to the following commands to be dispatched:
  • CreateAggregateCommand
  • DoACommand
  • DoBCommand
  • DoCCommand- The CreateAggregateCommand must be handled before any of the other commands.
  • The order in which DoACommand, DoBCommand, and DoCCommand are handled are not important. (To me, this sounds like a saga…)
    The added complexity is that I’d like to use the CreateAggregateCommand in other scenarios which (I think) makes using a saga for this issue a no-go. Maybe we shouldn’t reuse this specific command and create a separate command which better describes the intent of the action though.

Any help or guidance on this issue would be appreciated!

Regards,

Dennis Laumen

Hi Dennis,

if the commands that need to be executed in guaranteed order come from the same producer, the easiest way to guarantee ordering is to send each command in the “onSuccess” callback of the first one.
If you use the CommandGateway, you can do this by adding a parameter of type “CommandCallback”.

Saga should typically only be included if there are multiple different aggregates involved that need to react on each others actions. Exceptions are cases where aggregate’s have time based actions. As far as I understand your description, that’s not the case here.

Hope this helps.
Cheers,

Allard

Hi Allard,

Thanks for your response!

if the commands that need to be executed in guaranteed order come from the same producer, the easiest way to guarantee ordering is to send each command in the "onSuccess" callback of the first one.
If you use the CommandGateway, you can do this by adding a parameter of type "CommandCallback".

Of course! I checked out the callbacks before (although I'm still using a plain ol' CommandBus for now) and was confused by the following line in the JavaDoc of CommandBus:

When the method returns, the only guarantee provided by the CommandBus implementation, is that the command has been successfully received.

This, of course, refers to the return of the method not the calling of the callback. Well, problem solved! :slight_smile:

Saga should typically only be included if there are multiple different aggregates involved that need to react on each others actions. Exceptions are cases where aggregate's have time based actions. As far as I understand your description, that's not the case here.

That's definitely not the case. Thanks for the clear explanation of sagas though!

Regards,

Dennis Laumen