How to fix bugs in command handler with compromized events

I’m starting with Axon and CQRS and I am wondering in a situation where you find to have a bug in a command handler how do you fix it?

Example: classic banking application

CommandHandler (pseudo code)
on (withdrawCommand) -> {
// bug: business logic is missing (if withdrawAmount >= balanceAmount)
apply (eventWithdrawed())

In this situation I find eventWithdrawed saved even when the balance is not enough.
Doing the fix by adding business logic to the command handler, how could I reprocess the events?

Hello and welcome to the community Pietro!

That is a question I have also asked myself when I first started dealing with ES. The answer I came to actually has noting to do with the framework (in fact not even with programming as whole).

Think of your example this way. It’s not an application but an actual bank. It’s not a CommandHandler but actual employee. It’s not a software bug, it’s that the bank didn’t properly train the employee so he/she was allowing customers to withdraw more than they were allowed. This has been going on for some time before the bank discovered it. How would the bank fix it?

It’s certainly not possible to go back in time and enforce the rules. And even if was, that could alter the whole more than just those transaction. For example a customer who was wrongly allowed to withdraw over the limit may have used that to invest it and then deposited the earnings or paid off a credit or mortgage or … The chain of events that may have occurred “due to the bug” (and which would need to be undone) can be so large that going back in time and fixing it could result in current reality changing beyond recognition.

So the solution in the software case is the same one we apply in the real life. We acknowledge the fact something wrong has happened. We ensure it does not happen from now on. Then we attempt to compensate for what has been done wrong. Since we have the full record (event log) we can easily determine what needs compensation. It’s then a business decision how to compensate (send a note to the customer, automatically deduct from current balance, debt forgiveness, … )

Trust me, I know how this answer sounds to someone who is looking for a proper method in the framework to call. I’ve been there myself. But one thing I’ve realised over time is that often stepping away from the software and looking for solution in the actual problem space can offer much better options.


I would say the account will be handed over to the accountmanager who problably will call the customer and make an arrangement how the issue is will be resolved.

[edit] Just noticed, I somehow read the answer as the question ;). Guess I worked enough for today :).