Should the command handler be idempotent ?

Hi,

Let’s say a process need to the interaction of two commands, if the 1st command get executed successfully, but the 2nd command failed and in some extreme condition , for example JVM crash or the network error, in these cases there is no chance to undo the 1st command . How can Axon work well after the system restart ? Should I keep the command idempotent so that the 1st command can be executed again ? If doing so, I should not throw any validation exception in the command handler (the validation could be like checking add something twice ) , so where should I put the validation ?

This maybe a general problem of CQRS, but should we handle properly ?

Regards,
Sean

Hi Sean,

My mind might serve me wrong, but throwing validation exceptions in your command handler does not necessarily mean your command handler cannot be idempotent, right?
Regardless, this is indeed not so much an Axon problem as just a thing to deal with when building applications like this.

Making your command handlers idempotent would be the ideal approach if you’d ask me.

At least in such a manner that retrying said command (automatically through the [RetryScheduler](https://github.com/AxonFramework/AxonFramework/blob/master/messaging/src/main/java/org/axonframework/commandhandling/gateway/RetryScheduler.java) for example) is safe to do.

How you would make the command handling idempotent is dependent on your domain of course.
You can find some thoughts on this on the DDD/CQRS forum as well.

Hope this helps Sean!

Cheers,
Steven