Munchable's support tickets, their messages, reward ledger rows and product revisions all take their primary keys from the application, not the database. The key is a ULID: a 48-bit millisecond timestamp followed by 80 random bits, written in Crockford base32, 26 characters in all. Because the timestamp is the prefix, IDs sort by creation time as plain strings, which makes them good keyset-pagination tiebreakers and pleasant to read in a log.
There is a well-known package for this. We did not install it, because the whole thing fits in a file smaller than most licence headers:
const ENC = '0123456789ABCDEFGHJKMNPQRSTVWXYZ'; export function ulid(now: number = Date.now()): string { let ts = ''; let t = now; for (let i = 0; i < 10; i++) { ts = ENC[t % 32] + ts; t = Math.floor(t / 32); } const bytes = crypto.getRandomValues(new Uint8Array(16)); let rand = ''; for (let i = 0; i < 16; i++) rand += ENC[bytes[i] % 32]; return ts + rand; } The timestamp is peeled off five bits at a time from the right. The random half takes sixteen random bytes and keeps the low five bits of each. I wanted to know whether the short version was also a correct one, so I ran it against five properties.
Five checks
| Question | Result |
|---|---|
| Is the output always 26 characters? | yes |
| Does the timestamp decode back to the input? | yes, including the largest 48-bit value |
| One ID per millisecond for 1,000 milliseconds: ascending? | 1,000 of 1,000 |
| 1,000,000 IDs in one millisecond: any random-half duplicates? | 0 |
| Is the random alphabet uniform? | 320,000 draws over 32 symbols gave counts between 9,654 and 10,294 against an expected 10,000 |
The last one is the check that earns its keep for code written by hand. Taking a random byte modulo 32 would be biased if 256 did not divide evenly by 32, but it does, exactly eight times, so every symbol is equally likely. The same line with a modulo of 36 or 62 would be wrong in a way no unit test would notice.
The row that is not 100 per cent
The sixth check is the one I actually cared about. Call the generator twice in the same millisecond and ask whether the second ID sorts after the first:
same ms, successive ids ascending: 50005 of 100000 Fifty per cent, give or take. Within one millisecond the timestamp prefix is identical, so order is decided by the random half, which is a coin flip. That is not a defect in the 22 lines. It is what the ULID specification calls out: monotonic ordering inside a millisecond is an optional extra, done by incrementing the previous random value, and this generator does not do it.
It does mean the promise has a resolution. The comment above our ticket creation says the ID is a ULID "so the admin queue's keyset tiebreak sorts in creation order". That is true to the millisecond. The queue sorts by the time of the last message and breaks ties by ID, so two tickets with the same millisecond timestamp come out in a random order relative to each other. For a support queue that is a non-event. For something where "A before B" is a correctness property, such as an event log, it would be a bug, and the fix there is the monotonic variant, which keeps the last timestamp and random value in module state and adds one when the clock has not moved.
Messages show the same pattern in a smaller way. A thread is read with ORDER BY created_at, id. Two messages given an identical created_at fall back to the ID, which only orders them correctly if they are a millisecond apart. In practice a reply takes a human a good few seconds, so it works, and the rule I would write on the wall is: ULID order is real across milliseconds and random inside one.
Two things that are fine
- Overflow. The timestamp field holds 48 bits, so it runs out in the year 10889. Passing a larger number wraps to all zeros. I checked, and I am not worried.
- Where the randomness comes from.
crypto.getRandomValuesis the Web Crypto API, available in the Node runtime and at the edge, so the same file runs in a route handler and in a script without a package.
The generator is the least visible code in the product. You will not find an ID on a page, but you can see what sits on top of this kind of plumbing: the home page for the product itself, and the about page for who is behind it.

