Closed hassanrazakhalid closed 6 months ago
How to set I
dGenerator(0)
for multiple instances
See the README:
The generator-id-part of the Id is the part that you 'configure'; it could correspond to a host, thread, datacenter or continent: it's up to you. However, the generator-id should be unique in the system: if you have several hosts or threads generating Id's, each host or thread should have it's own generator-id. This could be based on the hostname, a config-file value or even be retrieved from an coordinating service. Remember: a generator-id should be unique within the entire system to avoid collisions!
So first of all: You shouldn't set multiple instances to 0.
Assuming we shouldn't be using a unique value each time a node is spawned or destroyed.
That's exactly what you need to do; each node needs to get it's unique value; where you base that value on, and how you coordinate which node gets which value is up to you. As mentioned in the README, you could create a coordinating service that keeps track of which generator-id's are 'in use' and which are 'available' and ask that service for a generartor-id upon creation of the IdGen instance. If that is too much work, you could base the generator-id on something else like the node-id or something; just be careful that you don't create collisions in the generator-id's,
Also in the README:
This library provides a basis for Id generation; it does not provide a service for handing out these Id's nor does it provide generator-id ('worker-id') coordination.
Right so just to be clear. Suppose I have deployed application to Azure App Service 1st Instance -- Has GeneratorId 0
0 is saved in Database or in some Cache
Then load increase and Azure spawns 2 more Instances, now we have 1st Instance -- Still Has GeneratorId 0 2nd Instance -- Has GeneratorId 1 3rd Instance -- Has GeneratorId 2
Load reduces and Azure kills 1 instance 1st Instance -- Still Has GeneratorId 0 2nd Instance -- Still Has GeneratorId 1
Now Again if load increase and Azure spawns 1 instance 1st Instance -- Still Has GeneratorId 0 2nd Instance -- Still Has GeneratorId 1 3rd Instance -- Has GeneratorId 3
So basically using auto-increment for GeneratorId or if node restarts due to some issue, then same id will not be used but it will be auto-incremented. I was actually thinking of some way to reuse existing Id instead of needlessly auto-incrementing Generator Ids each time when there is scale up or system restarts due to issue.
I was actually thinking of some way to reuse existing Id instead of needlessly auto-incrementing Generator Ids
I would advice that, yes. You'll run out of available generator-id's eventually if you don't reuse them. That's why you should look into a way to derive a generator-id from a node id or something else that returns a deterministic id with no collisions. A coordination service (you could compare it to a DHCP server) handing out ID's is an option, but using the IP or hostname of a node could possibly also be an option to derive a generator-id from. It's very, very dependent on the specific environment and circumstances in which IdGen is used. Unfortunately, because of that, I can't give a "one size fits all" solution.
I am little confused that how can we use this for example in Azure App Service with Auto-scaling enabled. How to set
IdGenerator(0)
for multiple instances and use same value when new node is created or destroyed. Assuming we shouldn't be using a unique value each time a node is spawned or destroyed.