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

NodeBits & StepBits change #26

Closed WeiWeiWesley closed 4 years ago

WeiWeiWesley commented 4 years ago

Would it possible that two different nodes created by two different NodeBits produce the same snowflakeID in the same time?

snowflake.NodeBits = 18
snowflake.StepBits = 7
a, _ := snowflake.NewNode(1)

snowflake.NodeBits = 14
snowflake.StepBits = 8
b, _ := snowflake.NewNode(2)

//a.Generate() == b.Generate() ?
bwmarrin commented 4 years ago

Without a bit of testing or a lot more thinking, I'm not sure 100% that you couldn't find some whacky mixture of node bits and step bits where you could create the same ID, though I think it's very unlikely. They would have to be made during the exact same millisecond and somehow have the node, and step bits be aligned too.

I would say, the uniqueness guarantee is for ID's created with the same NodeBits and StepBits settings.

You example above isn't valid though, the NodeBits + StepBits cannot be more than 22 total, that's the amount of space available to them.

WeiWeiWesley commented 4 years ago

Thanks your reply. In theory, It will be a tiny chance to get the same id when stepBits run out in the same millisecond. Yes, it's better to create snowflakeID with same NodeBits and StepBits settings at the same project or cluster.