A brief summary of the distributed ID

Source: Chenzhou Website Optimization

Id briefly summarize distributed implementation method popular  

Snow algorithm

snowflake twitter is an open source distributed ID generation algorithm.

The core idea is: a long distributed-type ID is a fixed number, a type of 8 bytes long, that is 64 bit, the original snowflake algorithm for bit allocation as shown below:

  • The first identification part is one bit, the most significant bit in java since long is the sign bit, 0 is a positive number, negative number is 1, generates ID for the generally positive, it is fixed to 0
  • Timestamp part represents 41bit, this is at the millisecond level, typically implemented on a current timestamp are not stored, but the difference in time stamps (current time - starting time fixed)
  • This allows the ID generated starting from a smaller value; timestamp 41 may be used 69 years, (1L << 41) / (1000L * 60 * 60 * 24 * 365) = 69 years
  • 10bit account id working machine, where more flexible, for example, the top 5 may be used as identification data center room, the room 5 as a standalone machine identification, node 1024 may be deployed
  • Part represents the serial number 12bit, support the same node may generate the same ID 4096 ms

snowflake algorithms require manual for each machine to specify a machine id, if a lot of machine or machine extension, one by one configuration is certainly not realistic, and similar containers docker popular, making this machine id has not narrowly remain in the "physical" level on, it should be expanded to the current machine id id "instance", such as Baidu open source-based uid-generator snowflake algorithm, in each instance of the application starts, insert a record to the database and return to the so-called machine id. similar the US group also open source leaf.

Based Middleware

Distributed Distributed Lock ID and a number of similar, generally also depend mysql, redis, zk middleware, wherein mysql based auto_increment, redis based incr, zk ordered based node. 

Distributed id required key value kept gradients, so in order to improve performance, usually a "pre-build" strategy, which once generates N id number segment cached locally, there is another benefit of doing so is that even if the middleware down for a little while too little effect.

In addition, if the middleware deployment architecture is no center, such as two master, then in order to prevent conflict, the general is not the same but id initial "step" the same tactics, such as two mysql initial id of 1 and 2, step length is 2, each node id as 1,3,5 ...; 2,4,6 ... will not conflict.

Guess you like

Origin www.cnblogs.com/1994july/p/12078750.html