What is ULID

ULID(Universally Unique Lexicographically Sortable Identifier) is a new globally unique identifier that combine timestamp with randomness to achieve high performance and low collision while preserving the ability to be sorted lexicographically.

ULID Features

  1. gloablly unique:With a combination of timestamp and randomness to enshure that the generated ulid are globally unique.
  2. sortableulid contain a timestamp in milliseconds which allow for easy sorting and tracing of ulid by creation time.
  3. high performanceulidUlid can be generated much more quickly than uuid.
  4. compatibilityulid is 128-bit compatibility with existing uuid standards.

Applications of ULID

  • distributed systems:Ulid provide a unique identifier to guarantee generated ID remain unique for each record across different nodes in distributed systems.
  • database index:Ulid as primary key in database avoids primary key conflicts and supports sorting, enhancing query performance.
  • Logging:Ulid can act as a unique ID in logging systems,enshuring ID generated is unique while enabling efficient log sorting and retrieval.

ULID Components

Timestamp

  1. 48 bit integer
  2. UNIX-time in milliseconds。

Randomness

  1. 80 bits。

Sorting

The left-most character must be sorted first, and the right-most character sorted last (lexical order). The default ASCII character set must be used. Within the same millisecond, sort order is not guaranteed

Encoding

Crockford's Base32 is used as shown. This alphabet excludes the letters I, L, O, and U to avoid confusion and abuse.

Binary Layout and Byte Order

The components are encoded as 16 octets. Each component is encoded with the Most Significant Byte first (network byte order).

 0                   1                   2                   3
 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                      32_bit_uint_time_high                    |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|     16_bit_uint_time_low      |       16_bit_uint_random      |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                       32_bit_uint_random                      |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                       32_bit_uint_random                      |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+