Snowflake is a network service for generating unique ID numbers at high scale with some simple guarantees.
- developing a
c
library package. - quickly use as a CLI application.
- extension to
lua
. - extension to
go
.
There are three ways to use snowflake
:
In this way, you can use snowflake
as a static library in your project.
mkdir build && cd build
cmake ..
make snowflake_lib
Then you can copy the build/libsnowflake.a
to your project or install it to your system manually.
In this way, you can use snowflake
as a CLI application.
mkdir build && cd build
cmake ..
make snowflake_cli
Then you can copy the build/cmd/snowflake/snowflake
to your system path.
In this way, you can use snowflake
as a lua extension.
mkdir build && cd build
cmake ..
make snowflake_lua
Then you can copy the build/contrib/lua/snowflake.so
to your lua package.cpath
.
Each time you generate an ID, it works, like this.
- A
timestamp
with millisecond precision is stored using 41 bits of the ID. - Then the
machineID
is added in subsequent bits. - Then the
counter
is added, starting at 0 and incrementing for each ID generated in the same millisecond. If you generate enough IDs in the same millisecond that the sequence would roll over or overfill then the generate function will pause until the next millisecond.
- twitter-snowflake
- thanks to bwmarrin/snowflake