Constraint violation due to duplicate sequence

Hi,

We have an application where we recently completed a major upgrade from Axon 2 to Axon 4. We have started seeing constraint violation errors thrown due to duplicate sequence.

We have few use cases where one event handler while in the process of handling an event makes an API call, API which in turn publishes an event on the same aggregate root, and then the handler itself tries to publish another event.
We use optimistic locking.
Our event store database is MySQL.
We do not use Axon server.

We are trying a few options such as retry when this exception occurs.

I wanted to ask if it’s ok to disable the unique constraint - uk_aggregate_seq
Our application does not really care if there are multiple events on the same aggregate root with the same sequence.

Please suggest.

Thanks
BG

I would honestly not recommend to remove the uniqueness constraint on the aggregate sequence number, @dbgsep09.
The combination of the aggregate identifier and the aggregate sequence number are key in ensuring Axon can Event Source your aggregates.
Thus, if you’d remove this constraint, and thus publish several events with the same sequence, you will end in a situation where you simply cannot load your aggregates anymore.

Instead, I’d have a recommendation how you can adjust your flow somewhat.
If I follow your explanation correctly, you’re invoking some third-party service from within your aggregate, right? Basing this assumption on this sentence:

I read this as if you’re constructing a cycle. Some operations causes you to end in an aggregate, while the aggregate is loaded you invoke a third party service, service circles back into the aggregate. You should be able to resolve the predicament if you would stop the cycle. Hence, some logic should move outside the scope of the aggregate, most likely in a separate event handling component.

Granted, I am not fully clear whether my assumption is correct. So, if the above doesn’t help at all, be sure to expand your scenario a bit for us. Then we should be able to figure out how to proceed for sure.

2 Likes