Backend Development 8 min read

Choosing the Right Distributed ID Generation Strategy for Scalable Systems

This guide examines ten popular distributed ID generation techniques—from simple UUIDs and database auto‑increment keys to advanced Snowflake, Redis, Zookeeper, and open‑source solutions like Baidu uid‑generator, Meituan Leaf, and Didi Tinyid—highlighting their pros, cons, and suitable scenarios for high‑traffic systems.

Lobster Programming
Lobster Programming
Lobster Programming
Choosing the Right Distributed ID Generation Strategy for Scalable Systems

In large‑scale distributed systems such as finance, payment, and order processing, unique identifiers are essential for data partitioning and messaging. This article reviews ten common distributed ID generation approaches, comparing their advantages, disadvantages, and appropriate usage scenarios.

1. UUID

<code>UUID uuid = UUID.randomUUID();</code>

Advantages: Simple to implement; generated locally without network overhead; globally unique.

Disadvantages: Stored as a string, leading to poor storage performance; lacks business meaning and readability; not sequential.

2. Database Auto‑Increment Primary Key

Insert a row and return the generated primary key to obtain a unique ID.

Advantages: Low cost, easy to implement; IDs are sequential and have some business readability.

Disadvantages: Relies on a single database instance, creating a single‑point‑of‑failure; cannot handle high concurrency.

3. Database Cluster Mode

Deploy multiple database instances, assign each a start value and step size, ensuring generated IDs increase without duplication.

Advantages: Solves the single‑point issue of the auto‑increment approach.

Disadvantages: Limits future scalability; each database still bears heavy load, unsuitable for very high concurrency.

4. Database Segment (Range) Mode

Allocate a range of auto‑increment IDs from the database (e.g., 1‑3000) and use them locally until exhausted.

Advantages: Reduces database calls for each ID generation, improving service performance.

Disadvantages: Still has a single‑point failure risk; database restarts may cause ID gaps.

5. Redis INCR

Redis provides a single‑threaded INCR command that guarantees unique, ordered IDs. Combining the counter with timestamps and machine identifiers yields a custom unique ID.

Advantages: Ordered, readable, high performance.

Disadvantages: Depends on Redis, introducing a single‑point risk.

6. Zookeeper Sequential Nodes

Creating a sequential node in Zookeeper appends a 10‑digit sequence number, ensuring uniqueness.

Disadvantages: Requires Zookeeper and potentially distributed locks; not commonly used for ID generation.

7. Snowflake Algorithm

Generates a 64‑bit long value composed of timestamp, machine ID, and sequence number. The bit layout provides globally unique, ordered IDs.

Advantages: High throughput, no central coordination.

Disadvantages: Strongly dependent on system clock—clock rollback can cause duplicates; 41‑bit timestamp limits usable span to ~69 years; sequence overflow requires additional handling.

8. Baidu uid‑generator

Based on Snowflake but allows custom timestamp, machine ID, and sequence fields, offering higher flexibility.

<code>https://gitcode.com/baidu/uid-generator/overview?utm_source=artical_gitcode</code>

9. Meituan Leaf

Combines database segment mode and Snowflake, allowing flexible switching between the two based on business needs.

<code>https://github.com/Meituan-Dianping/Leaf</code>
<code>https://tech.meituan.com/2019/03/07/open-source-project-leaf.html</code>

Segment Mode: Low‑bit growth, minimal ID waste, tolerates short MySQL outages.

Snowflake Mode: Fully distributed, IDs carry semantic meaning.

10. Didi Tinyid

Extends Meituan Leaf using a database segment approach with multi‑master support. Provides both REST API and Java client for easy integration.

<code>https://github.com/didi/tinyid</code>

Advantages: High performance, capable of generating billions of IDs daily.

Choosing the appropriate ID generation method depends on the specific characteristics of your business workload, such as data volume, concurrency requirements, and tolerance for single‑point failures.

BackendscalabilityRedissnowflakedistributed IDunique identifier
Lobster Programming
Written by

Lobster Programming

Sharing insights on technical analysis and exchange, making life better through technology.

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.