ULID Tutorial and example

This tutorial explains abuout Basic understanding of ULID.

ULID or Universally Unique Lexicographically is aunique Identifiers that have simple, sortable and unique key.

ULID generated by combinaing Current Unix timestamp and Random Data. Generated ULID has unique and does not equal to any key generated. It never generates duplicate due to timestamp factory by design.

It is similar to UUID , but there are advantagees. Sortable, Unique and does not collide with any new generated key.

The keys are generated Sorted on timestamp

Furthermore, ULIDS are encoded in base 32 format, and easily readable by humans It is used for creating unique index keys, allows to easily sort and query functions.

ULIDS by design sorted allow you to make applicaiton easorty easy It is length of 26 characters in case-insensitive Base32 format, easily readable by humans.

Please see my previous posts on GUID with examples.

What is ULID?

It is a combination of two components timestamp and Random data

timestamp is 48 bit integer, Epoch(Unix timestamp in milli seconds) Timestamp Random Data is 80 bits

Example ULID string is 01HHRHV5CC ETZY19NZEWYKB6MD.

01HHRHV5CC ETZY19NZEWYKB6MD
|----------| |----------------|
Timestamp Random Data
10 Characters 16 Characters
48 bits 80bits
  • It is 26 characters in lenght
  • 128 bit string
  • Sortable and ordered based on the timestamp
  • Case insenisitive
  • Special Characters are not allowed

ULID Usecases

Difference between ULID and UUID

ULID and UUID are used generate unique identifier keys. There are difference between

Format:

ULIDS are 26 characters in Length, combination of current timestamp and Random Data. Example ULID string is 01HHRHV5CC ETZY19NZEWYKB6MD.

UUIDS are 36 Character in length, Combination of random generated characters separated by Hyphen

Example UUID string is 01HHRHV5CC ETZY19NZEWYKB6MD.

Uniqueness: Both Provides unique key Geneation in avoding duplicates. ULID are less chance of getting duplicates compared with UUID. The reason is due to timestamp component in ULID

Length

ULID are less in size(26 characters) compared with UUID(36 characters).

In summary, ULIDS helpful for efficient sortable, and uniqueness anULIDs provide a modern, sortable, allow to use for variety of applications where UUID used.

Reference

ULID Specification🔗