Issue with "global" validation

Hello everybody!

Lets assume that I have a system with a Car aggregate, and there is a rule that says that a car cannot have the same color of an existing car. That way, in my view database table, I have an unique constraint validation in the color column. The question is: when I am validating a CreateCarCommand, what is the proper way of validate if the color of the new car do not exists in the system? If I use the query gateway at the command handler to verify if there is some car in the database with that color, I imagine that I can have some “parallel” issue, when two commands to create cars with the same color are sent, one right after the other, and before que Event Handler handle that event to create the first car one in the database, the validation of the second command occurs, and do not say that the color existis, just because the first one was not created in the database yet. It do not look the proper way to solve it, I think…

Hi Rafael,

What you’re dealing with here, is set validation, which is explained quite nicely by Daniel Whittaker in this post.
The process you’re suggesting is likely the approach I’d take to solving the issue at hand.
I’d have a very small/concise Query Model dedicated for this task, which is checked prior to issuing the command.

Nonetheless, you will still be dealing with Eventual Consistency on that specific Query Model.
Thus, the situation you describe, the ‘parallel issue’, can occur.

To resolve this I’d like take a pragmatic approach.

I’d have the Event Handling Component which updates this Query Model send out some compensating action to undo the latter of the two commands.
It should be tasked with this operation, as it knows best when the Query Model has a conflicting color in it’s database.
A ‘compensating action’ in this notion is just another command, one which will likely delete this Aggregate (which from Axon’s perspective can be done by calling AggregateLifecycle.markDeleted()`).

This is my 2 cents to the situation, hope it helps!


Hi Steven, thanks for you answer, it really helps me!! I had not thought of doing this check in Event Handler, which is a good idea. In this case, there is any way to notify the user the this color already exists, synchronously maybe?

Hi Rafael,

That’s great to hear, thanks for your response! :slight_smile:

Ideally, the front-end would be notified asynchronously if such a failed action.
However, bridging the Eventual Consistency over into the front-end is something which isn’t as straightforward in most situations.
To that end, what you could do is ‘spoof’ a synchronous connection, by leveraging the subscription query.
This repository by Frans van Buul provides a snippet on how to do this, which I assume should give you sufficient guidance on how to approach this.

Hope this helps you out Rafael!


Thanks again Steven! Really nice that repository showing subscriptions queries, I will read about it and use that approach.