Tenant Specific Id for Multi Tenant Application
Question
Application Type
Reactive

I have a multi tenant application with "Id" as auto incremental number. 

But we want to have another field "CustomizeId" which will be tenant specific. So that every tenant has their own running number. But we keep having duplicate "CustomizeId", when multiple users create it at the same time. 

This is how the flow work. 

> Aggregate to get max CustomizeID

> Assign to local variable

> Call "CreateCase" and pass in local variable as "CustomizeId"

Have anyone done something similar before?

Thanks,

Constance

mvp_badge
MVP
Solution

Hi Constance,

The typical solution is to have a special Entity that holds a single record, which is the sequence number (or next sequence number) to use for a specific tenant (so make the Entity multi-tenant). Then create an Action GetNextSequenceNumber() or whatever you want to name it, and inside that action use a GetForUpdate entity action, which blocks any other attempts to access, then increase the sequence number and save it. You can then return the unique number.

To bootstrap the entity, you can create a Timer that runs On Publish, that checks whether there's a record, and if not creates it.

Of course, if you have multiple sequences like this, you can have more records in the Entity, each with some specific key.

Hi Kilian Hekhuis,

I have update my code to this.  But there are still chance that duplicate could happen when there's no record for that specific tenant. I will change another module to automatically create the record when a new tenant is create and I guess that should solve the duplicate issue?


Regards,

Constance

mvp_badge
MVP

Hi Constance,

As I wrote, "To bootstrap the entity, you can create a Timer that runs On Publish, that checks whether there's a record, and if not creates it." If you create a new tenant, such timers will also run, so that should cover your use case.

Community GuidelinesBe kind and respectful, give credit to the original source of content, and search for duplicates before posting.