Backend Development 15 min read

Global ID Generation Strategies for Distributed Systems

The article surveys common global ID generation strategies for distributed systems—database‑managed approaches like MySQL auto‑increment and MyCat‑Zookeeper, and Java‑based methods such as UUID, Snowflake, Leaf, Redis INCRBY and direct Zookeeper—comparing their trade‑offs and recommending Leaf (segment or Snowflake mode) or MyCat‑Zookeeper for reliable, scalable identifiers.

Java Tech Enthusiast
Java Tech Enthusiast
Java Tech Enthusiast
Global ID Generation Strategies for Distributed Systems

After adopting micro‑services, many seemingly simple problems become complex, such as generating a global unique ID.

This article surveys common global ID generation strategies, compares them, and provides practical guidance.

1. Two Main Approaches

The problem can be solved either by letting the database handle ID generation or by generating IDs in Java code before insertion.

2. Letting the Database Handle IDs

2.1 Modify MySQL Auto‑Increment Settings

When using sharding middleware like MyCat, the default auto‑increment of each physical DB can cause duplicate keys. You can change the start value and step of MySQL auto‑increment:

SHOW VARIABLES LIKE 'auto_increment%';

Set a custom step (e.g., 9):

set @@auto_increment_increment=9;

Create tables with a specific start value and step, e.g.:

create table test01(id integer PRIMARY KEY auto_increment, username varchar(255)) auto_increment=8;

By configuring each physical DB with different start values (1, 2, 3) and the same step (3), you can avoid collisions, but this method is cumbersome and not recommended for large‑scale systems.

2.2 MySQL + MyCat + Zookeeper

If you use MyCat, you can let it generate IDs via Zookeeper. The configuration steps include:

Set the primary‑key generation mode to 4 (Zookeeper) in server.xml .

Configure the table and primary key in schema.xml .

Provide Zookeeper connection info in myid.properties .

Define the ID segment in sequence_conf.properties (table name must be uppercase).

After restarting MyCat, the IDs are generated globally with good scalability.

3. Java‑Based ID Generation

3.1 UUID

UUID is easy to generate locally but has drawbacks: long string, poor index performance, random insertion pattern, and potential MAC‑address leakage.

3.2 Snowflake

Twitter’s Snowflake algorithm produces 64‑bit IDs composed of a sign bit, 41‑bit timestamp, 10‑bit worker ID, and 12‑bit sequence. It guarantees monotonicity and high insertion efficiency.

public class IdWorker {
    private static final long twepoch = 1288834974657L;
    private static final long workerIdBits = 5L;
    private static final long datacenterIdBits = 5L;
    private static final long sequenceBits = 12L;
    // ... (fields and constructor omitted for brevity)
    public synchronized long nextId() {
        long timestamp = timeGen();
        if (timestamp < lastTimestamp) {
            throw new RuntimeException("Clock moved backwards.");
        }
        if (lastTimestamp == timestamp) {
            sequence = (sequence + 1) & sequenceMask;
            if (sequence == 0) {
                timestamp = tilNextMillis(lastTimestamp);
            }
        } else {
            sequence = 0L;
        }
        lastTimestamp = timestamp;
        return ((timestamp - twepoch) << timestampLeftShift)
                | (datacenterId << datacenterIdShift)
                | (workerId << workerIdShift) | sequence;
    }
    // helper methods omitted
}

Usage example:

IdWorker idWorker = new IdWorker(0, 0);
for (int i = 0; i < 1000; i++) {
    System.out.println(idWorker.nextId());
}

3.3 Leaf (Meituan Open‑Source)

Leaf provides two modes: segment mode (DB‑based) and Snowflake mode (Zookeeper‑based). It stores segment information in a table leaf_alloc :

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');

After configuring leaf.properties , you can obtain IDs via HTTP endpoints such as http://localhost:8080/api/segment/get/leaf-segment-test or http://localhost:8080/api/snowflake/get/test .

3.4 Redis INCRBY

Redis can generate IDs using the atomic INCRBY command, but it offers no ordering guarantees.

3.5 Zookeeper Directly

Zookeeper can also be used for ID generation, but the implementation is more complex and generally not recommended.

4. Summary

If your project already uses MyCat, the MyCat+Zookeeper solution is convenient. Otherwise, Leaf (either segment or Snowflake mode) is the recommended choice for reliable, scalable global ID generation.

backendZookeeperMySQLdistributed-systemsGlobal IDleafsnowflake
Java Tech Enthusiast
Written by

Java Tech Enthusiast

Sharing computer programming language knowledge, focusing on Java fundamentals, data structures, related tools, Spring Cloud, IntelliJ IDEA... Book giveaways, red‑packet rewards and other perks await!

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.