Sub topic 8
Time-ordered IDs: UUID v7 and ULID
This chapter assumes you know why random UUID v4 keys scatter inserts across a B-tree index. It also assumes you can read bit shifts in Python. Time-ordered IDs keep the "no coordination" property of random IDs and fix most of the index problem.
The idea
Put a timestamp in the most significant bits and random bits after it. Any two IDs created in different milliseconds then compare in creation order. Nodes still don't talk to each other, because the random tail keeps IDs from different nodes apart.
UUID v7 (RFC 9562) and ULID both use this layout:
- 48 bits: Unix time in milliseconds.
- The remaining 80 bits: mostly random. UUID v7 spends 6 of them on the version and variant fields, so 74 bits are random.
UUID v7 by hand
This generator builds the layout directly.
import os
import time
import uuid
def uuid7():
# 48-bit Unix timestamp in milliseconds, then 80 random bits.
ts_ms = time.time_ns() // 1_000_000
value = (ts_ms << 80) | int.from_bytes(os.urandom(10), "big")
# Set the 4-bit version field (bits 79-76) to 7.
value &= ~(0xF << 76)
value |= 7 << 76
# Set the 2-bit variant field (bits 63-62) to 0b10.
value &= ~(0x3 << 62)
value |= 0b10 << 62
return uuid.UUID(int=value)
print(uuid7())The timestamp is shifted left by 80 bits so it fills the top 48 bits. The two masking steps overwrite the random bits at the version and variant positions. The result is a normal 128-bit UUID that any UUID column or library accepts. Python 3.14 and newer also ship uuid.uuid7(), so use the library function when you have it.
ULID
A ULID has the same 48-bit timestamp and 80 random bits. It differs in the text form: 26 characters of Crockford Base32, such as 01JD5X3Q8M4V7R2K9T6B0N1ZAE. Plain string comparison of two ULIDs gives creation order. The text is shorter than the 36-character UUID format. ULID has no version or variant bits, so all 80 trailing bits are random. It is not an official UUID, so some databases have no native type for it. You store it as a string or convert it to 16 bytes.
| Property | UUID v4 | UUID v7 | ULID |
|---|---|---|---|
| Size | 128 bits | 128 bits | 128 bits |
| Random bits | 122 | 74 | 80 |
| Sorts by time | No | Yes (per millisecond) | Yes (per millisecond) |
| Native UUID column | Yes | Yes | No |
| Coordination needed | None | None | None |
What you gain
Because new IDs are larger than older ones, inserts land at the right edge of the index. Recent pages stay in memory, page splits are rarer, and the index is smaller than with v4 keys. You also get a rough "created at" ordering for pagination without a separate timestamp column.
What you do not get
- No order inside one millisecond. Two IDs from the same millisecond sort by their random bits. The same is true across nodes, since clocks disagree by some amount. The order is approximate, so don't treat it as a strict happens-before relation.
- Weaker collision margin than v4. With 74 random bits, two IDs created in the same millisecond collide with probability about 2⁻⁷⁴ per pair. Only IDs from the same millisecond can collide, so this is still negligible in practice. Do not compare it to v4's 122 bits as if both applied to the whole ID set.
- Clock dependence. If a clock jumps backwards, new IDs sort before old ones. Duplicates remain very unlikely, but ordering breaks. The RFC allows a monotonic counter in the random field to keep order within one process. Many libraries offer it.
- Leaked creation time. Anyone holding an ID can read when it was created. The security chapter returns to this.
Exercise
Two services create UUID v7 values in the same millisecond. Can they collide, and will the IDs sort by true creation order?
Show answer
A collision is possible only if all 74 random bits match, which has probability about 2^-74 per pair, so it is negligible. The sort order is not reliable. Within one millisecond the order is decided by random bits, and clock skew between the two services can also reorder IDs across milliseconds.