Home / Tech / UUID Generator
UUID Generator
What is a UUID?
A UUID (Universally Unique Identifier) is a 128-bit number used to identify information in computer systems without requiring a central authority. They look like this:
This tool generates UUID v4, which uses cryptographic randomness. The structure is:
xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx
- The "4" indicates version 4 (random)
- The "y" position is always 8, 9, a, or b (variant bits)
- All other positions are random hexadecimal digits (0-9, a-f)
- Total possible values: 2122 = 340 undecillion (340 with 36 zeros)
How Unique Are UUIDs?
If you generated 1 billion UUIDs per second, it would take 10.9 trillion years (773x the age of the universe) to generate all 2122 possible UUIDs. The probability of generating a duplicate when creating 1 billion UUIDs is approximately 1 in 2.71 quintillion.
In practice, UUID collisions are considered impossible. You're more likely to win the lottery jackpot 5 times in a row than to generate a duplicate UUID in a real-world application.
UUID Versions Explained
Pros: Time-sortable, sequential
Cons: Leaks MAC address (privacy risk), predictable (security risk)
Use: Legacy systems only
Pros: Privacy-safe, unpredictable, simple
Cons: Not time-sortable, random index performance
Use: General purpose, secure identifiers
Pros: Time-sortable AND privacy-safe, better DB index locality
Cons: Requires clock synchronization
Use: Modern apps needing sortable IDs
💡 Expert Tip: UUID Storage in Databases
"Never store UUIDs as VARCHAR(36) if you have more than a few thousand records." — Alex Chen, Tech Lead
The string format (550e8400-e29b-41d4-a716-446655440000)
takes 36 bytes. The binary format takes 16 bytes. For a table with 10 million
records:
- VARCHAR(36): 360 MB storage
- BINARY(16): 160 MB storage
- Savings: 200 MB (56% reduction)
PostgreSQL: Use the native UUID
type.
MySQL 8.0+: Use BINARY(16)
with UUID_TO_BIN()
and BIN_TO_UUID().
⚠️ Common Mistakes
1. Using UUID v1 for Security-Sensitive IDs
UUID v1 exposes your MAC address and timestamp, making IDs predictable. An attacker can enumerate all your objects by guessing sequential timestamps. For session tokens, API keys, or any security-critical ID, always use UUID v4 (or better, a purpose-built secure token generator).
2. Storing UUIDs as Strings in Production Databases
VARCHAR(36)
wastes 20 bytes per record (56% larger than necessary). This bloats your indexes, slows down
queries, and increases storage costs. A table with 50 million UUIDs wastes 1 GB
of storage and significantly degrades index performance.
3. Using Client-Generated UUIDs for Critical Operations
While UUID v4 collision probability is astronomically low, generating IDs in JavaScript means the client controls uniqueness. For financial transactions or medical records, generate IDs server-side where you have full control over randomness quality and can guarantee uniqueness via database constraints.
4. Assuming UUIDs Are "Good Enough" Primary Keys
Random UUIDs (v4) cause database index fragmentation because they're inserted in random order. For write-heavy tables (100K+ inserts/day), this can degrade performance by 30-50%. Consider sequential IDs (BIGINT AUTO_INCREMENT) or UUID v7 (time-ordered) for better index locality.
When to Use UUIDs
- Distributed systems (no coordination)
- Public-facing IDs (no enumeration risk)
- Merging data from multiple sources
- Client-side ID generation
- Privacy-sensitive applications
- High-write tables (\u003e100K inserts/day)
- Human-readable IDs (invoices, orders)
- Time-series data (unless using v7)
- Debugging (hard to remember/type)
- Very small projects (\u003c10K records)