Go back

Unique Id Generator Survey

Published:

Unique Id Generator Survey

Table of Contents

Open Table of Contents

Introduction

In this survey, I compare seven widely used pseudo-random unique-ID generators—UUID (v4 & v7), ULID, NanoID, MongoDB ObjectId, KSUID, TSID, and Twitter’s Snowflake—along key dimensions: bit-width, text representation, encoding, embedded timestamp (and sortability), randomness, collision resistance, human-readability, and potential pitfalls (check digits, “magic”/version bits, ambiguous characters). A consolidated comparison table is provided up front, followed by in-depth overviews of each scheme, discussion of strengths and weaknesses, and recommendations for scenarios ranging from high-throughput distributed systems to compact web tokens.

Comparison Table

Compiled from official specifications and community benchmarks (Wikipedia, GitHub, GitHub, MongoDB, GitHub, Foxhound Systems, Wikipedia)

GeneratorBit-WidthString LengthEncodingTimestamp BitsRandom BitsSortable?Check DigitCharacter SetAmbiguous Characters
UUID v412836 (8-4-4-4-12)Hexadecimal + hyphens0 (v4 is purely random)122 (6 version+variant bits) (Wikipedia)NoNo0–9, a–f, ‘–’0↔O, 1↔I/l
UUID v712836Hex + hyphens48 (Unix ms epoch) (npm)74 (version+counter+random) (npm)Yes (lexicographic)No0–9, a–f, ‘–’0↔O, 1↔I/l
ULID12826Crockford Base3248 (Unix ms epoch) (GitHub)80 (GitHub)Yes (lexicographic)NoA–Z, 0–9 (no I, L, O, U) (GitHub)Minimal (designed safe)
NanoIDVariable~21 (default)URL-safe Base64 variant0 (fully random)~168 bits of randomness (21 × 6 bits) (GitHub)NoNoA–Z, a–z, 0–9, ‘_’, ‘-’ (GitHub)‘_’↔‘-’, O↔0, l↔1
ObjectId9624 (hex)Hexadecimal32 (seconds since Unix epoch) (MongoDB)40 (5-byte random value) + 24 (counter) (MongoDB)Roughly (per-second order)No0–9, a–fMinimal (hex only)
KSUID16027Base6232 (big-endian UTC seconds since 2014-05-13) (GitHub)128 (GitHub)Yes (lexicographic)No0–9, A–Z, a–z0↔O, 1↔I/l
TSID6413Crockford Base3242 (ms-precision since custom epoch) (Foxhound Systems)22 (random/counter/node mix) (Foxhound Systems)Yes (numerical order)NoA–Z, 0–9 (no I, L, O, U) (Foxhound Systems)Minimal (safe set)
Snowflake64~18–19 digitsDecimal41 (ms since custom epoch) (Wikipedia)12 (sequence) + 10 (machine) (Wikipedia)Yes (numerical order)No0–9None (digits only)

1. UUID (Universally Unique Identifier)

UUIDs are 128-bit identifiers standardized by RFC 4122.

2. ULID (Universally Unique Lexicographically Sortable Identifier)

ULID is a 128-bit, Base32-encoded ID combining a 48-bit millisecond timestamp and 80 bits of randomness (GitHub).

3. NanoID

NanoID generates compact, cryptographically secure, URL-friendly IDs in JavaScript (and >20 languages), defaulting to 21 characters (≈168 bits) from an alphabet of 64 symbols (A–Z, a–z, 0–9, ‘_’, ‘–’) (GitHub).

4. MongoDB ObjectId

ObjectId is a 12-byte BSON type: 4 bytes timestamp (seconds), 5 bytes random value, 3 bytes counter (MongoDB).

5. KSUID (K-Sortable Unique IDentifier)

KSUID is a 20-byte (160 bit) ID: a 32-bit big-endian “KSUID epoch” timestamp + 128 bits of randomness, encoded as 27 Base62 characters (GitHub).

6. TSID (Time-Sorted Unique IDentifier)

TSID is a 64-bit integer: leading 42 bits for ms-precision timestamp + 22 bits of random/node/counter, optionally including a node ID, rendered as 13 Crockford Base32 characters (Foxhound Systems).

7. Snowflake (Twitter)

Snowflake is a 64-bit integer: 41 bittimestamp (ms since custom epoch) + 10 bits machine ID + 12 bits per-ms sequence, serialized as a decimal string (Wikipedia).

Recommendations

Each generator balances trade-offs in size, sortability, entropy, and human-factors. Your ideal choice depends on whether you prioritize pure randomness, timestamp ordering, storage efficiency, or ease of transcription.


Suggest Changes

Previous Post
Bit-twiddling vs. Logic in Modern C++
Next Post
FastCode.Guru Website, Inside & Out