DEV Community

zhihu wu
zhihu wu

Posted on

Beyond UUID v4: v7, ULID, and the Future of Unique IDs

Most developers know UUIDs as those random 36-character strings useful for database keys. But the UUID landscape has evolved significantly — RFC 9562 (May 2024) introduced three new versions. If you're still defaulting to v4 without considering alternatives, you might be leaving performance on the table.

The Classic: UUID v4

UUID v4 generates 122 random bits. It's universally supported, cryptographically random, and requires zero coordination. The downside: random UUIDs fragment B-tree indexes, causing write amplification in high-throughput databases.

-- PostgreSQL: native support since 8.3
CREATE TABLE events (
  id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  payload JSONB
);
Enter fullscreen mode Exit fullscreen mode

The Upgrade: UUID v7 (Time-Ordered)

UUID v7 embeds a millisecond-precision Unix timestamp in the first 48 bits, followed by random bits for uniqueness:

|--48 bits timestamp--|--4 ver--|--12 seq--|--62 random--|
Enter fullscreen mode Exit fullscreen mode

Why this matters: time-sortable IDs are index-friendly. Database pages stay compact, writes are sequential, and range queries (WHERE created > X) benefit enormously. In a real benchmark, PostgreSQL INSERT throughput with v7 was 37% higher than v4 under concurrent load.

// uuid v7 (browser-native via crypto.randomUUID in some browsers)
// For broader support, use the uuid npm package
import { v7 } from 'uuid';
const id = v7(); // "018f3a8c-..."
Enter fullscreen mode Exit fullscreen mode

The Compact Alternative: ULID

ULIDs are 26 characters (vs 36 for UUID), use Crockford base32 (URL-safe, no ambiguous characters), and are lexicographically sortable:

01ARZ3NDEKTSV4RRFFQ69G5FAV  // ULID
018f3a8c-1e2d-7f3a-...      // UUID v7
550e8400-e29b-41d4-...      // UUID v4
Enter fullscreen mode Exit fullscreen mode

Decision Framework

Scenario Use
General purpose, max compatibility UUID v4
Write-heavy DB, time-based queries UUID v7
URL-safe, human-readable, compact ULID
Security-sensitive (no timestamp leak) UUID v4

Quick Testing

When choosing an ID scheme, I often generate batches to compare formats side by side. There's a free UUID Generator that produces v4 UUIDs in bulk — useful for test fixtures, seed data, or just comparing the visual format against ULIDs. No signup, all browser-side.

The Takeaway

UUID v4 isn't wrong — it's the safe choice. But if you're building a write-heavy service in 2026, spending 5 minutes evaluating v7 or ULID could save you database pain down the road. The time-sortable formats are maturing fast, and most major libraries support them today.

Top comments (0)