What is UUID?

UUID (Universally Unique Identifier) is a 128-bit identifier that is guaranteed to be unique across all space and time. UUIDs are represented as 32 hexadecimal digits, displayed in five groups separated by hyphens (8-4-4-4-12 format).

Quick Facts

Full NameUniversally Unique Identifier
Created1980s (standardized in RFC 4122 in 2005)
SpecificationOfficial Specification

How It Works

UUIDs are generated using algorithms that combine various sources of uniqueness such as timestamps, random numbers, and hardware addresses. There are several versions: Version 1 uses timestamp and MAC address, Version 4 uses random numbers (most common), Version 5 uses namespace and name with SHA-1 hashing. The probability of generating duplicate UUIDs is so low that it's considered practically impossible. UUIDs are widely used in distributed systems where unique identifiers are needed without central coordination. UUID v7 (RFC 9562, 2024) introduces time-ordered UUIDs that combine the benefits of UUIDs with natural sorting by creation time. The first 48 bits encode a Unix timestamp in milliseconds, followed by random bits. This makes v7 ideal for database primary keys where chronological ordering improves index performance and query efficiency.

Key Characteristics

  • 128-bit length providing 2^128 possible values
  • Standardized format: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
  • Multiple versions for different use cases (v1, v4, v5, etc.)
  • Can be generated without central authority
  • Collision probability is negligible for practical purposes
  • Case-insensitive (uppercase and lowercase are equivalent)

Common Use Cases

  1. Database primary keys in distributed systems
  2. Session identifiers in web applications
  3. File and resource naming
  4. Transaction IDs in microservices
  5. Device identification

Example

loading...
Loading code...

Frequently Asked Questions

What is the difference between UUID and GUID?

UUID (Universally Unique Identifier) and GUID (Globally Unique Identifier) are essentially the same thing - both are 128-bit identifiers with the same format. GUID is the term used by Microsoft in Windows and .NET, while UUID is the term used in most other contexts and in the official RFC specification. They are fully compatible and interchangeable.

Which UUID version should I use?

UUID v4 (random) is the most commonly used and recommended for general purposes due to its simplicity and strong uniqueness. UUID v7 (time-ordered) is ideal for database primary keys because it maintains chronological ordering and improves index performance. UUID v1 (timestamp + MAC address) exposes machine information so it's less common now. UUID v5 is used for deterministic generation from namespace and name.

Can UUID collisions actually happen?

While theoretically possible, UUID v4 collisions are practically impossible. With 122 random bits, you would need to generate about 2.71 quintillion UUIDs to have a 50% chance of collision. To put this in perspective, generating 1 billion UUIDs per second would take about 85 years to reach that probability. For all practical purposes, UUIDs can be considered unique.

How do I generate a UUID in different programming languages?

In JavaScript: crypto.randomUUID() or use the 'uuid' npm package. In Python: import uuid; uuid.uuid4(). In Java: UUID.randomUUID(). In PHP: use Ramsey\Uuid\Uuid or uniqid() for simpler needs. In C#: Guid.NewGuid(). In Go: use the google/uuid package. Most modern languages have built-in or standard library support for UUID generation.

Should I use UUID or auto-increment ID for database primary keys?

Both have trade-offs. UUIDs are better for distributed systems (no coordination needed), data merging, and security (IDs aren't guessable). Auto-increment IDs are more space-efficient, human-readable, and have better index performance with B-tree indexes. UUID v7 offers a middle ground with time-ordering for better index performance. Consider using UUIDs for distributed systems and auto-increment for single-database applications.

Related Tools

Related Terms

Related Articles