DEV Community

woofgl
woofgl

Posted on Originally published at mtoolhub.com

ID Format Comparison: UUID vs ULID vs NanoID vs KSUID vs Snowflake

Table of Contents

  1. Why ID Format Is a Real Decision
  2. The Contenders at a Glance
  3. UUID: The Standard
  4. ULID: Sortable and URL-Friendly
  5. NanoID: Compact and Customizable
  6. KSUID: Big Randomness with a Timestamp
  7. Snowflake: 64 Bits, Coordinated
  8. Sortability and Database Indexes
  9. How to Choose

Why ID Format Is a Real Decision

An ID looks like the smallest decision in a system, yet it touches everything: how fast your database index absorbs inserts, whether your API leaks record counts, whether URLs stay short, and whether JavaScript silently corrupts your keys. The five formats below all solve "give this thing a name" - and they make sharply different trade-offs doing it.


The Contenders at a Glance

UUID (v4/v7) ULID NanoID KSUID Snowflake
Total size 128 bits 128 bits ~126 bits 160 bits 64 bits
Text form 36 chars 26 chars 21 chars 27 chars up to 19 digits
Time-sortable v7 only yes no yes yes
Needs coordination no no no no yes (worker IDs)
Safe as JS number no no no no no - 64 bits overflows
Alphabet hex Crockford Base32 URL-safe Base64 Base62 decimal digits

One row deserves emphasis before anything else: every format here is longer than JavaScript's 53-bit safe integer range, so always treat IDs as strings in JSON - the classic Snowflake bug is a backend sending a 64-bit number that JavaScript rounds into a different, wrong ID.


UUID: The Standard

The 128-bit UUID is the default answer everywhere: every database has a native type for it, every language has a generator, and RFC 9562 keeps it evolving. Version 4 (122 random bits) is the everyday choice; version 7 prepends a millisecond timestamp and fixed the randomness-driven index problems of v4. The cost is bulk: 36 characters with hyphens, or 16 bytes in a native column.

We covered UUID versions, collision math, and generation in detail in the UUID complete guide - the short version: if you have no reason to leave, v7 in a native uuid column is a perfectly modern choice.


ULID: Sortable and URL-Friendly

A ULID is also 128 bits, split as 48 bits of millisecond timestamp followed by 80 random bits, rendered in 26 characters of Crockford Base32:

01ARZ3NDEKTSV4RRFFQ69G5FAV
└── 10 chars time ──┘└ 16 chars randomness ┘
Enter fullscreen mode Exit fullscreen mode

Three properties follow from that anatomy:

  1. Lexicographic sortability. Two ULIDs generated in order sort in order, because the most significant digits carry time. This gives you v7-style index behavior in any storage that compares strings.
  2. URL-friendly, case-insensitive text. Crockford Base32 drops I, L, O and U to prevent both visual confusion and accidental profanity; no hyphens to strip, no case-folding surprises.
  3. Bit-compatible with UUID. Also 128 bits, so a ULID fits a native uuid column and round-trips to a UUID losslessly.

The limitation: 80 random bits per millisecond. Collisions remain astronomically unlikely in any single process, but strictly monotonic generation within one millisecond needs a per-process counter in serious implementations.


NanoID: Compact and Customizable

NanoID takes the opposite bet: drop the timestamp, keep pure randomness, but use a larger alphabet so the string gets shorter. The default is 21 characters from a 64-symbol URL-safe alphabet (A-Za-z0-9_-), which packs about 126 bits of entropy - slightly more than a UUID v4 in 15 fewer characters.

Because the alphabet and length are parameters, NanoID is the format of choice when the ID is visible: short links, share URLs, room codes. A 10-character NanoID still carries 60 bits - fine for a URL key with rate limiting, hopeless for a high-volume database key. Two practical notes: keep the default size unless you have done the birthday-math for your volume, and always generate with the crypto-backed API, never a shuffled alphabet by hand.


KSUID: Big Randomness with a Timestamp

Segment's KSUID is the maximalist: 160 bits total - a 32-bit epoch-seconds timestamp plus 128 bits of randomness - rendered as 27 Base62 characters. It was designed for event pipelines, where the generous random space gives headroom for merging streams and the embedded second-resolution timestamp lets you order or inspect events without a schema lookup. The trade-offs mirror its size: it is the longest of the text formats and its custom epoch (May 2014) means the timestamp needs a KSUID-aware decoder to read.


Snowflake: 64 Bits, Coordinated

Twitter's Snowflake packs everything into a single 64-bit integer:

1 bit unused | 41 bits millisecond timestamp | 10 bits machine | 12 bits sequence
Enter fullscreen mode Exit fullscreen mode

That is 69 years of range, 1,024 generator nodes, and 4,096 IDs per millisecond per node - all in the size of a database BIGINT, half the storage of a UUID. The price is coordination: every generator must hold a unique machine ID, or the entire scheme collides. Cloud platforms sell this as a service (AWS Snowflake-style ID generators, Snowflake IDs in managed queues), and most sharded mega-systems run some variant of it.

The JavaScript trap deserves its own paragraph: 64-bit Snowflake IDs exceed Number.MAX_SAFE_INTEGER, so JSON.parse rounds them into wrong values. APIs must serialize them as strings - Twitter learned this publicly. If your stack is JavaScript-heavy, that quirk nudges you toward ULID or NanoID, which are strings by construction.


Sortability and Database Indexes

Why does "sortable" appear in every row worth caring about? Because of how B+ tree indexes grow:

  • Random keys (UUID v4, NanoID) insert at random leaf positions, causing page splits and cache misses. At small scale you will never notice; at high insert rates, throughput measurably drops.
  • Time-ordered keys (v7, ULID, KSUID, Snowflake) append at the right edge of the index - hot pages stay cached and inserts stay cheap.

The counter-trap: strict monotonicity concentrates all writes on the last page, which single-node databases love but some distributed stores hate (DynamoDB-style systems recommend adding a hash prefix to spread partitions). Sortability is a tool, not a law - match it to your storage engine.


How to Choose

  1. Database primary key, general purpose: UUID v7 or ULID in a native 16-byte column - sortable, standard, zero coordination.
  2. Public-facing IDs and short links: NanoID - shortest for its entropy, URL-safe, no order leaked.
  3. Event streams and log pipelines: KSUID - timestamp plus huge random space for merging.
  4. Massive scale, controlled fleet, storage-constrained: Snowflake - 64 bits, but mind the JS string requirement and worker-ID management.
  5. "I just need an ID and don't want a dependency": crypto.randomUUID() - v4 UUID is built into every modern runtime.

Whatever you pick, verify the properties you care about with real values: generate a batch with the MToolHub UUID generator, check sortability by eye, and paste one into the JSON formatter to see how it survives an API round-trip. For the deeper tour of UUID versions and collision odds, start from the UUID complete guide; for the time math inside ULID, v7 and Snowflake, the Unix timestamp guide has the details.


Related Tools

Further Reading


Reference Resources


Copyright © MToolHub Online Tools
Original Article: https://www.mtoolhub.com/blog/id-format-comparison
When reprinting, the original source and this statement must be indicated in the form of a link.

Top comments (0)