Skip to main content

UUID

Every developer must has dealt with UUIDs at some point in their professional life. Unique Identifiers play a critical role in almost all applications.

UUID stands for "Universally Unique Identifier," is a 128-bit number used to uniquely identify information in computer systems. The main purpose is to enable distributed systems to uniquely identify information without significant central coordination. UUIDs are used in various applications where a unique identifier is necessary, such as in database keys, session identifiers in web applications, transaction IDs in financial systems, and more. They help in scenarios where it’s important to avoid duplication across different systems that might not be directly connected or communicating with each other.

Structure​

A UUID is made up of 32 hexadecimal digits, divided into five groups separated by hyphens, in the form of 8-4-4-4-12. This translates into a total of 36 characters (32 alphanumeric characters and 4 hyphens).

Versions​

There are several versions of UUIDs, each designed for specific methods of generation:

  • Version 1: Time Based + Unique or Random Host Identifier
    Generated based on the timestamp and some identifying property of the device generating it, most commonly the MAC address of the computer generating the UUID.

  • Version 2: Distributed Computing Environment Security
    Similar to Version 1 but includes additional information about the POSIX UID/GID. Less commonly used and only a small deviation from Version 1.

  • Version 3: Name-based + MD5 Hash
    Generated by hashing a namespace identifier and a name with MD5. For the same namespace and text, the generated UUID will be the same.

  • Version 4: Psuedo-random Number Generator (PRNG)
    Generated using random or pseudo-random numbers. Most commonly implemented in modern programming languages and simplest implementation of the lot.

  • Version 5: Name-based + SHA-1 Hash
    Similar to Version 3 but uses SHA-1 hashing instead of MD5. Preferred to Version 3 as SHA-1 > MD5.

Uniqueness​

While no UUID generation method can guarantee absolute uniqueness, they are unique enough for practical purposes. The chances of the same UUID being generated twice are astronomically low, especially with Versions 4 and 5.

Although the uniqueness of the UUIDs are practically unique, using them may be an overkill in certain situations. It wouldn't make sense generating a 128-bit ID to identify data that itself may be less than 128 bits in size. if you were to generate 10 trillion UUIDs, the chance of 2 UUIDs being the same is 0.00000006 %.

The fastest and easiest way to generate a UUID is to use the standard library of your language of choice. For example, you could use the standard crypto module to generate a UUID in Javascript.

const id = crypto.randomUUID();
// 'd91648bb-dce3-4755-a238-e33687fc39ea'

While, we get a unique ID with chances of repetition astronomically low, UUIDs can be annoying to work with and we can further enhance the experience by making some small changes.

  1. Removing the hyphens (crypto.randomUUID().replace(/-/g, ""))
  2. Differentiating environments by prefixing prod_${crypto.randomUUID().replace(/-/g, "")}
  3. Using libraries like nanoid in cases where we don't need high level of collision resistance.
  4. TypeID for a type-safe, K-sortable, globally unique identifier