preface

For primary key generation, I believe that contact with the database of children’s shoes are not strange. Probably for most children, primary key generation remains autogrowth. Well, do you know its advantages and disadvantages

implementation

Since the growth

The self-growth of MySQL is basically no problem for the early systems, but with the explosion of data volume and the necessity of distributed system, self-growth has long been unable to meet the current needs.

advantages

  1. Relying on the database layer reduces the amount of code in the service layer.

disadvantages

  1. forintIs incremented by, in the case where the first digit is a sign number, of length2^31 - 1More than 4 billion. Of course, the general level of the single table, has done the operation of the table.
  2. Table merge: In some cases, multiple tables in a distributed system may be merged. In this case, the primary key may be duplicated. In this case, you need to modify the primary key based on services. So that’s not satisfying the uniqueness of the primary key.
  3. After data is attacked, some information may be leaked due to the nature of primary key increment.

Redis Incr

Use Redis as a centralized primary key generator. Due to the Redis IO multiplexing model, as well as the atomicity of single thread operation, the primary key uniqueness can be achieved under the premise of ensuring speed. While redis is a 64-bit signed number, it is unlikely that any data in a single table will reach this magnitude.

advantages

  1. Uniqueness.
  2. The order.

disadvantages

  1. Once Redis goes down, all writes to the database become invalid. Therefore, Redis nodes need to be maintained while ensuring high availability.
  2. Third point above.

UUID

The UUID approach is likely to be used more often than not, but it has the obvious advantage of ensuring that the generated primary key is unique.

advantages

  1. uniqueness

disadvantages

  1. UUID takes up more space than Int.
  2. In order to ensure the speed of data query, the primary key index adopts the data structure of tree, such as B tree, B+ tree. In this case, the sequence of primary keys generated by the UUID cannot be guaranteed, and each primary key generated may cause the nodes indexed by the primary key to change significantly. (Reference balanced binary tree)

Snowflakes algorithm

From the examples above, you have seen several necessary conditions for primary key generation.

  1. Uniqueness.
  2. Orderly field
  3. There is no limit to length.

advantages

Twitter’s snowflake algorithm ensures all three.

0-0000000000 0000000000 0000000000 0-00000-00000-000000000000 the first part is the symbol bit 0 or 1 and the second part is the 41-bit timestamp difference value, which ensures the order to some extent. Of course this 41 will last 69 years... The third part is the identification of ten parts, which can be divided according to business and node identification. The fourth part of the 12-bit serial number part, support the same node in the same millisecond can generate 4096 ID;Copy the code

Through the above four sections, the above three conditions have been met

disadvantages

  1. It is necessary to implement the generation method and ensure the generation efficiency at the same time.

At the end

For this article, I was asked the same question in my interview. At the same time for the snowflake algorithm is the end of the interview, only to understand, reference link: fierce poke me. I hope this article will be helpful