Fundamentals 7 min read

Understanding ULID: Features, Specification, and Python Usage

This article explains what ULID (Universally Unique Lexicographically Sortable Identifier) is, compares it with UUID, outlines its timestamp‑based and random components, describes its Base32 encoding and binary layout, and shows how to generate and manipulate ULIDs in Python using the ulid‑py library.

IT Architects Alliance
IT Architects Alliance
IT Architects Alliance
Understanding ULID: Features, Specification, and Python Usage

ULID (Universally Unique Lexicographically Sortable Identifier) is a 128‑bit identifier that combines a millisecond‑precision timestamp with 80 bits of randomness, offering lexicographic ordering and collision‑free generation without the drawbacks of UUID versions that rely solely on random numbers or MAC addresses.

Compared with the five UUID versions, ULID provides deterministic ordering, a more compact 26‑character string (using Crockford's Base32), URL‑safe characters, and case‑insensitivity, while still being compatible with the 128‑bit space of UUIDs.

Key ULID characteristics :

48‑bit timestamp (UNIX time in milliseconds) valid until year 10889.

80‑bit randomness, preferably generated with cryptographic quality.

Lexicographically sortable string representation.

Encoded with Crockford's Base32 alphabet (0123456789ABCDEFGHJKMNPQRSTVWXYZ) that omits I, L, O, U.

Network‑byte order (big‑endian) binary layout.

The binary layout consists of a 32‑bit high timestamp, a 16‑bit low timestamp, and three 32‑bit random blocks, totaling 128 bits.

Typical use cases include replacing auto‑increment primary keys, providing globally unique IDs without a database round‑trip, and enabling time‑based sharding or partitioning of data stores.

Python usage with ulid‑py :

Installation:

pip install ulid-py

Generate a new ULID:

>>> import ulid
>>> ulid.new()
<ulid('01bjqe4qthmfp0s5j153xcfsp9')>

Create a ULID from an existing UUID:

>>> import ulid, uuid
>>> value = uuid.uuid4()
>>> ulid.from_uuid(value)
<ulid('09gf8a5zrn9p1rydvxv52vbahs')>

Create a ULID from a specific timestamp:

>>> import datetime, ulid
>>> ulid.from_timestamp(datetime.datetime(1999, 1, 1))
<ulid('00tm9hx0008s220a3pwsfvnfeh')>

Access ULID components:

>>> u = ulid.new()
>>> u.timestamp()   # returns the 48‑bit timestamp
>>> u.randomness()   # returns the 80‑bit random part

For more details, see the official repository: github.com/ahawker/ulid .

distributed systemsPythonUUIDULIDidentifieruniqueness
IT Architects Alliance
Written by

IT Architects Alliance

Discussion and exchange on system, internet, large‑scale distributed, high‑availability, and high‑performance architectures, as well as big data, machine learning, AI, and architecture adjustments with internet technologies. Includes real‑world large‑scale architecture case studies. Open to architects who have ideas and enjoy sharing.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.