Developer Guide
UUID v4 vs UUID v7
Compare UUID v4 (random) vs UUID v7 (time-ordered). Understand when to use each for database keys, distributed systems, and performance.
UUID v4 uses random bits. UUID v7 embeds a timestamp for time-ordering. The choice affects database performance and sortability.
UUID v4: Random
UUID v4 is 122 random bits. Simple to generate, no coordination needed, extremely low collision probability. Downside: completely random, so database indexes fragment over time and inserts slow down.
UUID v7: Time-ordered
UUID v7 (RFC 9562) puts a Unix timestamp in the high bits. UUIDs generated close in time sort close together. This keeps database indexes compact and improves insert performance significantly.
Database performance
B-tree indexes work best with sequential or nearly-sequential keys. UUID v4 scatters inserts across the tree, causing page splits. UUID v7 appends to the end like auto-increment IDs but remains globally unique.
When to use v4
When you need unpredictability (security tokens, session IDs). When you do not control the database or index strategy. When timestamps should not be extractable from IDs.
When to use v7
Primary keys in databases (Postgres, MySQL). Distributed systems needing rough time-ordering. Log correlation where IDs should sort chronologically. Any high-write-volume system with B-tree indexes.
Summary
Generate time-ordered UUIDs with the UUID v7 Generator and compare them to v4 UUIDs from the standard UUID tool.