Backend Development 5 min read

Understanding the Snowflake ID Generation Algorithm and Its Variants (Baidu UIDGenerator, Meituan Leaf‑Snowflake)

This article explains Twitter's Snowflake distributed ID algorithm, its 64‑bit structure, advantages and drawbacks, and compares it with Baidu's UIDGenerator and Meituan's Leaf‑Snowflake implementations, helping readers choose the most suitable unique‑ID solution for their backend systems.

Wukong Talks Architecture
Wukong Talks Architecture
Wukong Talks Architecture
Understanding the Snowflake ID Generation Algorithm and Its Variants (Baidu UIDGenerator, Meituan Leaf‑Snowflake)

Twitter open‑sourced the Snowflake algorithm, a 64‑bit distributed ID generator that divides the ID into four parts: 1 unused bit, 41 bits for a millisecond timestamp (covering 69 years), 10 bits for data‑center and machine identifiers (up to 32 data‑centers and 32 machines each), and 12 bits for a per‑millisecond sequence (up to 4096 IDs).

1 bit: unused, set to 0.

41 bits: millisecond timestamp.

10 bits: 5 bits for data‑center ID, 5 bits for machine ID.

12 bits: sequence number within the same millisecond.

Advantages :

IDs are roughly time‑ordered because the timestamp occupies the high bits.

No reliance on external databases; high performance and stability.

Bit allocation can be customized to fit business needs.

Disadvantages :

Strong dependence on the system clock; clock rollback can cause duplicate IDs or service downtime.

Baidu UIDGenerator (based on an optimized Snowflake): uses future timestamps and a double‑buffer strategy to handle clock rollback and improve generation performance, while integrating MySQL for ID segment allocation.

Advantages : solves clock‑rollback and performance issues.

Disadvantages : depends on a MySQL database.

Meituan Leaf‑Snowflake :

Named “Leaf” after Leibniz’s quote that no two leaves are identical, emphasizing uniqueness.

IDs are obtained via a proxy service that fetches a batch (segment) from the database; a double‑buffer caches the next segment when 10% of the current one is used, ensuring continuous issuance even if the DB temporarily fails.

Linear scalability; performance meets most business scenarios.

64‑bit IDs are monotonically increasing, suitable as primary keys.

High fault tolerance: cached segments allow operation during short DB outages.

Configurable max_id simplifies migration from legacy ID schemes.

Network jitter does not affect segment updates.

Disadvantages :

ID pattern is predictable, potentially leaking issuance volume and reducing security.

How to Choose : For most internal systems, the original Snowflake algorithm is sufficient; if higher security and reliability are required, consider Baidu's UIDGenerator or Meituan's Leaf‑Snowflake.

backenddistributed systemssnowflakeID generationUnique ID
Wukong Talks Architecture
Written by

Wukong Talks Architecture

Explaining distributed systems and architecture through stories. Author of the "JVM Performance Tuning in Practice" column, open-source author of "Spring Cloud in Practice PassJava", and independently developed a PMP practice quiz mini-program.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.