Skip to content

UUID Version Differences and Alternatives

5 min read
Published:
UUID Version Differences and Alternatives

TLDR: UUIDs are 128-bit values used to generate unique identifiers. If you’re not curious, just use v4 and move on.

What is UUID?

UUID (Universally Unique Identifier) consists of 128-bit values used to create unique identifiers. These values are typically generated randomly, and the probability of collision is extremely low. UUIDs are usually displayed in 5 groups of 32 hexadecimal digits. For example: cff46ea8-eb27-4c96-ad76-c2a21da6095d.

Fun Fact: You can generate a random UUIDv4 by typing crypto.randomUUID() in your browser console.

UUID Versions

The differences between versions depend on how the UUID is generated and which algorithm is used. To determine a UUID’s version, look at the first digit of the 3rd section. For example, the UUID cff46ea8-eb27-4c96-ad76-c2a21da6095d is version 4.

v1

UUIDv1

  • Generated using timestamp and MAC address. Since the MAC address is constant, UUIDs generated on the same machine can be very similar.

v2

  • Reserved for security identifiers, but details are not well known.

v3

  • Generated using MD5 hashes of provided data. DNS and URLs are typically used for hashing.

v4

UUIDv4

  • Completely randomly generated. Generally the most commonly used version. The probability of UUID collision is extremely low.

v5

  • Generated using SHA-1 hashes of provided data. Similar to v3, data like DNS or URLs is used.

v6

  • Uses the same data as v1, but unlike v1, the order of these components has been rearranged to facilitate sorting.

v7

  • Generated with timestamps and random data. Offers the ability to sort by creation time. (Can be useful when sorting in databases.)

v8

  • A fully customizable version; it only requires version and variant fields to follow certain rules, leaving the rest to the user.

Nil UUID

  • A UUID with all bits set to 0. Typically used as an empty UUID. 00000000-0000-0000-0000-000000000000

Max UUID

  • A UUID with all bits set to 1. Typically used as the maximum UUID. ffffffff-ffff-ffff-ffff-ffffffffffff

UUID Variants

Variants determine how the UUID is structured. This is the variant we saw in the previous diagrams. UUIDs have 4 variants:

  • Variant 0-7: UUID is backward compatible with old computer systems from the 1980s.
  • Variant 8-b: UUID conforms to the “RFC 4122” standard. The most commonly used UUID type today.
  • Variant c and d: UUID is compatible with legacy Windows systems.
  • Variant e and f: Reserved for future UUID versions.

Alternatives

ULID (Universally Unique Lexicographically Sortable Identifier)

  • Has a structure similar to UUID. Contains a 48-bit timestamp and 80 bits of random data. At 128 bits, it’s the same length as UUID and uses 26-character Base32 encoding, making it shorter than UUID. Additionally, being sortable is one of its advantages. For example: 01E2XZ6VXH3Z2ZJ9XKJZQYHJ7Z

KSUID (K-Sortable Unique Identifier)

  • The first 32 bits are a timestamp, making them sortable by creation time. The remaining part consists of a random number. It’s 160 bits long and uses 20-character Base62 encoding. GitHub Repo For example: 2o2PVhy4DF1bTH2b9hA0CwPkzWk

TSID (Time Sortable ID)

  • TSID is designed to be short and readable. It has time-based sorting capability. It’s a 64-bit number and uses 13-character Base32 encoding. For example: 0DXBQ9H279R05

Snowflake ID

  • Developed by Twitter, Snowflake IDs are 64-bit numbers (used as 63-bit signed integers).

    • The first 41 bits contain a timestamp in milliseconds.
    • The next 10 bits are used as a machine ID to prevent collisions.
    • 12 bits serve as a sequential counter for generating multiple IDs within the same millisecond.

Being shorter than UUID and sortable are among its advantages. It’s also used by Discord. For examples and converter, check here.

MongoID (MongoDB ObjectID)

  • MongoDB ObjectID, or simply MongoID, is the default identifier type used in MongoDB databases. It consists of 12 bytes (96 bits), each 8 bits, and is typically represented as a 24-character hexadecimal string. For example: 651d56b4f437a342a4f0e692

    • First 4 bytes: A timestamp showing when the ObjectID was created. It represents seconds since Unix epoch. This makes ObjectIDs sortable by creation time.
    • Next 5 bytes: A random value generated once per process. This value is unique per machine and process, preventing the same ObjectID from being generated on different machines.
    • Last 3 bytes: An incrementing counter initialized with a random starting value. Ensures uniqueness of multiple ObjectIDs within the same second.

Nano ID

  • NanoID is an ID generator for JavaScript that produces small (considerably larger than UUID), secure, URL-friendly unique string IDs. It has a performance-oriented and minimal structure. GitHub Repo For example: 1StGXR8_Z5jdHi6B-myT

Cuid2

  • Cuid2 is a system that generates secure, low-collision next-generation unique IDs. It’s optimized for horizontal scalability and performance, offering various advantages over traditional methods like UUID/GUID. Not recommended for sorting operations. GitHub Repo For example: tz4a98xxat96iws9zmbrgj3a

Summary

If we don’t need sorting and just want to generate a random identifier, we can continue using UUIDv4. However, when sorting might be needed, we can turn to alternatives like UUIDv7 or Snowflake. This article is based on my own research and may contain incomplete or incorrect information. For more detailed information, please refer to the sources.

Thanks and References

I wanted to research this just to clear my mind and out of curiosity. Special thanks to @eserozvataf and @9ssi7 who shared their knowledge with me after a casual tweet I posted, illuminating this article 🙏. Also thanks to @baskindev and @Keleesssss who contributed to improving the article with their feedback after publication 🙏.

References