Distributed primary key SNOWFLAKE (snow algorithm)

SNOWFLAKE

Snowflake algorithm is distributed by Twitter released the primary key generation algorithm, it can not guarantee the repeatability of different processes of the primary key, and the orderly process of the same primary key.

In the same process, it is the first time bit by guarantee of non-repetition, if the time is the same as the sequence bit by guarantee. At the same time due to the time position is monotonically increasing, and each server is generally done if time synchronization, it generates a primary key in a distributed environment it can be considered general ordered, which ensures efficient insertion of index fields. For example the primary key Innodb MySQL storage engine.

Snow algorithm used to generate the primary key, portion 4 comprises a binary representation, from high to low sub-table is: 1bit sign bit, 41bit bit time stamp, and the bit 12bit 10bit worker process sequence number bits.

  • Sign bit (1bit)

Reserved sign bit, a zero constant.

  • Bit timestamp (41bit)

41 milliseconds timestamp 41 may be accommodated a power of 2, the number of milliseconds is used year: 365 * 24 * 60 * 60 * 1000. By calculation shows:

Math.pow(2, 41) / (365 * 24 * 60 * 60 * 1000L); 

The results approximately equal to 69.73 years. Snow algorithm ShardingSphere time era from the beginning at 0:01 on the November 2016 day be used to 2086, the vast majority believe that we can meet the requirements of the system.

  • Worker Process bits (10bit)

The logo is unique within the Java process, if it is a distributed application deployment should ensure that each worker process id is different. The default value is 0, by setting property.

  • SEQ ID bits (12bit)

This sequence is used to generate the same ID within a different milliseconds. If the number generated within the millisecond than 4096 (12 th power of 2), then the generator will continue to wait until the next generation milliseconds.

Clock Callback

Callback server clock can result in repeated sequences, the default primary key generator distributed callback number of milliseconds provides a maximum tolerable clock. If the clock number of milliseconds callback time exceeds a threshold maximum tolerable, given the program; falls within a tolerable range, the default primary key generator distributed clock synchronization wait time after the last time the generated key to continue the work. The default maximum number of tolerated clock callback milliseconds is 0, by setting property.

Detailed structure shown below snow primary key algorithm.

Snow algorithm

 

The advantages and disadvantages of this approach are:

advantage:

  • Milliseconds high, low self-energizing in sequence, the entire ID is incremented trend.
  • System does not rely on third-party databases, as a service deployment, greater stability, performance ID generated is very high.
  • Bit bit can be assigned according to their business characteristics, very flexible.

Disadvantages:

  • Strong dependence machine clock, the clock on the machine if the callback will result Fa is repeated or services are unavailable.

Guess you like

Origin www.cnblogs.com/yx88/p/11285120.html