What Is ULID: Understanding The Universally Unique Lexicographically Sortable Identifier Format.

ULID (Universally Unique Lexicographically Sortable Identifier) is a new generation of unique identifier that is quickly gaining popularity among developers. It is a 128-bit identifier that can be used as a universally unique identifier (UUID) or a time-based identifier (TID). ULID is designed to be human-readable, compact, and easy to generate.


What makes ULID special is that it is lexicographically sortable, meaning that it can be sorted in a predictable way based on the order in which it was generated. This makes ULID ideal for use in databases and data stores where sorting is a common operation. Unlike traditional UUIDs, ULID can be sorted in a natural order, making it easier to locate and manage records.


ULID consists of two parts: a 48-bit timestamp and an 80-bit random number. The timestamp provides the ULID with a unique ordering, while the random number provides the ULID with its universality. The timestamp is generated using the number of milliseconds since the Unix epoch (January 1, 1970), while the random number is generated using a cryptographically secure random number generator.


The structure of ULID makes it easy to generate and encode using various programming languages. For example, in Python, the ULID library can be used to generate ULIDs. In JavaScript, the ulid library can be used. There are also libraries for generating ULIDs in other programming languages, such as Go and Rust.

Differences between ULID vs UUID:

ULID and UUID are both unique identifier formats, but there are some important differences between them:


Structure: ULID consists of two parts - a 48-bit timestamp and an 80-bit random number, while UUID consists of four parts - a 32-bit time_low, a 16-bit time_mid, a 16-bit time_high_and_version, and a 128-bit clock_seq_and_node.


Sorting: ULID is lexicographically sortable, meaning it can be sorted based on the order in which it was generated. UUID, on the other hand, is not sortable and does not have any inherent order.


Generation: ULID uses a combination of a timestamp and a random number generator to generate a unique identifier, while UUID can be generated using various methods, such as random number generation or MAC addresses.


Size: ULID is smaller in size than UUID, which can be an advantage when using it in a database or data store where storage space is limited.


Human Readability: ULID is more human-readable than UUID, as it consists of 26 base32 characters. UUID, on the other hand, consists of 36 hexadecimal characters.


Collision: Both ULID and UUID are designed to be unique, but ULID has a lower probability of collision as it uses a longer random number and a timestamp.


In conclusion, ULID and UUID are both useful for generating unique identifiers, but ULID offers some advantages over UUID, such as sortability, smaller size, and better human-readability. The choice between ULID and UUID will depend on the specific requirements of your project.