· articles · 3 min read
By Ankit JainWhat 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
Advantage | Description |
---|---|
Lexicographical Sorting | ULIDs can be sorted as strings and the order will reflect their chronological order, useful in databases and log files. |
Readability and Compactness | More readable and compact compared to UUIDs, with a fixed length of 26 characters, making them easier to handle and display. |
Collision Resistance | With 48 bits for the timestamp and 80 bits for randomness, the probability of two ULIDs colliding is extremely low. |
ULID Structure
Component | Description |
---|---|
Timestamp | 48-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. |
Randomness | 80-bit random component ensuring uniqueness even if generated at the same millisecond. |
Format | Encoded 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
- 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.
- 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.
Database | Support for ULID | Version |
---|---|---|
PostgreSQL | Plugin/libraries available | 10+ |
MySQL | Custom code | 8.0+ |
MongoDB | Custom code | 4.0+ |
SQLite | Custom code | 3.35.0+ |
Oracle | Custom code | 19c+ |
Microsoft SQL Server | Limited support (custom code) | 2019+ |
Cassandra | No native support; custom code | 3.11+ |
Redis | No native support; custom code | 6.0+ |
Neo4j | Custom code | 4.0+ |
CockroachDB | Custom code | 21.1+ |