Complete Guide to UUID: Version Differences, Generation Principles & Use Cases

In software development, UUID (Universally Unique Identifier) is an indispensable foundational component. Whether it's database primary keys, session identifiers, message IDs in distributed systems, or file naming, UUID plays an important role. However, many developers are not very clear about the differences and applicable scenarios of various UUID versions (v1, v3, v4, v5, v7), and often casually choose a version to use. This article will deeply analyze UUID standard specifications, generation principles of each version, pros and cons comparisons, and best practices in different scenarios to help you make more informed technical choices.

I. What is UUID?

UUID (Universally Unique Identifier) is a 128-bit number used to uniquely identify information in computer systems. Its design goal is to generate unique identifiers in distributed systems without a central coordinating authority.

1.1 UUID Format

A standard UUID consists of 32 hexadecimal digits, divided into five segments by hyphens, in the form:

xxxxxxxx-xxxx-Mxxx-Nxxx-xxxxxxxxxxxx

Where M represents the version number and N represents the variant number. The total length of a UUID is 36 characters (including hyphens).

1.2 UUID Versions

UUID has multiple versions, each using a different generation algorithm:

  • Version 1: Based on time and MAC address
  • Version 3: Based on namespace and MD5 hash
  • Version 4: Based on random numbers
  • Version 5: Based on namespace and SHA-1 hash
  • Version 7: Based on time and randomness (new proposal)

1.3 Uniqueness Guarantee

UUID uniqueness is based on the following principles:

  • Sufficiently large address space: 128 bits can represent approximately 3.4×10Âģâļ different values
  • Reasonable generation algorithm: combining time, space, and randomness ensures uniqueness
  • Collision probability is extremely low in practical applications, negligible

ðŸ’Ą Fun Fact:Generating 1 billion v4 UUIDs per second for 100 years results in approximately 50% collision probability. In other words, in normal usage scenarios, the probability of a UUID collision is much lower than the probability of you being hit by a meteorite.

II. UUID v1 - Time and MAC Address Based

UUID v1 is the earliest UUID version. It combines timestamp and MAC address to generate unique identifiers.

2.1 Structure Composition

UUID v1 consists of the following parts:

  • Timestamp low 32 bits
  • Timestamp mid 16 bits
  • Version number (4 bits) + timestamp high 12 bits
  • Variant number (2 bits) + clock sequence (14 bits)
  • Node (48 bits, usually MAC address)

2.2 Advantages

  • Guarantees temporal ordering, can be sorted by time
  • Absolutely unique for UUIDs generated on the same machine
  • Can extract timestamp and MAC address information from UUID

2.3 Disadvantages

  • Leaks MAC address, may bring privacy and security issues
  • May produce duplicates in distributed systems (clock rollback, etc.)
  • Not completely random, may be predictable

III. UUID v4 - Random Number Based

UUID v4 is currently the most commonly used UUID version. It is entirely based on random number generation.

3.1 Structure Composition

The structure of UUID v4 is very simple:

  • 122 random bits
  • 4 version bits (fixed as 0100)
  • 2 variant bits (fixed as 10)

3.2 Advantages

  • Completely random, unpredictable, high security
  • Simple implementation, no external information needed
  • Does not leak any information, protects privacy
  • The most universal and widely supported UUID version

3.3 Disadvantages

  • No time information, cannot be sorted by time
  • Theoretically possible collisions (though extremely low probability)
  • May cause index fragmentation when used as primary key in databases

🔐 Security Tip:When generating v4 UUIDs, always use cryptographically secure random number generators (CSPRNG). In browsers, use crypto.getRandomValues(); in Node.js, use crypto.randomUUID() or crypto.randomBytes(). Don't use Math.random() because it doesn't provide enough randomness.

IV. UUID v3 and v5 - Namespace Based

Both UUID v3 and v5 are deterministic UUIDs based on namespace and name. The same input always produces the same output.

4.1 Basic Principle

v3 and v5 are generated the same way, differing in the hash algorithm used:

  • v3 uses MD5 hash algorithm
  • v5 uses SHA-1 hash algorithm

Generation process: concatenate namespace UUID and name, then hash, take the first 128 bits and set version and variant bits

4.2 Standard Namespaces

RFC 4122 defines several standard namespaces:

DNS:  6ba7b810-9dad-11d1-80b4-00c04fd430c8
URL:  6ba7b811-9dad-11d1-80b4-00c04fd430c8
OID:  6ba7b812-9dad-11d1-80b4-00c04fd430c8
X.500: 6ba7b814-9dad-11d1-80b4-00c04fd430c8

V. UUID v7 - New Generation Time-Ordered UUID

UUID v7 is a new UUID version proposal (RFC 9562) that combines the advantages of time ordering and randomness.

5.1 Structure Composition

Structure of UUID v7:

  • 48-bit timestamp (millisecond Unix time)
  • 4 version bits (0111)
  • 12-bit random number A
  • 2 variant bits (10)
  • 62-bit random number B

5.2 Advantages

  • Time-ordered, can be sorted by time, suitable for database indexes
  • Contains sufficient randomness, unpredictable
  • Does not leak MAC address, protects privacy
  • Excellent performance in distributed systems

5.3 Disadvantages

  • Relatively new, library support in various languages is still improving
  • Requires special handling during clock rollback

🚀 Trend:UUID v7 is rapidly gaining popularity. It combines the time-ordering of v1 with the randomness of v4, solving the database index fragmentation problem. If you're designing a new system, strongly consider using v7 as your primary key solution.

VI. Version Comparison & Selection Guide

Let's compare the characteristics of each UUID version to help you choose.

į‰đ性 v1 v3 v4 v5 v7
æ—ķé—ī有嚏 ✅ ❌ ❌ ❌ ✅
随朚性 éƒĻ分 ❌ ✅ ❌ ✅
įĄŪåŪšæ€§ ❌ ✅ ❌ ✅ ❌
隐ᧁåŪ‰å…Ļ ❌ ✅ ✅ ✅ ✅
åđŋæģ›æ”Ŋ持 ✅ ✅ ✅ ✅ å‘åą•äļ­

6.2 How to Choose

  • Most scenarios: use v4, simple, secure, universal
  • Need time ordering: use v7, new generation standard
  • Need to extract time and MAC: use v1 (note privacy)
  • Need determinism: use v5 (more secure than v3)

VII. Best Practices & Common Issues

When actually using UUID, there are some best practices worth following.

7.1 Storage Optimization

  • Use binary (16 bytes) storage instead of string (36 bytes)
  • When used as primary key in database, consider using ordered UUID (v1 or v7)
  • Case-insensitive, can uniformly convert to lowercase

7.2 Generation Considerations

  • Use cryptographically secure random number generators (for v4)
  • Pay attention to clock rollback issues (for v1 and v7)
  • Use mature libraries, don't implement yourself

7.3 Common Misconceptions

  • Myth: UUIDs never repeat — Fact: collision probability is extremely low but not zero
  • Myth: v4 is insecure — Fact: v4 uses sufficient random entropy and is secure
  • Myth: String format UUID has poor performance — Fact: impact is negligible in most scenarios

VIII. Using TudoSi UUID Generator

TudoSi Tools provides convenient UUID generation functionality:

🆔

UUID Generator

v1 / v3 / v4 / v5 / v7 multi-version support

TudoSi Tools' UUID generator supports multiple versions of UUID generation, including v1 (time+MAC), v3 (namespace+MD5), v4 (random), v5 (namespace+SHA-1), v7 (time+random). Supports batch generation, custom namespaces, case format conversion. All generation is completed locally in the browser, data is never uploaded to the server.

Multi-version support Batch generation Custom namespace Local execution
Use Now →

IX. Summary

UUID is one of the most basic and important tools in software development. Understanding the characteristics and applicable scenarios of each version can help us make the most appropriate choices in different scenarios. v4 has become the preferred choice for most scenarios due to its simplicity, security, and universality; v7, as a new generation standard, excels in scenarios requiring temporal ordering; v3 and v5 are suitable for special scenarios requiring determinism.

In practical applications, in addition to choosing the right UUID version, we also need to pay attention to best practices such as storage optimization and generation security. With the development of technology, UUID v7 is being adopted by more and more systems. It combines the temporal ordering of v1 and the randomness of v4, making it a very promising new standard.

TudoSi Tools' UUID generator supports multiple versions of UUID generation, which can help you quickly generate UUIDs in various formats, and is a valuable assistant during development and testing.