Choosing the Right Distributed ID Strategy: UUID, DB, Redis, Snowflake Explained
Distributed ID systems must be globally unique, incremental, highly available, and secure; this article compares common generation methods—including UUID, database auto‑increment, Redis atomic counters, and the Snowflake algorithm—detailing their advantages, drawbacks, and suitable use‑cases for modern high‑throughput services.
Characteristics of Distributed IDs
Globally unique – no duplicate IDs.
Incremental – benefits relational DB index performance.
Highly available – must handle high request pressure.
Security – avoid predictable patterns.
Generation Schemes
UUID
The core idea combines the machine's MAC address, current timestamp, and a random number.
Advantages:
Very high performance, generated locally without network overhead.
Simple generation, no high‑availability risk.
Good security due to low readability and lack of pattern.
Disadvantages:
Lengthy, not easy to store.
Potential MAC address leakage.
Unordered, which harms MySQL InnoDB index performance.
Database
Uses the auto_increment feature of databases such as MySQL auto_increment.
Advantages:
Simple, leverages built‑in database functionality.
Absolutely ordered.
Disadvantages:
Risk of duplicate IDs during master‑slave switch.
Requires special measures to ensure high availability.
Generation speed limited by database performance; scaling incurs high cost.
Redis
Redis provides atomic increment commands that guarantee uniqueness and order.
Advantages:
Simple, self‑contained capability.
Excellent performance under high concurrency, better than databases.
Lower maintenance cost than databases.
Disadvantages:
Potential duplicate IDs during master‑slave failover.
High availability must be specially ensured.
Snowflake Algorithm
Assigns a unique identifier to each machine and composes a global unique ID using the structure:
Timestamp + Machine ID + Auto‑increment sequenceTimestamp occupies high bits, sequence low bits, ensuring monotonic increase.
Advantages:
High generation performance.
Flexible bit allocation to suit business needs.
Disadvantages:
Strong dependency on machine clock; clock rollback can cause service anomalies.
Summary
Different schemes have distinct characteristics; choose the one that fits your scenario.
For example, Meituan early used multiple forms: DB auto‑increment, Redis generators, and UUIDs. Later they released a Snowflake‑like service called Leaf with a QPS of about 50 k/s.
Project address: https://github.com/Meituan-Dianping/Leaf
Other references:
https://github.com/hengyunabc/redis-id-generator
https://github.com/baidu/uid-generator
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.
Java High-Performance Architecture
Sharing Java development articles and resources, including SSM architecture and the Spring ecosystem (Spring Boot, Spring Cloud, MyBatis, Dubbo, Docker), Zookeeper, Redis, architecture design, microservices, message queues, Git, etc.
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.
