🆔
← Back to Guides

UUID v4 vs UUID v7 — Which One Should You Use?

· Tags: uuid-v7, uuid-v4, rfc-9562, database, primary-key

UUID v7, standardized in RFC 9562 (May 2024), addresses the main weakness of UUID v4: poor database index performance.

UUID v4 (Random)

UUID v4 uses 122 random bits and 6 fixed bits. This guarantees uniqueness without coordination but produces IDs scattered randomly across time. When used as a B-tree primary key in a database, random IDs cause page splits and index fragmentation, degrading write performance.

UUID v7 (Time-Ordered)

UUID v7 uses a 48-bit Unix millisecond timestamp followed by random bits. The timestamp prefix makes UUIDs naturally sortable by creation time, solving the B-tree fragmentation problem. Keys are still globally unique and can be generated without coordination.

Comparison

| | UUID v4 | UUID v7 | |---|---------|---------| | Standard | RFC 9562 | RFC 9562 | | Sortable | No | Yes (by time) | | DB Performance | Poor | Good | | Unique | Yes | Yes | | Leaks timestamp | No | Yes (millisecond precision) |

Recommendation: Use UUID v7 for database primary keys and UUID v4 when you explicitly do not want to reveal creation time. Try both in our UUID generator.

UUID v4 vs UUID v7 — Which One Should You Use? - CoolTool