Giter Site home page Giter Site logo

snowflake's Introduction

snowflake

To efficiently generate 64-bit UUID for large-scale distributed application.

What is Snowflake?

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.

What is my Snowflake?

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

Requirements

The following minimum versions are required to build the library

  • CMake 3.5+
  • GCC 7.5+
  • Google Benchmark 1.5.5+

Installation

# 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

Benchmark

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

Tips

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

Contributing

Step 1

  • ๐Ÿด Fork this repo!

Step 2

  • ๐Ÿ”จ HACK AWAY!

Step 3

Support

License

  • This project is licensed under the MIT License - see the MIT license for details.

snowflake's People

Contributors

amazingchow avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.