ULID Generator
Generate sortable, timestamp-prefixed ULIDs with human-readable time breakdown.
Advertisement
Loading...
What this tool does
ULID (Universally Unique Lexicographically Sortable Identifier) is a 128-bit identifier designed to be sortable, URL-safe, and more compact than UUID. It consists of a 48-bit timestamp (milliseconds since Unix epoch) followed by 80 bits of randomness, encoded as 26 characters using Crockford's base32 alphabet (no I, L, O, U to avoid confusion).
The timestamp prefix means ULIDs sort lexicographically in the same order as their creation time, making them excellent for database primary keys that benefit from sequential inserts. The 80 random bits provide the same collision resistance as UUID v4, but the encoded string is shorter and easier to read.
Usage Example
// ULID structure
01ARZ3NDEKTSV4RRFFQ69G5FAV
^^^^^^^^^^ ^^^^^^^^^^^^^
time (10) random (16)
48 bits 80 bits
// Generate in JavaScript
import { ulid } from "ulid";
const id = ulid();
// "01ARZ3NDEKTSV4RRFFQ69G5FAV"
// Decode the timestamp
const decoded = decodeTime("01ARZ3NDEKTSV4RRFFQ69G5FAV");
// 1469918176385 (milliseconds since epoch)Common Edge Cases
- ULIDs are time-ordered, so two IDs created in the same millisecond differ only in the random portion. If you generate many per millisecond, consider adding entropy from a counter.
- The 48-bit timestamp overflows in the year 10889. While this is not a practical concern, some implementations may exhibit unexpected behavior near the boundary.
- Crockford's base32 omits I, L, O, U to reduce visual ambiguity. The lowercase form also omits lowercase L for the same reason.
- ULIDs generated on different machines with skewed clocks will sort by the (potentially incorrect) timestamp, not the true creation order.
- The 80 random bits give ~1.2e24 possible values per millisecond, so collisions are essentially impossible in practice.
FAQ
- Should I use ULID or UUID v4?
- Use ULID when you want sortable identifiers or better database index locality with sequential inserts. Use UUID v4 when you need maximum tool compatibility or do not need time ordering.
- Is ULID a drop-in replacement for UUID?
- No. ULID uses Crockford base32 and is case-insensitive, while UUID uses hex and is case-insensitive in spec but often treated case-sensitively in practice. Databases and APIs that expect UUID format will reject ULIDs.
- Can ULIDs replace auto-increment IDs?
- Yes, in most cases. ULIDs are safe to expose publicly (unlike sequential IDs which leak business metrics) and sort chronologically. The only downside is 16 bytes vs 4-8 bytes for an integer.
Advertisement