UUID, ULID and Nano ID, Explained

After reading this you will know what bits each identifier format actually contains, why UUID v7 and ULID sort by time while UUID v4 does not, and how to pick the right one for a database primary key or a public URL.

What these identifiers are

An identifier is a string you generate locally that is unique enough to name a row, a file, or a request without asking a central server for the next number. That last part matters. A database auto-increment column needs a round trip to hand you 10001. A UUID you can mint on any machine, offline, and trust it will not clash with one minted on another machine a moment later.

Four formats cover almost every case. A UUID v4 is 122 random bits printed as f47ac10b-58cc-4372-a567-0e02b2c3d479. A UUID v7 puts a millisecond timestamp in the front so IDs sort by creation time. A ULID encodes the same idea in 26 Crockford base32 characters like 01ARZ3NDEKTSV4RRFFQ69G5FAV. A Nano ID is a short random string with a size you choose, V1StGXR8_Z5jdHi6B-myT by default.

When to use each one

Reach for UUID v4 when you want maximum unpredictability and do not care about ordering: session tokens, idempotency keys, anything a person should not be able to guess. Reach for UUID v7 or ULID when the identifier becomes a database primary key, because time-ordered values keep the index compact (the next section shows the arithmetic). Reach for a Nano ID when the value goes in a URL and length is the constraint: 21 characters instead of 36.

Do not use any of these as a security secret on their own. A UUID v4 is unpredictable, but it is still printed in logs, sent in URLs, and cached by proxies. Treat it as a name, not a password. And do not parse meaning out of a v4: the version and variant bits are fixed, so exactly 122 of the 128 bits carry information.

UUID v7 and ULID leak their creation time by design. The first 48 bits are a Unix millisecond timestamp anyone can decode. If a competitor should not learn when a record was created, do not expose a v7 or ULID in a public URL.

The bit layout, field by field

All UUIDs are 128 bits. Six of those bits are reserved: 4 for the version and 2 for the variant. That leaves 122 free bits in v4. The count of distinct v4 values is therefore:

N = 2^{122} \approx 5.32 \times 10^{36}

Here N is the number of possible UUID v4 values and the exponent 122 is the free bit count. For scale, 5.32 \times 10^{36} is about five undecillion.

UUID v7 spends its first 48 bits on a timestamp, keeps the 4 version bits and 2 variant bits, and fills the remaining 74 bits with randomness. ULID uses the same 48-bit timestamp followed by 80 random bits, with no version or variant field, so a ULID carries 80 random bits against v7's 74. Nano ID has no fixed structure: with the default alphabet of 64 symbols and length 21, it carries:

\log_2(64^{21}) = 21 \times 6 = 126 \text{ bits}

Each of the 64 alphabet symbols encodes 6 bits (\log_2 64 = 6), and 21 symbols give 126 bits, slightly more entropy than a v4's 122.

Structure of each format at default settings
FormatLengthTimestamp bitsRandom bitsSortable by time
UUID v436 chars0122No
UUID v736 chars4874Yes
ULID26 chars4880Yes
Nano ID21 chars0126No

Why time ordering keeps an index happy

A database B-tree index stores keys in sorted order across fixed-size pages. When you insert a random v4 key, it lands in a random page. If that page is full, the database splits it, copies half the rows, and often writes the page back to disk. Random inserts scatter these splits across the whole tree.

A time-ordered key (v7 or ULID) always sorts after the previous insert, so every new row appends to the rightmost page. One page fills, one split happens, then the next page fills. Fewer splits mean fewer dirty pages and a denser tree.

The gap is real and measurable. On common workloads, switching a primary key from random UUID v4 to time-ordered v7 can cut index write amplification by a factor of 2 to 4 and shrink the on-disk index because pages stay near full instead of half empty after random splits.

Random keys trigger roughly 3.6 times as many page splits as time-ordered keys, because inserts land all over the tree instead of appending at the end.

A worked example using the demo output

The demo button uses the field defaults and produces one value in each format. Walk through what the ULID timestamp decodes to.

Decoding the timestamp in a ULID

Take the ULID 01ARZ3NDEKTSV4RRFFQ69G5FAV. Its first 10 characters, 01ARZ3NDEK, are the 48-bit timestamp in Crockford base32.

  1. Map each character to its 5-bit value using the alphabet 0123456789ABCDEFGHJKMNPQRSTVWXYZ. So 0=0, 1=1, A=10, R=27, and so on.
  2. Concatenate the ten 5-bit groups into a 50-bit number, then keep the low 48 bits.
  3. The result is 1469918176385 milliseconds since the Unix epoch.
  4. Divide by 1000 to get 1469918176 seconds, which is 30 July 2016, 23:36:16 UTC.

The same 48-bit field sits at the front of a UUID v7 generated at the same instant, so both formats sort into creation order without you storing a separate timestamp column.

To check that decoded value in seconds against a human date, paste 1469918176 into the Unix Timestamp Converter. If you are unsure whether a number is in seconds or milliseconds, the Epoch Millis vs Seconds Detector tells you which unit it is.

Collision odds you can actually compute

The footnote on the tool says generating a billion v4 UUIDs per second for 85 years gives a 50% chance of one collision. That comes from the birthday approximation. With N = 2^{122} possible values, the number of items n that gives a 50% chance of any collision is:

n \approx 1.1774 \sqrt{N} = 1.1774 \times 2^{61} \approx 2.71 \times 10^{18}

The constant 1.1774 is \sqrt{2 \ln 2}, the standard birthday factor for a 50% probability. Dividing 2.71 \times 10^{18} by one billion per second gives about 2.71 \times 10^{9} seconds, which is roughly 85 years. The point is not that collisions never happen: it is that at any human scale they do not.

At 1 billion v4 UUIDs per second, reaching a 1% collision probability takes about 12 years; reaching 50% takes about 85 years. Fewer random bits shorten these times sharply: dropping from 122 bits to 80 bits (a ULID's random part) reduces the 50% point from 85 years to under 1 second at the same rate.

Common mistakes

Storing a UUID as text costs 36 bytes per row. Stored as a 16-byte binary column it costs 16. Across 100 million rows that is 2 GB versus 1.6 GB in the column alone, before indexes. Store the raw bytes, format on the way out.

Sorting v4 UUIDs and expecting chronological order gives nonsense, because v4 has no timestamp. If you need order, generate v7 or ULID. Truncating a Nano ID to save space silently drops entropy: cutting the default 21 characters to 10 leaves 60 bits, and 60 bits collides far sooner than you expect at scale.

Finally, do not roll your own generator with Math.random. It is not cryptographically secure and its output is predictable. This tool uses crypto.getRandomValues for every byte.

Related tools

Identifiers, timestamps, and schedules often show up together in the same system. To decode the millisecond value inside a v7 or ULID, use the Unix Timestamp Converter. To read a raw numeric timestamp of unknown unit, use the Epoch Millis vs Seconds Detector. To reason about the schedule that generates those records in the first place, the Cron Expression Parser explains any cron line and previews its next runs.

Frequently asked questions

Is a UUID v4 truly unique?

Not guaranteed, but the odds are negligible. You would need about 2.71 \times 10^{18} of them before a 50% chance of any single collision. For any real application, treat v4 as unique.

Should I use UUID v7 or ULID for a database key?

Both sort by time and both work well as keys. Choose UUID v7 if your stack, ORM, or database already has a native UUID type, because it stores as 16 bytes and validates as a UUID. Choose ULID if you want the shorter 26-character text form and do not need UUID compatibility.

Can I decode when a UUID v7 was created?

Yes. The first 48 bits are the Unix time in milliseconds. Read those bits, divide by 1000, and you have the creation second. This is a feature for sorting and a leak for privacy, so weigh both.

How long should a Nano ID be?

The default 21 characters give 126 bits, comparable to a UUID v4. If you shorten it, keep the math in mind: each character with the 64-symbol alphabet is 6 bits, so 12 characters is only 72 bits and collides much sooner in high-volume systems.

Does generating these send data anywhere?

No. Every value is created in your browser with the local secure random generator. Nothing you generate leaves your device.