NanoID vs. UUID: Cryptographic Collision Math and URL Payload Efficiency
Compare NanoID and UUID across entropy, mathematical collision resistance, and string length. Discover which identifier is best suited for your application architecture.
When building modern web applications, distributed microservices, or public REST APIs, you inevitably encounter the question of how to identify resources.
For decades, the standard answer has been the UUID (Universally Unique Identifier). Whether Version 4 (pure random) or Version 7 (time-ordered), UUIDs are supported natively by programming languages, serialization libraries, and relational database column types.
However, in recent years, NanoIDโoriginally developed by Andrey Sitnikโhas gained immense popularity among frontend and full-stack developers.
Why did developers invent another identifier when UUID was already standard? The answer comes down to information density, payload size, and URL ergonomics.
In this guide, we evaluate the cryptographic mathematics, collision probabilities, and architectural tradeoffs between NanoID and UUID.
Anatomy: Comparing the Formats
| Attribute | Standard UUID (v4 / v7) | Default NanoID |
|---|---|---|
| String Length | 36 characters (with hyphens) | 21 characters |
| Alphabet Range | Hexadecimal: 0-9, a-f (16 symbols) |
URL-safe: A-Z, a-z, 0-9, _, - (64 symbols) |
| Bit Representation | 128 bits | ~126 bits |
| Effective Entropy | 122 bits (v4) / 74 bits (v7 random) | 126 bits |
| URL Compatibility | Needs hyphens; longer in query params | Native URL-safe, compact, copy-friendly |
| Database Native Type | UUID / BINARY(16) |
VARCHAR(21) / CHAR(21) |
The Mathematics of Entropy & Compactness
To understand why NanoID is so much shorter than a UUID while offering equivalent cryptographic collision resistance, we examine Shannon Information Entropy.
Information entropy measures the total unpredictability contained within a string. The formula for the entropy $E$ (in bits) is:
$$E = L \times \log_2(R)$$
Where:
- $L$ is the string length.
- $R$ is the size of the character alphabet (symbol pool).
Calculating UUIDv4 Entropy
A standard UUID is formatted as a 36-character hexadecimal string with hyphens (550e8400-e29b-41d4-a716-446655440000).
- While the representation is 36 characters long, the hyphens contain zero entropy.
- There are 32 hexadecimal digits. Since each hex digit has 16 possible values ($0$ through $f$), each digit holds $\log_2(16) = 4\text{ bits}$ of information.
- $32 \times 4 = 128\text{ bits}$ total.
- Subtracting the 4 version bits and 2 variant bits required by RFC 4122 leaves 122 bits of true random entropy.
Calculating NanoID Entropy
NanoID switches from a 16-symbol hexadecimal alphabet to a 64-symbol URL-safe alphabet consisting of:
- Uppercase letters
A-Z(26) - Lowercase letters
a-z(26) - Numbers
0-9(10) - Symbols
_and-(2)
Because $R = 64$, each character in a NanoID holds:
$$\log_2(64) = 6\text{ bits of entropy}$$
For a default 21-character string:
$$E = 21 \times 6 = 126\text{ bits of entropy}$$
The Result
A 21-character NanoID holds more cryptographic entropy (126 bits) than a 36-character UUIDv4 (122 bits).
By utilizing a richer 64-character alphabet, NanoID eliminates 15 charactersโa 41.6% reduction in string payloadโwith zero loss of collision safety.
Collision Math: The Birthday Paradox
Developers often worry that a shorter string must be more prone to collisions. We can test this statistically using the Birthday Problem formula.
The probability $p$ of at least one collision after generating $n$ random identifiers with an alphabet of total permutations $N$ is approximated by:
$$p \approx 1 - e^{-\frac{n^2}{2N}}$$
For a default 21-character NanoID, the total number of unique combinations is:
$$N = 64^{21} \approx 5.39 \times 10^{37}$$
What does this mean in production reality?
- If an application continuously generates 1,000 NanoIDs every single second, it would take approximately 4,100 years of continuous generation to reach a mere 1% probability of a single collision.
- If your application generates 10,000,000 IDs per second, reaching that same 1% threshold would still require roughly 149 days.
For virtually any web application, SaaS backend, or distributed mobile system, the risk of a NanoID collision is mathematically negligible.
Practical Architectural Tradeoffs
When NanoID is the Superior Choice
- Public & Shareable URLs:
When generating public document slugs, invite tokens, or shareable links (e.g.,
https://myapp.com/d/V1StGXR8_Z5jdHi6B-myT), shorter strings are easier for humans to read, click, and copy on mobile devices. - Bandwidth-Constrained APIs & JSON Payloads: In high-throughput microservices exchanging millions of JSON objects containing identifier arrays, saving 15 bytes per record adds up to meaningful network and caching savings over time.
- Custom Alphabets & Human Ergonomics:
NanoID allows developers to define custom alphabets. For example, if you are generating two-factor confirmation codes or gift cards, you can exclude visually ambiguous characters (like
0,O,1,l,I):import { customAlphabet } from 'nanoid'; // Clean alphabet without easily confused glyphs const safeCode = customAlphabet('23456789ABCDEFGHJKLMNPQRSTUVWXYZ', 10); const code = safeCode(); // e.g. "8K7W2NZ4TF"
When Standard UUID (v4 / v7) is the Superior Choice
- Relational Database Performance (UUIDv7): If you are indexing millions of rows in PostgreSQL or MySQL, standard UUIDv7 offers time-ordered prefixing that prevents B-Tree fragmentation. NanoID is random by default, meaning it exhibits the same indexing penalties as UUIDv4 unless custom timestamp formatting is implemented.
- Native Database Storage Types:
PostgreSQL provides a native
UUIDcolumn type that stores 128-bit identifiers internally as a compact 16-byte binary value, regardless of how they are printed in queries. Storing NanoID requires aVARCHAR(21)orCHAR(21), which consumes 21 bytesโroughly 31% more internal storage space than a native 16-byte UUID column. - Enterprise Standards & Interoperability: Many third-party integrations (like OpenTelemetry, Kafka tracing, AWS request IDs, or legacy enterprise ERPs) strictly validate the 8-4-4-4-12 hyphenated format.
Architectural Decision Matrix
Is the identifier intended for:
โโโ Database Primary Key with High Write Volume?
โ โโโ Choose -> UUIDv7 (Prevents B-Tree Index Fragmentation)
โ
โโโ Public URLs, Slugs, or User-Visible Tokens?
โ โโโ Choose -> NanoID (41% shorter, clean ergonomics)
โ
โโโ Strict RFC Standards / Interoperability Required?
โ โโโ Choose -> RFC 9562 UUID (Universal enterprise compatibility)
โ
โโโ Custom Friendly Codes (PINs, Vouchers, OTPs)?
โโโ Choose -> NanoID with Custom Alphabet
Conclusion
Both UUID and NanoID are cryptographically sound tools that leverage secure random number generators (CSPRNG).
- If your priority is compact, clean URLs and user-facing ergonomics, NanoID is the modern standard.
- If your priority is relational database indexing and schema standardization, UUIDv7 remains unmatched.
You can experiment with custom lengths, alphabets, and instantaneous client-side generation using our Online NanoID Generator and Online UUID Generator.
Modern UUID Core Engineering Team
Verified AuthorsEngineers specializing in distributed systems, cryptography, and client-side web performance. All implementations and architectural guides are validated against official IETF RFC test vectors and W3C Web Cryptography API standards.