UUID (Universally Unique Identifier)

A random ID that won't collide with anyone else's, ever.

3 min read

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:

VersionHow it's generatedWhen to use
v4RandomMost common. Default choice.
v7Timestamp + randomWhen you need sortable IDs
v1MAC address + timestampLegacy, 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

javascript
// 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

Database Performance

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-446655440000 and 550E8400-E29B-41D4-A716-446655440000 are 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

TypeExampleProsCons
UUID v4f47ac10b-58cc-...Universal, no collisionsLong, not sortable
UUID v7018d5f23-5c9a-...Sortable, no collisionsNewer, less support
Auto-increment1, 2, 3Simple, compactExposes count, needs DB
Snowflake1234567890123456Sortable, compactNeeds coordination
ULID01ARZ3NDEKTSV4...Sortable, URL-safeLess standard

Try It

Generate UUIDs

"UUID v4: Because 'just use a random number' needed a 36-character specification."