What is a KSUID?
KSUID (K-Sortable Unique Identifier) is a 20-byte ID from Segment. The first four bytes count seconds from 13 May 2014, 16:53:20 UTC (1400000000 Unix). The other 16 bytes are random. Encoded with Base62, every KSUID is 27 characters.
Because time sits in the high bits, sorting KSUID strings is the same as sorting by creation time. That is the whole point: unique IDs that still work as a rough timeline.
Why 27 characters of Base62?
20 bytes is 160 bits. Base62 packs about 5.95 bits per character, so you need 27 characters to cover 160 bits. The string is padded on the left with 0 so the length never changes. That fixed width is what keeps lexicographic order aligned with time.
Base62 uses digits, uppercase, and lowercase. Unlike ULID, case matters. 0ujssw and 0UJSSW are different values.
KSUID vs ULID vs UUID
- KSUID: second precision, 128 random bits, 27-character Base62. Fine when one ID per second is plenty and you want extra payload entropy.
- ULID: millisecond precision, 80 random bits, 26-character Crockford Base32, case-insensitive. Pick this if you generate many IDs in the same second. Use the ULID Generator.
- UUID v7: millisecond time in the RFC 9562 layout, native
uuid columns. Use the UUID Generator.
For compact public slugs that do not need to sort, a NanoID is shorter. For 64-bit distributed IDs, see the Snowflake ID Decoder.