Choosing the Right Distributed ID Strategy for Microservices
This article reviews common distributed ID generation methods—including UUID, database auto‑increment, segment mode, Redis, Snowflake, Baidu UidGenerator, Meituan Leaf, and Zookeeper—detailing their principles, advantages, drawbacks, and suitable scenarios to help developers select the most appropriate solution for their microservice architecture.
1. JDK built‑in UUID
Java’s UUID generator creates globally unique identifiers by combining the server’s MAC address, current timestamp, and a random component.
Advantages: simple to generate, good performance, globally unique, works well during data migration or system merges.
Disadvantages: stored as strings, low readability, large storage footprint, slower queries, and higher network overhead in microservice environments.
2. Database auto‑increment ID
A dedicated database instance can generate IDs using an auto_increment column; a record is inserted to obtain the primary key.
When traffic spikes, the database becomes a bottleneck, making this approach risky for distributed services.
Advantages: IDs are strictly ordered and easy to implement.
Disadvantages: requires a separate database instance, high cost, limited performance, and potential security concerns; clustering can mitigate but adds complexity.
3. Segment mode
IDs are allocated in batches from the database, with the maximum value stored and the current range kept in memory, often using Redis.
Advantages: reduces database load and improves performance.
Disadvantages: local generation can cause single‑point failures and non‑continuous IDs after service restarts.
4. Redis generation
Redis can generate global IDs using its atomic INCR/INCRBY commands, leveraging its single‑threaded nature.
Advantages: no database dependency, high performance, numeric IDs are naturally ordered, and Redis clusters mitigate single‑point failures.
Disadvantages: adds Redis as a third‑party component and requires additional coding and configuration effort.
5. Snowflake algorithm
Twitter’s Snowflake generates a 64‑bit ID composed of a timestamp (41 bits), a machine identifier (10 bits), and a sequence number (12 bits), yielding roughly 4096 IDs per millisecond per node.
Advantages: simple, fast, time‑ordered IDs, flexible bit allocation, no external dependencies.
Disadvantages: relies on synchronized clocks; clock rollback can cause duplicate IDs, and managing worker IDs across nodes can be cumbersome.
6. Baidu UidGenerator
UidGenerator is a Java implementation based on Snowflake, packaged as a component that supports custom worker‑ID bits and initialization strategies, suitable for containerized environments.
Advantages: globally unique, high availability, high performance, mitigates clock rollback issues.
Disadvantages: includes a built‑in WorkerID allocator that depends on a database during startup; custom implementations can remove this dependency.
7. Meituan Leaf
Meituan’s Leaf component builds on Snowflake with two optimizations: a segment‑based database approach that batches allocations via a proxy server, and a Snowflake variant that rejects ID requests on clock rollback and raises alerts.
Advantages: globally unique, high availability, high performance, uses Zookeeper to handle clock rollback with weak Zookeeper dependency.
Disadvantages: depends on Zookeeper.
8. Zookeeper unique ID
Zookeeper can generate 32‑bit or 64‑bit sequence numbers using node information, which clients can use as unique identifiers.
Advantages: simple implementation.
Disadvantages: requires Zookeeper, involves multiple API calls, and may need distributed locks under high contention, limiting performance.
Overall, ID generation solutions fall into two categories: centralized approaches (e.g., databases, Redis) that produce shorter, incrementing IDs but can become bottlenecks, and decentralized approaches (e.g., UUID) that are simple and highly scalable but result in longer identifiers without natural ordering.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Sanyou's Java Diary
Passionate about technology, though not great at solving problems; eager to share, never tire of learning!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
