Common Distributed Unique ID Generation Strategies and Their Implementation
The article reviews five major distributed unique ID generation methods—including UUID, Redis auto‑increment, database auto‑increment, database segment mode, and Snowflake—explains their principles, advantages, drawbacks, and provides practical integration guides and code examples for Java developers.
What are the current industry unique ID generation methods?
There are roughly five major approaches, illustrated in the diagram below.
UUID Mode
UUID (Universally Unique Identifier) is a 128‑bit value generated from MAC address, timestamp and random numbers, usually represented as a 32‑character hexadecimal string.
public static void main(String[] args) {
String uuid = UUID.randomUUID().toString().replaceAll("-", "");
System.out.println(uuid);
}Advantages: Guarantees uniqueness, requires no external middleware, and generates IDs extremely fast.
Disadvantages: The 32‑character string consumes more storage than 4‑byte int or 8‑byte long, and the IDs are not ordered, which can cause index fragmentation and slower inserts in databases.
Despite these drawbacks, UUIDs are still widely used for log tracing and other scenarios where ordering is not critical.
Redis Auto‑Increment Mode
Redis provides the INCR command, which produces a monotonically increasing 64‑bit integer. A single Redis instance can generate up to 19‑digit numbers, enough for billions of IDs per day for decades.
Advantages: Guarantees uniqueness and monotonic increase, is memory‑resident for ultra‑fast generation, and is friendly to database indexing.
Disadvantages: Redis is an in‑memory KV store; data loss can occur during failover or asynchronous replication, so high‑availability and retry mechanisms are required.
Database Auto‑Increment Mode
Traditional relational databases can generate unique IDs via auto‑increment primary keys. An insert returns the generated key.
INSERT INTO your_table (column1, column2) VALUES ('value1', 'value2');
SELECT LAST_INSERT_ID();Advantages: Guarantees uniqueness and provides natural ordering.
Disadvantages: Generation speed is limited by disk I/O; each ID requires a separate insert, making it unsuitable for high‑concurrency scenarios.
Database Segment Mode
This is an optimized version of the database auto‑increment approach. Instead of requesting a single ID each time, an application obtains a range (segment) of IDs from the database and then serves IDs from memory.
Advantages: Maintains uniqueness and monotonic increase while achieving high throughput (e.g., Meituan’s Leaf reaches 50 k IDs/second).
Disadvantages: Strongly depends on the underlying database; however, most systems already rely on a database.
It is a good fit for small‑to‑medium companies that need decent performance without adding extra middleware.
Snowflake Algorithm Mode
Originally created by Twitter, Snowflake generates 64‑bit long IDs composed of a sign bit, 41‑bit timestamp, 5‑bit machine ID, 5‑bit datacenter ID, and 12‑bit sequence.
1 bit: sign (0 for positive IDs)
41 bits: timestamp (milliseconds since a custom epoch, supporting ~69 years)
10 bits: machine (work) ID, configurable per host or rack
12 bits: sequence within the same millisecond (up to 4096 IDs per node per ms)
Advantages: Guarantees uniqueness, provides ordered IDs, extremely fast, and has weak dependency on third‑party middleware.
Disadvantages: Requires synchronized clocks to avoid duplicate IDs, IDs grow over time (may not suit all scenarios), and machine‑ID configuration/maintenance is needed.
How to Choose the Right Method?
The following comparison table (image) helps decide which solution fits a particular business based on team size, technical expertise, implementation complexity, and maintenance cost.
Ready‑Made ID Generation Tools
Several open‑source Java libraries implement the above algorithms:
Meituan’s Leaf (segment and Snowflake modes)
Baidu’s UidGenerator (Snowflake‑style)
Didi’s Tinyid (segment mode, based on Leaf)
All three are Java‑based and can be used out‑of‑the‑box.
Integrating Meituan Leaf Segment Mode
1. Clone the source from GitHub.
2. Build the project: mvn clean install -DskipTests
3. Execute the provided SQL script to create the leaf_alloc table.
CREATE DATABASE leaf;
CREATE TABLE `leaf_alloc` (
`biz_tag` varchar(128) NOT NULL DEFAULT '',
`max_id` bigint(20) NOT NULL DEFAULT '1',
`step` int(11) NOT NULL,
`description` varchar(256) DEFAULT NULL,
`update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`biz_tag`)
) ENGINE=InnoDB;
INSERT INTO leaf_alloc(biz_tag, max_id, step, description) VALUES('leaf-segment-test', 1, 2000, 'Test leaf Segment Mode Get Id');4. Configure the database connection in the leaf-server module.
5. Start leaf-server and call the API, e.g., http://localhost:8080/api/segment/get/leaf-segment-test .
To use a different business tag, insert a new row into leaf_alloc .
Using Leaf Core Locally (Web‑less Mode)
Add the Maven dependency:
<dependency>
<groupId>com.tencent.devops.leaf</groupId>
<artifactId>leaf-core</artifactId>
<version>1.0.2-RELEASE</version>
</dependency>Then initialize the ID generator in a Spring service:
@Service
public class LeafIdDBSegmentImpl implements InitializingBean {
IDGen idGen;
DruidDataSource dataSource;
@Override
public void afterPropertiesSet() throws Exception {
Properties properties = PropertyFactory.getProperties();
dataSource = new DruidDataSource();
dataSource.setUrl(properties.getProperty("leaf.jdbc.url"));
dataSource.setUsername(properties.getProperty("leaf.jdbc.username"));
dataSource.setPassword(properties.getProperty("leaf.jdbc.password"));
dataSource.init();
IDAllocDao dao = new IDAllocDaoImpl(dataSource);
idGen = new SegmentIDGenImpl();
((SegmentIDGenImpl) idGen).setDao(dao);
idGen.init();
}
public Long getId(String key) {
Result result = idGen.get(key);
if (result.getStatus().equals(Status.EXCEPTION)) {
throw BizException.createBizException(BizErrorCode.LEAF_CREATE_ERROR_REQUIRED);
}
return result.getId();
}
}This class initializes the data source, creates the DAO, and provides a getId method that applications can call with the previously configured bizTag .
Original content, please like, comment, follow, and share.
Rare Earth Juejin Tech Community
Juejin, a tech community that helps developers grow.
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.