Several ways to generate globally unique id's

Several ways to generate globally unique id of:

1, uuid generating a globally unique id, simple and crude generation mode, generated locally, there is no network overhead, high efficiency; disadvantages longer length, there is no incremental trend, easy maintenance, token used for generating the token.

2, MySQL comes increment generates id, Oracle can generate a sequence id, but in the database cluster environment, poor scalability.

3, based on the characteristics of single-threaded redis generating globally unique id, redis high performance.

Id is generated for the current date (yyMMddHHmmss) +6 bits (from 000000 to start up less than the median 0)

20200206164329000001

4, the algorithm based on snow snowflake generate a global id, generated locally, there is no network overhead, high efficiency

principle

Twitter snowflakes algorithm SnowFlake.

ID SnowFlake algorithm produces a 64-bit integer, the following structure (with each part "-" signs):

0 - 0000000000 0000000000 0000000000 0000000000 0 - 00000 - 00000 - 000000000000

An identification section, in java since long in the most significant bit is a sign bit, 0 is a positive number, negative number is 1, generates ID for the generally positive, it is 0;

Stamp portion 41, 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 constant),

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;

10 node section, Twitter implemented prior to use as a data center identifies five, after five as machine identification, node 1024 may be deployed;

12 Serial number part, support the same node may generate the same ID 4096 ms;

SnowFlake algorithm generated ID is substantially increasing in time, when used in a distributed system, the data center identifier and the note identifier must be unique machinery,

This ensures that each node in the generated ID is unique. Perhaps we do not necessarily need to use 5 as above, as the data center identification, 5 as the machine identification, according to the needs of our business, flexible distribution node part, such as: data center if needed, can use all 10 as machine identification; if no more data centers, only three can be used as a data center, 7 as machine identification.

Snowflake ID whole generated in a time increment sort, and do not generate collisions ID (as distinguished by the datacenter and workerId) throughout the distributed system, and high efficiency.

5, zookeeper id generated by creating a global order of nodes in a highly concurrent scenario, performance is not good. (Not yet studied)

 

Please refer to redis

https://github.com/sentianhui/idglobal.git

Guess you like

Origin www.cnblogs.com/ts-sd/p/12292640.html