# Distributed ID generation algorithm: Snowflake ID

Snowflake ID algorithm is a distributed ID generation algorithm, which can generate unique, orderly and non repeated ID numbers, and is widely used in distributed systems. The generated ID number consists of 64 bit binary numbers and can be converted into a string representation of hexadecimal or hexadecimal.

The core idea of the Snowflake ID is to divide a 64 bit binary number into four parts, representing timestamp, data center ID, machine ID and serial number. Specifically, the length of each section is as follows:

  • Time stamp: occupying 42 bits, accurate to the millisecond level, can be used for 69 years.
  • The data center ID occupies 5 bits and is used to identify different data centers, with a maximum of 32 data centers.
  • Machine ID occupies 5 bits and is used to identify different machines, with a maximum of 32 machines.
  • Serial number: occupying 12 digits, used to represent different IDs generated within the same millisecond, and can generate up to 4096 serial numbers.

Therefore, a 64 bit binary number can be represented in the following format:

1bit 41bit 5bit 5bit 12bit
Symbol bit (not used) time stamp Data center machine ID serial number

The specific process of using the Snowflake ID to generate IDs is as follows:

  1. Obtain the current timestamp, accurate to the millisecond level.
  2. Generate a 10 bit binary number based on the given data center ID and machine ID.
  3. Move the timestamp to the left by 22 bits, the data center ID to the left by 17 bits, the machine ID to the left by 12 bits, and then use a bit or operator to combine them into a 64 bit binary number.
  4. If multiple IDs are generated within the same millisecond, a sequence number is used to distinguish them. The sequence number increments from 0, and a maximum of 4096 sequence numbers can be generated.
  5. Convert the generated 64 bit binary number into a hexadecimal or hexadecimal string, which is the final ID number.

The advantage of the snowflake algorithm is that the generated ID numbers are ordered, unique, non repetitive, and support high concurrency, which can be widely applied in distributed systems. However, it also has some drawbacks, such as time callback issues and reliance on network clocks, which require corresponding solutions.

# Difference between Snowflake ID and other distributed ID generation algorithms

Currently, the commonly used distributed ID generation algorithms mainly include Snowflake ID, Twitter's improved Snowflake IdWorker, Meituan's Leaf algorithm, etc. Next, compare and explain these three algorithms:

  1. Snowflake ID

The Snowflake ID is an algorithm that generates unique IDs based on time stamps, data center IDs, machine IDs, serial numbers, and other information. By combining this information, the generated ID numbers are ordered, unique, and non repetitive, suitable for ID generation requirements in distributed systems.

  • Advantages:
    • The algorithm is simple, easy to implement and deploy;
    • The generated ID numbers are orderly, unique, and non repetitive;
    • Supports high concurrency and is suitable for distributed systems.
  • Disadvantages:
    • There is a time callback issue;
    • Depending on the system clock, there may be clock errors;
    • The data center ID and machine ID need to be manually configured.
  1. Snowflake IdWorker algorithm

Snowflake IdWorker algorithm is an improvement on the Snowflake ID. It avoids the problem of time callback by performing difference calculation on the timestamp, and automatically allocates the data center ID and machine ID, reducing the workload of manual configuration.

  • Advantages:
    • Avoiding time callback issues;
    • Automatically generate data center ID and machine ID;
    • The generated ID numbers are orderly, unique, and non repetitive;
    • Supports high concurrency and is suitable for distributed systems.
  • Disadvantages:
    • Depending on the system clock, there may be clock errors.
  1. Leaf algorithm

The Leaf algorithm is a distributed ID generation algorithm proposed by Meituan Dianping Company. It divides the ID number into three parts, which are used to represent timestamp, data center ID, and machine ID. Different from the Snowflake ID and Snowflake IdWorker algorithm, the Leaf algorithm uses ZooKeeper to ensure the uniqueness of data center ID and machine ID, while avoiding clock error and time callback problems.

  • Advantages:
    • Automatically generate data center ID and machine ID;
    • Avoiding time callback issues and clock errors;
    • The generated ID numbers are orderly, unique, and non repetitive;
    • Supports high concurrency and is suitable for distributed systems.
  • Disadvantages:
    • The algorithm is relatively complex and difficult to implement and deploy.

In summary, these three distributed ID generation algorithms have their own advantages and disadvantages, and the specific selection of which algorithm needs to be based on actual business needs and technical environment.

Last Updated: 7/3/2023, 2:17:39 PM