opyebULIDLab

KSUID: the 2014 epoch and 27 Base62 characters

KSUID · Base62 · 6 min Read time

In one line: Four bytes of epoch seconds, a 128-bit payload, Base62 ordering, and how KSUID compares with ULID and UUIDv7.

A KSUID is a 20-byte value: four bytes of seconds since 2014-05-13T16:53:20Z and sixteen bytes of random payload. Encoded in Base62 it is always 27 characters.

Why 2014

KSUID stores seconds in an unsigned 32-bit integer. Anchored at 1970 that field would overflow in 2106; anchored at 2014 it lasts another 2^32 seconds, to roughly 2150. A decoder must therefore add 1400000000 to the stored seconds to get the real time.

0ujtsYcgvSTl8PAuAdqWYSMnLOv
= 20 bytes = 4 bytes seconds + 16 bytes payload

seconds     107608047
real time   107608047 + 1400000000 = 1507608047
            = 2017-10-10 04:00:47 UTC
payload     B5A1CD34B5F99D1154FB6853345C9735 (128 bits)

Base62 ordering

The alphabet runs 0–9, A–Z, a–z, which matches ASCII order, so ordinary string comparison equals numeric comparison. Every value is padded to 27 characters, so length never disturbs the sort. Since the leading four bytes are time, lexicographic order is time order.

Compared with ULID and UUIDv7

PropertyKSUIDULIDUUIDv7
Size20 bytes16 bytes16 bytes
Text27 chars Base6226 chars Base3236 chars hex
Time unitsecondmillisecondmillisecond
Random bits1288074
Order within a unitrandom ordermonotonic optionmonotonic counter

KSUID has the strongest collision resistance with 128 random bits, and its second granularity keeps time order stable even at millions per second. Inside one second, however, creation order is not guaranteed. If you need strict order, add an application sequence or consider monotonic ULID.

Practical cautions

  • Comparison: a case-insensitive database collation breaks Base62 order. Use a binary collation.
  • Length: always 27 characters. Never strip the leading 0 padding.
  • Time exposure: the creation time is visible. For sensitive contexts, add a random prefix or a separate mapping.
  • No 2038 problem: the field is an unsigned offset in seconds, not a signed 32-bit timestamp.

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

Open ULIDLab

Frequently asked questions

KSUID or ULID — which is better?

KSUID has more collision resistance (128 random bits) and ULID has the shorter string (26 characters). Choose ULID for millisecond ordering, KSUID for second granularity with maximum random payload.

Can a KSUID string be shorter than 27 characters?

The standard encoding always pads to 27. If a library omits leading zeros, ordering can break, so normalise to 27 characters before storing.

Why does a KSUID timestamp look wrong?

Most likely the 2014 epoch offset was not added. Reading the stored 4 bytes directly gives a time before 2014. This tool adds the offset and shows both UTC and KST.

Sources

Read next