Understanding ULID: A Universally Unique Lexicographically Sortable Identifier
This article explains ULID, a 128‑bit identifier that combines a millisecond‑precision timestamp with high‑entropy randomness, offering lexicographic ordering, URL‑safe Base32 encoding, and a lower collision risk compared to UUIDs, along with Python usage examples and specifications.
ULID (Universally Unique Lexicographically Sortable Identifier) is an alternative to UUID that combines a timestamp with randomness, offering lexicographic ordering and a lower risk of collisions.
Why not choose UUID
UUID has five versions; version 1 requires a MAC address, version 2 uses UID/GID, version 3/5 rely on MD5/SHA‑1 hashing with a seed, and version 4 is purely random. Even UUID‑4 can collide.
ULID Features
ulid() # 01ARZ3NDEKTSV4RRFFQ69G5FAV128‑bit compatibility with UUID
1.21e+24 unique ULIDs per millisecond
Lexicographically sortable
Encoded as 26 characters (Crockford Base32)
Case‑insensitive and URL‑safe
Monotonic ordering within the same millisecond
ULID Specification
The binary layout consists of a 48‑bit timestamp (milliseconds since Unix epoch) followed by an 80‑bit random component, both encoded in network byte order.
01AN4Z07BY 79KA1307SR9X4MV3
|----------| |----------------|
Timestamp Randomness
10 chars 16 chars
48 bits 80 bitsComponents
Timestamp
48‑bit integer representing Unix time in milliseconds
Valid until year 10889
Randomness
80‑bit random value, preferably generated with a cryptographic source
Encoding
Uses Crockford’s Base32 alphabet (0123456789ABCDEFGHJKMNPQRSTVWXYZ) which omits I, L, O, U to avoid confusion.
Use Cases
Replace auto‑increment primary keys without database involvement
Distributed systems where ordered, globally unique IDs are needed
Sharding or partitioning databases by embedded timestamp
Sorting records by creation time without a separate timestamp column
Python Usage
Install the library:
pip install ulid-pyCreate a new ULID:
>> import ulid
>>> ulid.new()Convert from an existing UUID:
>> import ulid, uuid
>>> value = uuid.uuid4()
>>> ulid.from_uuid(value)Create from a timestamp or custom randomness, and retrieve the timestamp and randomness parts via timestamp() and randomness() methods.
Source code is available at https://github.com/ahawker/ulid .
Selected Java Interview Questions
A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.