Backend Development 11 min read

Global Unique ID Generation Strategies: Overview, Advantages, Disadvantages, and Optimizations

The article provides a comprehensive overview of global unique ID generation in distributed systems, detailing the characteristics of a good ID, comparing five common strategies—database auto‑increment, UUID, Redis, Zookeeper, and Twitter Snowflake—along with their pros, cons, and practical optimization tips.

Top Architect
Top Architect
Top Architect
Global Unique ID Generation Strategies: Overview, Advantages, Disadvantages, and Optimizations

In complex distributed systems a globally unique identifier is essential for reliably distinguishing records, especially when data is sharded across multiple databases or services.

Key characteristics of a good global ID include absolute uniqueness, monotonic or trend‑increasing order for indexing efficiency, security (non‑predictability), high availability (no single point of failure), support for sharding, and a reasonable length.

1. Database auto‑increment uses the database's native sequence or auto‑increment column to guarantee uniqueness within the whole database. Advantages : simple, low cost, monotonic, and easy to implement. Disadvantages : strong dependency on the DB, single‑point failure, difficult to scale, and limited throughput on a single MySQL instance.

2. UUID (Universally Unique Identifier) is generated entirely in the application. It provides global uniqueness without a central service. Example in Java:

UUID uuid = UUID.randomUUID();
String s = UUID.randomUUID().toString();

Advantages : easy to use, no external dependency, high uniqueness across migrations. Disadvantages : large storage (128‑bit, usually stored as 36‑character string), poor index performance, non‑monotonic, and potential security concerns when generated from MAC addresses.

3. Redis‑generated IDs leverage Redis's atomic INCR or INCRBY commands. A cluster can be used to increase throughput by assigning each node a distinct start value and step. Example sequences:

A: 1,6,11,16,21
B: 2,7,12,17,22
C: 3,8,13,18,23
D: 4,9,14,19,24
E: 5,10,15,20,25

Advantages : no DB dependency, high performance, natural ordering, and easy to generate daily serial numbers. Disadvantages : requires adding Redis to the stack, configuration overhead, and Redis itself can become a single‑point failure.

4. Zookeeper can generate IDs using the version number of a znode, yielding 32‑ or 64‑bit values. It is rarely used because it introduces a dependency on Zookeeper and may involve multi‑step API calls, making it less suitable for high‑concurrency scenarios.

5. Twitter Snowflake is a 64‑bit ID composed of a timestamp, data‑center ID, machine ID, and a sequence number. It provides high‑throughput, monotonic IDs without a central database. The layout typically allocates 41 bits for milliseconds, 10 bits for machine identifiers, and 12 bits for per‑millisecond sequence.

Pros of Snowflake : high stability, no DB dependency, configurable bit allocation, and globally monotonic ordering. Cons : relies on accurate system clocks (clock rollback can cause duplicates), and IDs may not be globally increasing across nodes with unsynchronized clocks.

Several optimization ideas are discussed, such as using multiple master databases with different start values to avoid DB bottlenecks, converting UUIDs to 64‑bit integers for readability, and applying the Comb algorithm (GUID + timestamp) to improve index performance.

Overall, the choice of ID generation strategy should consider factors like traffic volume, storage constraints, ordering requirements, fault tolerance, and operational complexity.

distributed systemsDatabaseRedisUUIDGlobal IDsnowflake
Top Architect
Written by

Top Architect

Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.

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.