UUID Generator: Create Unique Identifiers

A deep tutorial on UUID versions, generation methods, and best practices for distributed systems

Developer ToolsApril 13, 20269 min read

Understanding UUIDs: The Universal Standard for Unique IDs

A Universally Unique Identifier (UUID) is a 128-bit identifier standardized by the IETF in RFC 4122. It is represented as a 36-character string in the format xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx, where each x is a hexadecimal digit. UUIDs are designed to be unique across both space and time — no coordination between systems is required to generate one, making them ideal for distributed databases, microservices, and any system where independent nodes need to create identifiers without central authority.

The UUID standard defines five versions (v1 through v5), each with a different generation strategy. Understanding these versions is essential for choosing the right one for your use case. In this tutorial, we'll break down each version, explain the generation mechanism, and provide practical guidance on when and how to use them.

UUID Structure: Anatomy of a 128-Bit Identifier

Before diving into versions, let's understand the internal structure. A UUID's 128 bits are divided into fields:

6ba7b810-9dad-11d1-80b4-00c04fd430c8
│       │    │    │    │
│       │    │    │    └── node (48 bits): MAC address or random
│       │    │    └─────── clock_seq (14 bits)
│       │    └──────────── version + variant (4+2 bits)
│       └───────────────── time_mid (16 bits)
└───────────────────────── time_low (32 bits)

The version field (bits 48-51) indicates which UUID version generated the identifier. The variant field (bits 64-65) specifies the UUID variant — RFC 4122 UUIDs always use variant 10xx (binary), which appears as 8, 9, a, or b in the first character of the third group.

UUID Version 1: Time-Based Identifiers

How It Works

UUID v1 combines a 60-bit timestamp (representing 100-nanosecond intervals since October 15, 1582 — the Gregorian reform date), a 14-bit clock sequence (to handle clocks running backwards or multiple generators on the same node), and a 48-bit node identifier (typically the machine's MAC address). The result is a time-ordered identifier that naturally sorts chronologically.

Example UUID v1: 6ba7b810-9dad-11d1-80b4-00c04fd430c8
                 └──┬──┘ └┬┘
                  time   version=1

Advantages

Problems

UUID Version 3: Name-Based (MD5)

How It Works

UUID v3 generates identifiers by computing the MD5 hash of a namespace UUID concatenated with a name string. The result is a deterministic identifier — the same namespace and name always produce the same UUID. This is invaluable for content-addressable systems where you need reproducible identifiers.

UUID v3 = MD5(namespace_UUID + name)
Example: UUID v3 for "example.com" in DNS namespace:
         9073926b-929f-31c2-abc6-f2aa9e8b5ef8

Common predefined namespaces include 6ba7b810-9dad-11d1-80b4-00c04fd430c8 (DNS), 6ba7b811-9dad-11d1-80b4-00c04fd430c8 (URL), 6ba7b812-9dad-11d1-80b4-00c04fd430c8 (OID), and 6ba7b814-9dad-11d1-80b4-00c04fd430c8 (X.500).

When to Use

Use UUID v3 when you need stable, reproducible identifiers derived from names. Examples include generating consistent IDs for API resources based on their URL, creating cache keys from content hashes, or building content-addressable storage systems. The MD5 dependency is acceptable here because the purpose is deterministic generation, not cryptographic security.

UUID Version 4: Random Identifiers

How It Works

UUID v4 uses purely random bits for all 122 non-version/variant bits. It's the simplest and most common UUID variant — just generate 16 random bytes, set the version bits to 0100 (v4) and the variant bits to 10, and format the result.

Example UUID v4: 550e8400-e29b-41d4-a716-446655440000
                         └┬┘
                      version=4

Advantages

The Database Index Problem

The major drawback of UUID v4 is that random identifiers fragment B-tree indexes. When you insert randomly-ordered values into a B-tree, the database must constantly split and rebalance pages, leading to poor insert performance and index bloat. In PostgreSQL, a table with UUID v4 primary keys can be 50-80% larger than one with sequential keys, and insert throughput drops significantly.

UUID Version 5: Name-Based (SHA-1)

How It Works

UUID v5 is identical in concept to v3 but uses SHA-1 instead of MD5 as the hashing algorithm. The namespace + name input is hashed with SHA-1, and the first 128 bits of the result form the UUID.

UUID v5 = SHA1(namespace_UUID + name)[0:128]
Example: UUID v5 for "example.com" in DNS namespace:
         0d9b1e3b-2a3c-5f7d-9e1b-4c6d8a0f2e3b

v3 vs v5: Which to Choose?

Since both are deterministic, the choice comes down to the hashing algorithm. SHA-1 is stronger than MD5, but since we're truncating to 128 bits, the practical collision resistance difference is negligible. UUID v5 is generally preferred for new systems because it uses the stronger hash function, but if you need compatibility with existing v3 identifiers, stick with v3.

Use Cases and Best Practices

Database Primary Keys

For database primary keys, consider these trade-offs:

API Resource Identifiers

UUIDs are excellent as public-facing API resource IDs because they're non-sequential (preventing enumeration attacks), globally unique (no coordination needed across services), and opaque (no information leakage). Never use auto-incrementing integers as public IDs — they enable attackers to estimate your data volume and scrape all records sequentially.

Distributed Systems

In microservices architectures, UUIDs eliminate the need for a central ID generation service. Each service can generate IDs independently, reducing system complexity and eliminating a single point of failure. Snowflake IDs (Twitter's approach) are an alternative that offers time-ordering with less storage overhead (64 bits vs 128 bits), but require coordination with a central service for machine ID assignment.

Generate UUIDs with RiseTop's UUID Generator

Need a UUID right now? Our free Online UUID Generator creates UUID v4 identifiers instantly in your browser. Generate single or bulk UUIDs, copy them with one click, and choose between uppercase, lowercase, and no-hyphen formats. No registration required.

Frequently Asked Questions

What is the difference between UUID v4 and UUID v7?UUID v4 is randomly generated and has no ordering guarantee, making it poor for database indexes. UUID v7 is time-ordered (prefix contains a Unix timestamp), so newly generated UUIDs sort chronologically, making them much better for database primary keys.
Can UUIDs collide?Theoretically yes, but the probability is astronomically low. For UUID v4 (122 random bits), you'd need to generate approximately 2.71 quintillion UUIDs before having a 50% chance of a collision. In practice, collisions are considered impossible.
When should I use UUID v5 instead of v4?Use UUID v5 when you need deterministic identifiers — the same input name and namespace always produces the same UUID. This is useful for content-addressable systems, consistent caching keys, and scenarios where reproducibility matters.
Are UUIDs secure enough for use as tokens?UUID v4 uses 122 bits of randomness, which provides sufficient entropy for most non-security-critical token use cases. However, for authentication tokens, session IDs, or API keys, dedicated cryptographically secure random generators with additional entropy are recommended.
What is a UUID namespace?A UUID namespace provides context for UUID v3 and v5 generation. It's itself a UUID that categorizes the name being hashed. Common predefined namespaces include DNS (for domain names), URL (for web addresses), OID (for ISO object identifiers), and X500 (for distinguished names).

Related Articles