Skip to main content
hhow09's Blog

Distributed ID Generation Solutions

Using auto-increment primary keys from traditional databases as ID for distributed systems can be inefficient and vulnerable to being predicted and analyzed.

Nowadays, distributed systems require unique global identifiers; it is an essential task in distributed computing with growing internet usage.

Requirement #

Functional Requirement #

Nice to have features #

  1. UUID V4
  2. Nano ID
  3. ULID
  4. KSUID
  5. Mongdb objectID
  6. Snowflake ID

Overview #

Designing a distributed ID generation scheme can be divided into two steps

  1. Generate the binary ID, usually selected from pseudo-random numbers, time, and node ID.
  2. Convert the binary ID into a human-readable and easily transmittable string text.

Binary ID Generation #

1. PRNG (pseudo random number generator) #

  1. UUID v4 (128 bit)
  2. Nano ID (no standard length)
random number (bits) total (bits)
UUID v4 122 128
Nano ID 126 126

UUID [2] #

UUIDv4 consists of 128 bits, which are typically represented as 32 hexadecimal characters in the following format: xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx, where the digit "4" in the third group signifies the version (i.e., UUIDv4) and the digit "y" in the fourth group specifies the variant.

Pros #

Cons #

Chance of duplicate ID

With the sheer number of possible combinations (2^128), it would be almost impossible to generate a duplicate unless you are generating trillions of IDs every second, for many years.

Nano ID [3] #

A tiny, secure, URL-friendly, unique string ID generator. Small. 130 bytes (minified and gzipped). No dependencies. Size Limit controls the size. Safe. It uses hardware random generator. Can be used in clusters. Short IDs. It uses a larger alphabet than UUID (A-Za-z0-9_-). So ID size was reduced from 36 to 21 symbols. Portable. Nano ID was ported to 20 programming languages.

2. Timestamp + PRNG #

timestamp (bits) random number (bits) total (bits)
ULID 48 80 128
KSUID 32 128 160

ULID [4] #

KSUID [4] #

There are numerous methods for generating unique identifiers, so why KSUID?

  1. Naturally ordered by generation time
  2. Collision-free, coordination-free, dependency-free
  3. Highly portable representations

3. Timestamp + Node ID + Monotonic Counter #

timestamp (bits) Node Id (bits) Process Id (bits) Counter (bits) total (bits)
Snowflake ID 41 10 - 12 64
MongoDB ObjectID 32 24 16 24 96

Security Concern #

Snowflake ID [6] #

MongoDB ObjectID [7] #

Types of data source #

Pseudo random number #

Packages #

Considerations #

Reference #

  1. 6 个流行的分布式 ID 方案之间的对决
  2. UUID
  3. Nano ID
  4. ULID
  5. KSUID
  6. Snowflake ID
  7. MongoDB ObjectID