> I like this post and yes, we can pre-validate user's email using eventually
> consistent store before sending a command.
> But I think the real question was how to validate a command when we are
> within an aggregate so the aggregate publish either UserRegistered or
> DuplicationFound event? Assuming this aggregate is implemented with ES.
A few things need to be looked at first.
1) How likely is this to happen?
2) What is the impact of it happening (a powerful question here is "if
this happened who would be the first person to notice and why".
Very often we spend too much time working with things from a systems
perspective when these types of risks are better mitigated through by
a business perspective. There are many heuristics that can be used to
lower the probability of something like this happening to a point that
it may happen once in the next five years in your busy system. One
question I like to ask is "Can we detect that this happened", if so
then often we can use a reactive instead of a preventative approach to
risk management.
That said.
You can have full consistency for such things on the command side
quite easily. Drop in an infrastructure service that inserts into a
table with a unique constraint as part of processing your command.
There are however trade offs with this. The largest is what happens if
that is down? Do you really want to reject the transaction?
There is another trick here for this particular problem, you can use
the natural key (user id) as the aggregate id in which case you will
be fine here as they will write back to the same event stream.
Also keep in mind that all of this is discussing a distributed
environment. If you have a single command processing server just
remember the last N users you created and check against it.
Cheers,
Greg