· articles · 3 min read

By Ankit Jain

What is ULID (Universally Unique Lexicographically Sortable Identifier)?

Universally Unique Lexicographically Sortable Identifier are lesser known in developer community. Learn about its structure, advantages, and real-world applications. ULID combines timestamps and random components for unique and sortable identifiers, making it ideal for various use-cases.

Unique IDs are everywhere in computer science and software applications. They ensure that each entity within a system can be uniquely identified, avoiding conflicts and maintaining data integrity. One common identifier you might have heard of is the UUID called as Universally Unique Identifier. However, there’s another identifier lesser known: the ULID as Universally Unique Lexicographically Sortable Identifier. Let’s dive deep into what ULID is, how it works, and how it compares to UUID.

What is ULID?

A ULID, or Universally Unique Lexicographically Sortable Identifier, is a type of identifier designed to be unique and sortable. Created to overcome some limitations of UUIDs, ULIDs offer several advantages, particularly in scenarios where ordering by time is crucial. ULIDs consist of two main components: a timestamp and a random component.

Origin

ULID was introduced by Alizain Feerasta in 2016 to provide a more readable and sortable identifier compared to UUID. The design aimed to retain the benefits of UUID while adding new features, particularly lexicographical sorting.

Advantages of ULID

AdvantageDescription
Lexicographical SortingULIDs can be sorted as strings and the order will reflect their chronological order, useful in databases and log files.
Readability and CompactnessMore readable and compact compared to UUIDs, with a fixed length of 26 characters, making them easier to handle and display.
Collision ResistanceWith 48 bits for the timestamp and 80 bits for randomness, the probability of two ULIDs colliding is extremely low.

ULID Structure

ComponentDescription
Timestamp48-bit timestamp representing milliseconds since Unix epoch (1970-01-01T00:00:00.000Z). Allows for 281,474,976,710,655 unique timestamps, covering a span of approximately 10889 years.
Randomness80-bit random component ensuring uniqueness even if generated at the same millisecond.
FormatEncoded using Crockford’s Base32, resulting in a 26-character string. The character set includes digits (0-9) and uppercase letters (A-Z), excluding potentially confusing characters like I, L, O, and U.

How ULID Works

  1. Generation Process: Generating a ULID involves two steps: capturing the current timestamp and generating a random component. These two parts are then combined and encoded into a 26-character string.
  2. Encoding and Decoding: The ULID is encoded using Crockford’s Base32, which ensures that the resulting string is both compact and easy to read. Decoding a ULID involves reversing this process, extracting the timestamp and the random component.

Here’s a simple example in JavaScript using the ulid library:

const { ulid } = require('ulid');

console.log(ulid());
// Example output: 01F3Z0GHD0P7S9T7RK7JDJGZ8H

ULID and UUID both have unique properties. Here are some key differences and similarities between ULID and UUID. Both are suited for specific requirements, choose the one that best fits your use case.

Database Support for ULID

This table provides an overview of ULID support across popular database systems, categorized as plugin/library availability, or the necessity for custom code implementation.

DatabaseSupport for ULIDVersion
PostgreSQLPlugin/libraries available10+
MySQLCustom code8.0+
MongoDBCustom code4.0+
SQLiteCustom code3.35.0+
OracleCustom code19c+
Microsoft SQL ServerLimited support (custom code)2019+
CassandraNo native support; custom code3.11+
RedisNo native support; custom code6.0+
Neo4jCustom code4.0+
CockroachDBCustom code21.1+

[Top]

Back to Blog