A UUID is 128 bits. The version (4 bits) says how the rest was made, and the variant (2 bits, 10) says the value follows RFC 9562, which replaced RFC 4122 in 2024.
How it works#
Generating. Pick a type and a count from 1 to 1,000:
- UUIDv4 is 122 random bits with the version set to
4and the variant to10. The tool callscrypto.randomUUID()where the browser has it, and otherwise sets those bits itself on bytes fromcrypto.getRandomValues(). - UUIDv7 (RFC 9562 §5.7) starts with the current Unix time in milliseconds as a 48-bit big-endian number, then version
7, 12 random bits (rand_a), variant10and 62 random bits (rand_b). - ULID has the same 48-bit millisecond timestamp followed by 80 random bits. It is written as 26 characters of Crockford base32, which leaves out
I,L,OandUso the IDs are hard to misread.
Both time-ordered generators are monotonic. When two IDs fall in the same millisecond, the second keeps the timestamp and increases the random part: a v7 by a random step from 1 to 2^32 (RFC 9562 advises against a step of 1 where IDs should be hard to guess), a ULID by exactly 1, as its spec defines. A clock that goes backwards keeps the last timestamp, so a batch is always sorted.
Inspecting. Paste a UUID (hyphens, braces and urn:uuid: are optional) or a ULID. The tool reports the version, the variant, the timestamp for v1, v6, v7 and ULID, and the clock sequence and node for v1 and v6. It flags the Nil and Max UUIDs and values with another variant or an undefined version, and shows the same 128 bits in the other notation. Nothing you paste or generate leaves the page.
A worked example#
The RFC’s own UUIDv7 test vector is filled in for you: 017F22E2-79B0-7CC3-98C4-DC0C0C07398F. The first 12 hex digits, 017F22E279B0, are 1645557742000 milliseconds, which is 22 February 2022 at 19:22:22 UTC (2:22:22 PM in New York). The 7 that starts the third group is the version. The 9 that starts the fourth group is binary 1001; its first two bits, 10, are the variant. The tool’s tests check every field against the RFC’s table, along with the v1 and v6 vectors, which encode the same moment in 100-nanosecond steps since 15 October 1582.
UUID primary keys in Postgres and Rails#
A uuid takes 16 bytes, twice a bigint. The bigger difference is where new keys go in the primary key’s B-tree index. Random v4 keys land on random leaf pages. Each insert dirties a page that may not be in memory, splits leave pages half full, and the first change to a page after a checkpoint writes the whole page to the WAL. Time-ordered keys (v7, ULID, or a sequence) all go to the right edge of the index. The same few pages stay hot, and splits mostly happen at the end.
In Rails, id: :uuid creates the column with a database default:
create_table :orders, id: :uuid do |t|
t.references :customer, type: :uuid, foreign_key: true
t.timestamps
end
The Rails guide says gen_random_uuid() is assumed when you pass no :default. That function produces v4. It is built into Postgres 13 and later; before 13 it came from the pgcrypto extension. Postgres 18 adds uuidv7(), which you can name as the default:
create_table :orders, id: :uuid, default: -> { "uuidv7()" } do |t|
# ...
end
On an older Postgres, generate the key in Ruby instead. SecureRandom.uuid_v7 exists from Ruby 3.3:
class Order < ApplicationRecord
before_create { self.id ||= SecureRandom.uuid_v7 }
end
With v4 keys, Order.first and Order.last sort by a random value. Set self.implicit_order_column = "created_at" to fix that. With v7 keys, ordering by id follows creation order to the millisecond.
The trade-off is privacy. A v7 or a ULID tells anyone who sees it when the row was created. Paste one here and you can read the time. If that matters, for example in a public URL, use v4. If you store ULIDs, put them in a uuid column (16 bytes) rather than as 26-character text, and convert at the edges.
UUID keys also help job systems: idempotent AI workers in Rails keys its idempotency gate on a record UUID that exists before the job is enqueued.
Limitations#
- Ordering is guaranteed only within one generator: one batch, or one page session. Two machines generating v7 IDs in the same millisecond produce IDs that sort in random order within that millisecond.
- Postgres’s
uuidv7()uses some of the random bits for a sub-millisecond timestamp instead of a counter. Both are valid v7 and decode to the same millisecond here. - ULIDs from one millisecond differ by one, so one reveals its neighbours. That is the ULID spec’s design; use v7 or v4 where it matters.
- v2 (DCE Security), v3, v5 and v8 are identified, but no time or name is decoded: v2 has no reliable time, v3 and v5 are one-way hashes, and v8’s layout is up to the vendor.
- The bulk limit is 1,000 per batch.
Frequently asked questions
Should I use UUIDv7 or UUIDv4 for a primary key?
For a primary key in Postgres, v7 is usually the better choice: new rows get increasing keys, so inserts land at the right edge of the B-tree index instead of on random pages. Use v4 when the ID must not reveal when the row was created, because a v7 carries its creation time to the millisecond.
Is a ULID the same as a UUIDv7?
They have the same idea and the same 48-bit millisecond timestamp, but different bits. A ULID uses all 80 remaining bits for randomness and is written in 26 Crockford base32 characters. A UUIDv7 gives up 6 of those bits to the version and variant fields and is written as 36 hex characters with hyphens. A ULID stored in a uuid column is 128 bits like any UUID, but it is not a valid v7.
Are IDs generated in the same millisecond still in order?
Yes, within one batch or page session. When two IDs share a millisecond, the later one keeps the timestamp and increases the random bits: a v7 adds a random step (RFC 9562 §6.2 Method 2), and a ULID adds one, as the ULID spec says. A batch of 1,000 is therefore already sorted. IDs from separate machines are only ordered to the millisecond.
How random are the generated IDs?
All randomness comes from the browser's Web Crypto API: crypto.randomUUID for v4 where the browser has it, otherwise crypto.getRandomValues, which also supplies every v7 and ULID. Math.random is never used. Nothing is sent anywhere, and the page makes no network requests.