Error during event handler - query side

Hi,

I would like to know how to handle the situations of error during event handler (which manage read side).
For example, the case where you check the uniqueness of user name. We suppose that there was no check on client side before saving aggregate. After aggregate commited, an event handler write to
the read-side in a mongodb collection (for example) the object, but an unique index exists on this collection, so mongodb throws an error. When error is thrown, we must launch a compensating command in order to cancel the previous command,
what are the best practices for this situation ?

Note : we suppose that environnement does not support transaction : Event Sourcing Aggregate and read side collections are both stored with MongoDB.

Thanks.

Baptiste.

Hi, nobody has an advice?

Hi,

this is really a dicussion you need to have with you product owner. What do you want to do when a user account was found to be ‘illegal’? One option is to block the account using a command. Given the chance of somseone creating a duplicate account is very, very small, that’s the one I’d try to go for.

Cheers,

Allard

Hi Allard,

In this case, do you think that it is the role of this event handler (which manage read side) to launch the command to block the account, or there is a better solution to do that ?

It is perfectly fine for an event handler to do that. In the end, that’s the handler that is able to discover duplicates.

Cheers,

Allard

Is the event handler is thread-safe ?
Because, we cannot be sure that just after we test duplicate in event handler, an other thread add a user with the same user name (yes, there is very small chance to happen, but …)

If you use a database constraint, that shouldn’t be any problem.

Cheers,

Allard

So, the better thing to do is to have a unique constraint and catch DuplicateKeyIntegrityException in order to launch the compensating command.
If i have no unique constraint, a simple query to test duplicate is not safe ?

The concurrency characteristics of your handlers are defined by the cluster implementation you use. By default, that’s the SimpleCluster. That just handles events in the thread that calls them.
If you can’t use constraints, you’ll have to resort to other concurrency controlling mechanisms, or a concurrent collection to store information.

Cheers,

Allard

Ok thanks for this clarification.