What is a UUID?
A UUID is a 128-bit number used to identify things uniquely. It looks like this:
550e8400-e29b-41d4-a716-446655440000
The magic of UUIDs is that you can generate them anywhere—your laptop, a server in Tokyo, a phone in Brazil—and they won't collide. No central registry needed. No coordination required.
This makes them perfect for distributed systems where multiple machines need to create IDs independently.
UUID Versions
There are several versions, but these are the ones you'll actually use:
| Version | How it's generated | When to use |
|---|---|---|
| v4 | Random | Most common. Default choice. |
| v7 | Timestamp + random | When you need sortable IDs |
| v1 | MAC address + timestamp | Legacy, avoid (leaks info) |
Version 4 is by far the most common. It's 122 bits of randomness, giving you about 5.3 × 10³⁶ possible values. You'd need to generate 1 billion UUIDs per second for 85 years to have a 50% chance of a collision.
Version 7 is newer and gaining popularity. It starts with a timestamp, so UUIDs sort chronologically. Great for database primary keys.
Example
// Most languages have built-in UUID support
crypto.randomUUID()
// → "f47ac10b-58cc-4372-a567-0e02b2c3d479"
// Or use a library for v7
import { v7 } from 'uuid';
v7()
// → "018d5f23-5c9a-7000-8000-4f6a7c8d9e0f"
Where You'll See This
- Database primary keys
- API request IDs for tracing
- Session identifiers
- File names for uploads
- Message queue deduplication
- Anything that needs a unique ID without coordination
Common Gotchas
If using UUIDs as primary keys in a database, consider UUID v7. Random v4 UUIDs cause index fragmentation because they're inserted in random order. v7 UUIDs are time-ordered, so inserts are sequential.
- Don't use v1 in production - It includes your MAC address, which is a privacy leak and potential security issue.
- Case insensitive -
550e8400-e29b-41d4-a716-446655440000and550E8400-E29B-41D4-A716-446655440000are the same UUID. - The hyphens are optional - Some systems store them without:
550e8400e29b41d4a716446655440000 - Not sequential - v4 UUIDs are random. If you need ordering, use v7 or a different ID scheme.
UUID vs Other IDs
| Type | Example | Pros | Cons |
|---|---|---|---|
| UUID v4 | f47ac10b-58cc-... | Universal, no collisions | Long, not sortable |
| UUID v7 | 018d5f23-5c9a-... | Sortable, no collisions | Newer, less support |
| Auto-increment | 1, 2, 3 | Simple, compact | Exposes count, needs DB |
| Snowflake | 1234567890123456 | Sortable, compact | Needs coordination |
| ULID | 01ARZ3NDEKTSV4... | Sortable, URL-safe | Less standard |
Try It
Generate UUIDs"UUID v4: Because 'just use a random number' needed a 36-character specification."