bwmarrin / snowflake

A simple to use Go (golang) package to generate or parse Twitter snowflake IDs
BSD 2-Clause "Simplified" License
2.98k stars 371 forks source link

Generate unique node ID #28

Closed w4-hanggi closed 1 year ago

w4-hanggi commented 3 years ago

The node ID accepts an int between 0~1023.

Is there a way to generate distributed unique or sequential node ID?

Because it's only has 10 bits, it's difficult to generate unique node ID by machine ID or process ID.

dongshanlang commented 3 years ago

I'm facing this problem to, I think out three methods: 1.config it manually 2.use a third component, such as redis. 3.use sonyflake hope someone suggests a better way.

bwmarrin commented 3 years ago

You can configure the NodeBits to a larger value if you need more than 10 bits but that comes at a cost of less space for the sequence number and then you're not using the standard "Twitter Snowflake" format but that may not be an issue.

In order for this library to guarantee that it provides you a unique ID - you must provide it with a unique NodeID. Most methods I can think of to automatically generate a NodeID are not going to guarantee uniqueness or are far too large to be used in a snowflake. Because of that I'm leaving that out of the library.

You can absolutely write some clever code to generate NodeID's from host information or other methods that you feed into this library when you create the Node.

The best options are prob to configure the ID manually and make sure each instance has a unique number. If you're dynamically spawning nodes then using some microserver/redis that can hand out IDs and deal with making sure they're all unique would work pretty good.

Sonyflake may be an option but it still is limited to 16 bits for the MachineID and you may run into all the same problems there as well.