I have a question about correct Entity ID generation while using CQRS pattern.
In my application I have Customer
entity. I use microservices and Actor
pattern for implementation: Azure Service Fabric Actor.
I have restriction - CustomerId
should be unique across all the system(thanks cap) and it should be 5 digit number.
Common approaches for ID generation I met before:
- Generate GUID in UI and pass already created GUID to API method.
Thats the approach I always used before, but I am pretty sure its not applicable in my current case since UI can not generate random 5 digit number which will be 100% unique.
- Command returns Id from API method call
Ok, this approach is applicable in my case. But I use Actor
pattern and all my actors live in different partitions on the server. That means creating unqiue ID is not a trivial task. This approach also violates CQRS pattern, but I think we can live with that since CQRS is not a silver bullet.
This goal can be achieved by using some proxy
layer - for example new Actor - CustomersListActor
, which implementation will contain list of all actors in the system with related Ids. That will help me generate new unique CustomerID
.
But this approach have a big drawback - I have a bottleneck - CustomersListActor
. So it kills all the benefits of microservices. Every POST call will have to pass through CustomersListActor
. And its not multithreaded.
- In a situation of DB-persisted systems there is a practice when we reserve batch of IDs from the DB and store them somewhere in memory. The purpose is to provide these reserved IDs while GetId request from client.
With microservices this approach looks like the previous one. We still need to take care of some Actor who works using one thread and stores all the info about existing Ids(or at least the MAX id).
I also read about approach of deviding Id to 2 pieces. GUID for storing data in persistance layer and Int ID for human-readable ID. But I dont like this approach beacuse I store two different unique IDs for one Entity. And this approach does not save me from creating same Actor with list of other actors.
So my main question is: What approach is widely used while working with microservices in case of CQRS and your EntityID is not GUID?
Aucun commentaire:
Enregistrer un commentaire