To efficiently generate 64-bit UUID for large-scale distributed application.
The snowflake UUID is comprised of
+--------+-------------------+--------------+-------------------+
| sign | delta timestamp | machine id | sequence number |
+--------+-------------------+--------------+-------------------+
1bit 41bits 10bits 12bits
- 1-bit sign flag
Always be zero.
- 41-bits epoch timestamp in millisecond precision
Not current timestamp, but the delta timestamp, so we can use it for a long long long time.
- 10-bits machine id
The snowflake UUID service can be deployed on most up to 1024 nodes.
- 12-bits sequence number
Counter within milliseconds, the 12-bits can support most up to 4096 sequence numbers per (node + millisecond).
The UUIDs are increased in order by timestamp.
I provide a grpc server to serve the Snowflake UUID generating which [email protected]:18888.
You can use grpcurl to test it.
grpcurl -plaintext -d '{"machine_id": 8}' 0.0.0.0:18888 photon_dance_snowflake_service.PhotonDanceSnowflakeService/GetUUID
The following minimum versions are required to build the library
- CMake 3.5+
- GCC 7.5+
- Google Benchmark 1.5.5+
# Check out the library.
$ git clone https://github.com/amazingchow/snowflake.git
# Go to the library root directory.
$ cd snowflake
# Make a build directory to place the build output.
$ cmake -E make_directory "build"
# Generate build system files with cmake.
$ cmake -E chdir "build" cmake -DCMAKE_BUILD_TYPE=Release ../
# Build the library.
$ cmake --build "build" --config Release
# Install the library globally.
$ sudo cmake --build "build" --config Release --target install
As google benchmark framework shows, we can generate 4098 UUIDs per millisecond.
Run on (12 X 4304.79 MHz CPU s)
CPU Caches:
L1 Data 32 KiB (x6)
L1 Instruction 32 KiB (x6)
L2 Unified 256 KiB (x6)
L3 Unified 12288 KiB (x1)
Load Average: 1.48, 1.36, 1.25
-----------------------------------------------------------------------------------------------
Benchmark Time CPU Iterations
-----------------------------------------------------------------------------------------------
BM_NextUUID/iterations:100000000/real_time/threads:4 61.0 ns 244 ns 400000000
Before execute photon_dance_snowflake_benchmark_tester, please record current CPU governor, for instance, current CPU governor is powersave
.
cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor
You better set CPU governor mode as performance
.
sudo cpupower frequency-set --governor performance
If you meet the "cpupower: command not found", just install the linux-tools
sudo apt-get install -y linux-tools-$(uname -r)
After benchmark, you better set CPU governor mode back to powersave
sudo cpupower frequency-set --governor powersave
- ๐ด Fork this repo!
- ๐จ HACK AWAY!
- ๐ Create a new PR using https://github.com/amazingchow/snowflake/compare!
- Reach out to me at [email protected].
- This project is licensed under the MIT License - see the MIT license for details.