opyebULIDLab

ULID structure: why 26 characters sort by time

ULID · Crockford Base32 · 7 min Read time

In one line: The 48-bit timestamp, 80 random bits, Crockford Base32, monotonic ULIDs and the details that trip people up.

A ULID encodes 128 bits as 26 characters of Crockford Base32. The first 48 bits are a Unix millisecond timestamp and the last 80 bits are random. Sort strings of equal length lexicographically and you get time order.

How 128 bits fit into 26 characters

Base32 uses 5 bits per character. 26 × 5 = 130 bits, two more than needed, so the first character is always in the range 0–7 and the two spare bits are fixed at zero. That rule keeps every ULID exactly 26 characters and makes lexicographic sorting stable.

The alphabet is the digits 0–9 plus A–Z without I, L, O and U, 32 characters in all. Those letters are removed because people confuse them. Lenient decoders also read I and L as 1 and O as 0; this tool accepts lowercase input and normalises it to uppercase.

Bit layout and a worked decode

01ARZ3NDEKTSV4RRFFQ69G5FAV
└──── 48-bit timestamp ────┘└──────── 80 random bits ────────┘

timestamp 1469922850259 ms
= 2016-07-30 23:54:10.259 UTC
= 2016-07-31 08:54:10.259 KST
random 0xD6764C61EFB99302BD5B (80 bits)

The timestamp covers 1970 through the year 10889, so range is not a practical concern. The 80 random bits are smaller than the 122 bits of UUIDv4, however, so at billions of IDs per second you must calculate collision risk separately.

Monotonic ULID

Several ULIDs made in the same millisecond get different random tails and may sort out of creation order. A monotonic ULID increments the 80-bit random value by one inside the same millisecond, preserving order, and carries into the next millisecond when all 80 bits are used. Ordering is guaranteed in one process but not across nodes, so use per-node prefixes or a separate sequence when several generators run at once.

Practical cautions

  • Case: store uppercase. Mixed case breaks lexicographic ordering.
  • Length: always 26 characters. Dropping a leading zero breaks both sorting and decoding.
  • Character validation: a string containing I, L, O or U is not a canonical ULID; reject it on input if you need strictness.
  • Column type: CHAR(26) or VARCHAR(26) with uppercase values, and check that the collation is binary for columns you sort.

Unlike a UUID column, a ULID is readable in logs and needs no quoting in URLs or CSV. The cost is that standard UUID tools cannot consume it, so convert at system boundaries if needed.

Decode an ID you just made and inspect its bit structure.

Open ULIDLab

Frequently asked questions

How is a ULID different from UUIDv7?

Both start with a 48-bit millisecond timestamp, but the encoding and random width differ. A ULID is 26 characters with 80 random bits; UUIDv7 is a 16-byte binary value with 74 random bits. Both sort by time.

Can I store ULIDs in lowercase?

You can, but be consistent. Mixing cases makes string order disagree with time order. Normalise to one form before storing, comparing or sorting.

Is a ULID safer against collisions than a UUID?

A ULID has 80 random bits and UUIDv4 has 122, so UUIDv4 has the lower collision probability. The ULID advantage is time ordering and readability, not more entropy.

Sources

Read next