Custom sequence

Hello everyone.

I have an app that created payment sequence based on postgres, via

  1. create sequence if not exists sequence_name;
  2. select nextval (‘sequence_name’)

I need this to have custom sequence logic, for example
organization 1
payment in store1 // sequence_number #1
payment in store2 // sequence_number #2
organization 2
payment in store1 // sequence_number #1

So I create sequence based on organization_id

I do this in PaymentAggregate, so this covers multithreading usage

The problem is when PaymentAggregate handles CreatePaymentCommand, in selects new sequence_number, but if later CommandHandler fails to successfully execute, Payment is not created, but sequence_number has already been choosen, and never will be used. And I have a hole in ordering payments.

How to get sequence_number in other way ? May be Axon provide some out-of-box implementation for this ?

Is there a chance you can perhaps tie the creation of the sequence number to a transaction?
Under the hood, Axon uses the UnitOfWork to manage the lifecycle of every message.

To let things happen in the correct order, operations are attached to the lifecycles of the UnitOfWork.
Things like event publication too.
The UnitOfWork can also roll back, thus disregarding the event publication staged in one of its phases.

Perhaps you can attach your sequence creation to this approach to benefit from this logic.
The Reference Guide has this to say on the UnitOfWork, by the way.

As you might’ve noticed, Axon doesn’t have an out-of-the-box implementation for this.
But perhaps it does have handles you can utilize. :wink: