UUIDv7 vs UUIDv4 vs ULID: Which ID Strategy Wins for Database Performance?
This article benchmarks UUIDv4, UUIDv7, and ULID as primary keys in a 1-million-row H2 database, showing UUIDv7 and ULID insert 3.5× faster than UUIDv4 due to timestamp-ordered B-tree writes, while explaining monotonic generation, security trade-offs, and Java implementation with uuid-creator.
Reading font
Auto-increment IDs work until you need IDs before insert, merge data from multiple databases, or expose IDs in public URLs. The common fix is UUID.randomUUID() (UUIDv4), but random keys fragment B-tree indexes: each insert targets a random page, forcing disk reads and page splits once the index exceeds memory.
Benchmark: 1 Million Rows in H2
A test inserts 1,000,000 orders into an H2 2.5.252 file database ( CACHE_SIZE=16384, 16 MB page cache) on Linux with 2×2.10 GHz Xeon vCPUs, 7 GB RAM, Java 25.0.4.1, -Xmx2g. Five median runs per ID type:
UUIDv4 : 9,377 ms, 1,028.5 MB database file
UUIDv7 : 2,680 ms, 46.1 MB
ULID : 2,561 ms, 46.1 MB
UUIDv4 takes 3.5–3.7× longer; individual runs ranged 8,972–12,878 ms (v4), 2,532–3,130 ms (v7), 2,339–2,767 ms (ULID).
Why UUIDv7 and ULID Are Faster
Both prefix a 48-bit Unix-millisecond timestamp. New IDs always sort after old ones, so inserts append to the last B-tree page — already in memory — just like auto-increment. UUIDv4 is random from bit 0, scattering inserts across the tree.
RFC 9562 defines UUIDv7 layout: 48-bit timestamp, version nibble (7), variant bits, then random bits. ULID uses the same 48-bit timestamp plus 80 random bits, encoded as 26 Crockford Base32 characters (alphabet 0-9, A-H, J, K, M, N, P-V, Z) for readability.
Generating UUIDv7 in Java
Java lacks native UUIDv7; add com.github.f4b6a3:uuid-creator:6.1.1:
UUID orderId = UuidCreator.getTimeOrderedEpoch();
assertThat(orderId.version()).isEqualTo(7);getTimeOrderedEpoch()implements RFC 9562 §6.2 monotonic generation: within the same millisecond it increments a counter in the low-order bits, guaranteeing each ID > previous ID.
Same-Millisecond Ordering
A burst of 1,000 IDs with identical timestamp:
Naive timestamp-then-random ( UuidV7.of(now)) produces randomly ordered IDs — assertThat(ids.stream().sorted().toList()).isNotEqualTo(ids) passes. UuidCreator.getTimeOrderedEpoch() keeps them sorted — assertThat(v7).isSorted() passes. Sample output shows identical timestamp prefix 01a18e73-293d and incrementing counter suffixes b5d9, b5da, b5db.
Counter is per-JVM; two servers in the same millisecond may interleave, but IDs remain close in time and thus near the index end.
Security Trade-off: Timestamp Leakage
The leading timestamp lets anyone decode creation time:
Instant createdAt = Instant.ofEpochMilli(orderId.getMostSignificantBits() >>> 16);Exposing UUIDv7 in URLs reveals order timestamps and volume — same risk as auto-increment. RFC 9562 §8 advises UUIDv4 for security-sensitive IDs. Mitigation: use UUIDv7 as primary key and a separate UUIDv4 for public exposure. Note: UUID.timestamp() only works for v1; it throws UnsupportedOperationException for v4/v7.
ULID: Shorter Text, Same Ordering
ULID ( com.github.f4b6a3:ulid-creator:5.2.4) uses monotonic generator UlidCreator.getMonotonicUlid(). Example: 01M7KHCBAHSTGW29R4QTMVQKRA (26 chars, case-insensitive). Convert for storage:
Ulid orderId = UlidCreator.getMonotonicUlid();
String text = orderId.toString();
UUID forDatabase = orderId.toUuid();Lossless 128-bit round-trip; fits in UUID column. Benchmark matches UUIDv7 because timestamp leads.
Caveat: ULID lacks version bits. When viewed as UUID, the version nibble is random (e.g., 0 or 4), so uuid.version() returns misleading values. Databases compare bytes, so storage is unaffected.
Decision Guide
Primary keys : UUIDv7 (standard UUID, monotonic, fast inserts).
Must-be-secret IDs : UUIDv4 (no timestamp leakage).
Human-readable / phone-dictated IDs : ULID (26-char Base32, no hyphens, monotonic option).
For questions about attribution, corrections or content rights, please contactand we will review it promptly.