Universally unique lexicographically sortable identifier
| Acronym | ULID |
|---|---|
| No. of digits | 26 |
| Example | 01AN4Z07BY79KA1307SR9X4MV3 |
| Website | https://github.com/ulid/spec |
A Universally unique lexicographically sortable identifier (ULID) is a unique identifier implementation created as an alternative to UUID to address some perceived shortcomings of UUIDs.
Specifically:
- UUID is a relatively inefficient means of encoding 128 bits of randomness,
- UUID v1/v2 is impractical in many environments because it requires access to a unique and stable MAC address,
- UUID v3/v5 requires a unique seed and produces randomly distributed IDs that can cause fragmentation in certain data structures, and
- UUID v4 provides no other information than randomness, which can also cause fragmentation in many certain structures.
Properties of ULID
- Like UUID, ULID values are 128-bits in length, and are compatible with UUID,
- ULIDs are based on a millisecond timestamp, giving them a monotonic sort order,
- ULIDs are lexicographically sortable, with older ULIDs having smaller values,
- Canonically encoded as a 26-character string, as opposed to the 36-character UUID,
- Uses Crockford's base32, which provides some advantages:
- It is designed to be more readable by excluding the letters I, L, and O to avoid confusion with digits
- It is explicitly case-insensitive
- It contains no special characters, making it URL safe.
Components
Like UUID, a ULID is a 128-bit value. They are composed of two primary components, summarized below.
| Component | Bits | Characters | Description |
|---|---|---|---|
| Timestamp | 48 | 10 | UNIX time in milliseconds (safe until 10889 AD) |
| Randomness | 80 | 16 | Cryptographically secure source of randomness (if available) |
Canonical string representation
A sample ULID, 01AN4Z07BY79KA1307SR9X4MV3, can be decomposed into two parts as follows:
01AN4Z07BY 79KA1307SR9X4MV3 |----------| |----------------| Timestamp Randomness 48bits 80bits
Because the leftmost bits are constructed from a millisecond resolution timestamp, lexicographic sortability is guaranteed for ULIDs created at different times. Within the same millisecond, sort order is not guaranteed.
Monotonicity
When multiple ULIDs are generated within the same millisecond, an effort is made to preserve sort order. If the same millisecond is detected, the random component is incremented by 1 bit in the least significant bit position (with carrying). For example:
import { monotonicFactory } from 'ulid'
const ulid = monotonicFactory()
// Assume that these calls occur within the same millisecond
ulid() // 01BX5ZZKBKACTAV9WEVGEMMVRZ
ulid() // 01BX5ZZKBKACTAV9WEVGEMMVS0
External links
This article "Universally unique lexicographically sortable identifier" is from Wikipedia. The list of its authors can be seen in its historical and/or the page Edithistory:Universally unique lexicographically sortable identifier. Articles copied from Draft Namespace on Wikipedia could be seen on the Draft Namespace of Wikipedia and not main one.
