CreateUUID — createuuid.com

UUID Versions Explained

Dev Tools Weekly Cheat Sheet
**By Dev Tools Weekly** | createuuid.com
When to use v1 vs v4 vs v5 vs v7 — with code examples.

UUID Format

xxxxxxxx-xxxx-Vxxx-Yxxx-xxxxxxxxxxxx

│ │

│ └── Variant (8, 9, a, or b = RFC 4122)

└─────── Version digit (1-8)

Total: 128 bits = 32 hex characters + 4 hyphens = 36 characters

Example: 550e8400-e29b-41d4-a716-446655440000


Version Comparison

VersionBased OnSortable?Unique Without Coordination?Use When
**v1**Timestamp + MAC✅ (roughly)Need time-ordering, don't mind leaking MAC
**v3**MD5 hash of name❌ (deterministic)Consistent ID from a name (legacy)
**v4**RandomDefault choice — random unique IDs
**v5**SHA-1 hash of name❌ (deterministic)Consistent ID from a name (preferred over v3)
**v6**Reordered v1Time-sortable v1 replacement (new)
**v7**Unix timestamp + random**Best for databases** — sortable, no MAC leak
**v8**CustomvariesvariesCustom/experimental

Deep Dive: Each Version

Version 1 — Timestamp + MAC Address

Structure:  time_low - time_mid - time_hi_version - clock_seq - node(MAC)

Bits: 32 16 16 16 48

⚠️ Privacy concern: Embeds the machine's real MAC address. Avoid in public-facing IDs.

Good for: Internal systems where time-ordering matters and you control the infrastructure.

Version 4 — Random

All 122 bits are randomly generated (6 bits reserved for version/variant).

Good for: Almost everything. Default choice for database PKs, API keys, session IDs.

Version 5 — Name-Based (SHA-1)

UUID = SHA-1(namespace_UUID + name)[first 128 bits]

Predefined namespaces:

NamespaceUUIDUse For
DNS6ba7b810-9dad-11d1-80b4-00c04fd430c8Domain names
URL6ba7b811-9dad-11d1-80b4-00c04fd430c8URLs
OID6ba7b812-9dad-11d1-80b4-00c04fd430c8ISO OIDs
X.5006ba7b814-9dad-11d1-80b4-00c04fd430c8X.500 DNs

Key property: Same namespace + same name → always the same UUID.

Good for: Deterministic IDs from URLs, emails, domain names. Idempotent operations.

Version 7 — Unix Timestamp + Random (NEW — RFC 9562)

Structure:

┌──────────────────┬────┬──────────────────────────────┐

│ Unix ms (48 bit) │ V │ Random (74 bits) │

└──────────────────┴────┴──────────────────────────────┘

Best for databases: Natural sort order = better B-tree index performance than random v4. No MAC leak like v1.


UUID vs. Alternatives

IdentifierBitsSortableLengthNotes
UUID v412836 charsUniversal standard
UUID v712836 charsNew standard, recommended
ULID12826 chars (Crockford Base32)Compact, popular in JS
Snowflake64~19 digitsTwitter/Discord. Needs coordinator.
KSUID16027 charsSegment. Large, very unique.
NanoIDconfigurable21 chars defaultURL-safe, configurable alphabet
CUID212824 charsCollision-resistant, secure
ObjectId (Mongo)9624 hexTimestamp + machine + counter

Code Examples

JavaScript / TypeScript

// Native (v4 random)

crypto.randomUUID();

// → "550e8400-e29b-41d4-a716-446655440000"

// UUID v7 (using 'uuid' package)

import { v7 as uuidv7 } from 'uuid';

uuidv7();

// → "01902e1c-0e2d-7000-8a3b-2c1d4e5f6a7b"

Python

import uuid

uuid.uuid4() # Random

uuid.uuid1() # Timestamp + MAC

uuid.uuid5(uuid.NAMESPACE_DNS, "example.com") # Name-based

PostgreSQL

-- Built-in (v4)

SELECT gen_random_uuid();

-- v7 (PostgreSQL 17+)

SELECT uuidv7(); -- or use extension

-- Use as primary key

CREATE TABLE users (

id UUID DEFAULT gen_random_uuid() PRIMARY KEY,

name TEXT NOT NULL

);

Go

import "github.com/google/uuid"

id := uuid.New() // v4

id := uuid.Must(uuid.NewV7()) // v7


Decision Flowchart

Need a unique ID?

├── Need to be deterministic (same input → same ID)?

│ └── YES → Use v5 (name-based SHA-1)

├── Need time-sortable IDs?

│ ├── For a database PK? → Use v7

│ └── For distributed systems? → Use v7 or ULID

└── Just need random uniqueness?

└── Use v4 (the default)


🛠 Generate UUIDs instantly: createuuid.com

📧 More cheat sheets: Dev Tools Weekly Newsletter