pbolduc / FlakeGen

Flake ID Generators is a set of decentralized, k-ordered id generation services in C#.
Microsoft Public License
10 stars 1 forks source link

How do I generate worker id? #2

Open ondrix opened 4 years ago

ondrix commented 4 years ago

Hi, have you ever use it in any kind of scalable enviroment where many instances of the same service may be running? Any idea how to generate worker ID?

Thank you ;)

pbolduc commented 4 years ago

I guess it depends on a few factors like the kind of id you are generating. '

the int-64 generator

the GUID generator

How many instances are you running and how fast are you generating ids? Without coordination, you could generate the instance id using cryptographically strong random number generator like RNGCryptoServiceProvider. This approach with the int-64 could have a fairly high likely hood of collision. See Chance of selecting a duplicate?. Even if selecting the same instance id, you need to calculate the probability of two instances generating an id at the same millisecond. You could also base the id off of part of the computer's MAC address, IP address or process id.

The GUID generate has such big instance ids and time division, it would be highly improbable but not impossible to generate a conflict.

It also depends on what you are using the generated Id for. Is it going into a system that checks for duplication (like a database with unique constraint?) If so, can handle the conflict and regenerate a new id and try again.

If the probability is too high for your solution, you will need to use some kind of coordination service to hand out new instance ids. This could be redis, SQL Server Sequence, Zookeeper, etc. Depending on your architecture (short running functions vs long running/always on APIs) the overhead of this coordination may be significant or not.

Hopefully this helps.