Several generation schemes of user UID

Time:2021-7-28

The purpose of this paper is to introduce the popular uid generation methods, advantages and disadvantages in the market, and help readers choose the appropriate generation scheme according to their own product type and user scale.

What is a uid?

UIDIt is the unique identification of users in a system,

Uid features: uniqueness, public broadcasting, possible value, etc.

Uniqueness

adoptUIDIt can be quickly mapped to a specific unique user, similar to hash and short URL mapping.

Public broadcasting

UIDIt can form a corresponding relationship with the user’s account. For some systems with private content such as mobile phone number and email as login account, if you want to add transfer business, enter the other party’s uid to protect privacy.

Possible value

Similar QQ number, short ID of station B and microblog ID can have some value.

Popular generation methods

  1. Random generation common duplicate checking mode
  2. Classic table ID auto increment mode
  3. No. 1 pool mode
  4. Random generation – duplicate checking mode – addition method
  5. Class snowflake pattern
  6. UUID mode

Random generation common duplicate checking mode

Use the rand function to randomly generate the result, and then check the duplicate in the user table. If it is not repeated, it will be used as the user’s uid. If it is repeated, it will continue to RAND until it is not repeated.

Advantages: fast generation speed, simple logic, and the format of generation number segment can be controlled by filter.
Disadvantages: when the total number of users increases, the repetition rate will increase.
Applicable: the total number of users will not be very high, and there are no requirements for beautiful numbers.

Classic table ID auto increment

Set the ID of the user table to auto_ Increment, the ID will be automatically generated by inserting, and the primary key ID of the table will be used as the uid

Advantages: no active management, automatic generation and no duplication.
Disadvantages: it is easy to expose the real number of users of the system, which is not suitable for commercial companies that need good data.
Applicable: ordinary community, blog content and other systems that do not pay attention to uid mode.

No. 1 pool mode

Generate a batch of UIDs, store them in the number pool, register one and take one away.

Advantages: if the control of the beautiful number is accurate and the number pool is properly controlled, there will be no repetition.
Disadvantages: the stability of the number pool service is very high, and the increase and deletion of data in the number pool need to be actively managed, otherwise duplication will occur.
Applicable: it has strict requirements on pretty numbers, and is applicable to the player community with general level sense of honor and high sense of honor.

Random generation – duplicate checking mode – addition method

The addition duplicate checking method is an upgrade of the ordinary duplicate checking method. When a duplicate number is encountered, a random number is added to the tail of the number. If it is repeated, it will continue to increase until it is not repeated.

Advantages: compared with the common duplicate checking method, the number of re acquisition after repetition can be reduced
Disadvantages: as the number of users increases, the re acquisition rate after repetition will also encounter a bottleneck.
Applicable: the same as the common duplicate checking mode.

Class snowflake pattern

SnowflakeIt is a classic number segment generation algorithm. At the same time, there are a large number of xxxflake algorithms on the market. It is generally used as order number. Mainly aboutSnowflakePrinciple of

41bit is used as the number of milliseconds, 10bit is used as the machine ID (5 bits are the data center and 5 bits are the machine ID), 12bit is used as the serial number within milliseconds, and finally there is a symbol bit, which is always 0

Advantages: no active management is required to ensure the anti duplication, and the bit can be adjusted according to the service ratio.
Disadvantages: the generated data result is relatively long, and the index needs to be actively optimized.
Applicable: there is no requirement for uid number

UUID mode

UUIDIt is an international standard algorithm, which will not be described in detail. Its advantages and disadvantages are consistent with class snowflake

Advantages: weight prevention can be guaranteed without active management.
Disadvantages: the generated data result is relatively long, and the index needs to be actively optimized.
Applicable: there is no requirement for uid number

summary

Generally, for systems with an estimated number of users less than one million, set the uid to 10 bits and use the random production ordinary duplicate check mode. Basically, duplicate checking will not lose too much performance. You can also filter out beautiful numbers according to the filter, which can basically solve most business needs.

For more than one million users, the most important thing is to pay attention to the business’s dependence on uid and the demand for beautiful numbers, and select the appropriate scheme.

Recommended Today

Implementation example of go operation etcd

etcdIt is an open-source, distributed key value pair data storage system, which provides shared configuration, service registration and discovery. This paper mainly introduces the installation and use of etcd. Etcdetcd introduction etcdIt is an open source and highly available distributed key value storage system developed with go language, which can be used to configure sharing […]