Talking about the generation scheme of global unique ID

  Business system IDs must have unique requirements, and on top of this, high availability and trend growth are required; there are four common methods to generate IDs

  1.UUID

  The Id is generated directly locally, no remote call is required, the latency is low, the scalability is good, and there is basically no performance upper limit; but there is no way to ensure the trend is increasing, the uuid is generally long, and it is identified by a string, which is used as a primary key index and query efficiency is low

  2. The database is incremented

  Using the existing functions of the database, it is easy to use, and can ensure uniqueness and incremental type; but the availability is difficult to guarantee. The database is generally a structure of one master and multiple slaves. If the main database hangs, it will not be able to play. The performance of the database determines the Id The generated performance can be increased by increasing the main database, avoiding single-point writing, setting different initial values ​​and the same growth step value for each database, ensuring availability, but losing the availability of absolute growth, but this problem is not big, but Every time an Id is generated, the database still needs to be accessed, and the bottleneck of the database still exists.

  3.Id generation service+sequence

  The primary key rule is the current number of milliseconds (long) + sequence; by creating a new database table, the fields contain the table name, initial value, and maximum value; each database table needs to configure a record in this table, and return it once in the service generated by Id Take the next batch of sequences and cache them in the database, so that you don’t have to access the database every time you generate an ID. The ID generation service can use a cluster. When the main service is down, other services can be on top. This switching process It is also transparent and can be done automatically, but the generated id may not be continuous, but it is not a big problem.

  4. Snowflake-like algorithm

  The snowflake algorithm is an open-source distributed ID generation algorithm of twitter. The core algorithm is a long-type ID. The first 41 bits are the current milliseconds, the middle 10 bits are the machine code, and the last 12 bits are used as the sequence. In theory, 400 bits can be generated in one second. 10,000 Ids, the performance is completely sufficient; put the number of milliseconds at a high level to ensure that the Ids generated by each server are in order, but it cannot guarantee that the Ids generated by different servers within the same millisecond are in order.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325036965&siteId=291194637