jrnl · home about list

# Twitter Snowflake Format

Following scenario: We have 4 workers and they want to generate unique ids. Usually you'd need a central server that they get the ids from. That central server would, for example, just use the current time in ms plus a counter if we get multiple messages in the same ms. With snowflake format, we additionally add a worker id.

So let's say each worker has same time in ms. Due to the additional worker id, we already have 4 unique ids! If time in ms is different, we already have unique ids no matter what. And if we get multiple messages at the same time, we have our additional counter. If that counter runs out (it's usually rather small, like 4096), we simply wait 1 ms. But think about it, 4000 messages per millisecond?

So there you have it, distributed unique ids!

For a technical breakdown on which bits are used for what in the snowflake format, see the sources below.

https://discordapp.com/developers/docs/reference#snowflakes

Published on