ULID (Universally Unique Lexicographically Sortable Identifier)

Description

A ULID (Universally Unique Lexicographically Sortable Identifier) is a unique identifier designed to be sortable by creation time, offering a blend of uniqueness and timestamp-based ordering. It is represented as a 26-character string encoded in base32, which makes it compact, URL-safe, and case-insensitive.

Structure

A ULID consists of:

  • Timestamp: The first 10 characters encode a timestamp with millisecond precision.
  • Randomness: The last 16 characters provide a unique random component to ensure global uniqueness.

Usages

  1. Database Primary Keys: ULIDs are often used as primary keys in databases, providing unique and sortable identifiers.
  2. Distributed Systems: They prevent ID collisions in distributed systems and maintain chronological order.
  3. Event Sourcing: ULIDs uniquely identify and sort events based on their creation time.
  4. Microservices: Useful for unique request IDs, tracing logs, and correlating events across services.
  5. APIs: Commonly used in APIs for unique resource identification due to their readability and guaranteed uniqueness.

ULIDs offer a practical and efficient solution for generating unique identifiers across various applications, combining the advantages of UUIDs with the added benefit of chronological sorting.