Universally Unique Identifiers (UUIDs) are 128-bit numbers used to uniquely identify information in computer systems. They are essential for distributed systems, database design, and modern software architecture. This comprehensive guide covers everything you need to know about UUIDs.
Table of Contents
- Key Takeaways
- What is a UUID?
- UUID Structure and Format
- UUID Versions Explained
- Code Examples
- UUID Best Practices
- Common Use Cases
- Frequently Asked Questions
- Conclusion
Key Takeaways
- Universality: UUIDs are designed to be unique across space and time without central coordination.
- 128-bit: A UUID is a 128-bit number, typically displayed as 32 hexadecimal characters with hyphens.
- Multiple Versions: UUID v4 (random) is most common; UUID v1 (time-based) is useful for sorting.
- Collision Probability: The chance of generating duplicate UUID v4s is astronomically low (1 in 2^122).
- Database Keys: UUIDs are excellent for distributed database primary keys.
- Privacy: UUID v4 reveals no information about creation time or location.
Need to generate UUIDs quickly? Try our free online tool that supports multiple versions and formats.
Generate UUIDs Now - Free Online UUID Generator
What is a UUID?
A UUID (Universally Unique Identifier), also known as GUID (Globally Unique Identifier) in Microsoft systems, is a 128-bit identifier that is unique across all devices and all time. UUIDs are standardized by RFC 4122 and are used extensively in software development.
The key properties of UUIDs are:
- Uniqueness: Each UUID is virtually guaranteed to be unique
- Decentralization: No central authority is needed to generate UUIDs
- Portability: UUIDs can be generated on any system without coordination
UUID Structure and Format
A UUID is typically represented as 32 hexadecimal digits, displayed in five groups separated by hyphens:
xxxxxxxx-xxxx-Mxxx-Nxxx-xxxxxxxxxxxx
For example:
550e8400-e29b-41d4-a716-446655440000
The structure breaks down as:
| Part | Length | Description |
|---|---|---|
| time_low | 8 chars | Low 32 bits of time |
| time_mid | 4 chars | Middle 16 bits of time |
| time_hi_and_version | 4 chars | High 12 bits of time + 4 bits version |
| clock_seq_hi_and_reserved | 2 chars | Clock sequence high + variant |
| clock_seq_low | 2 chars | Clock sequence low |
| node | 12 chars | 48-bit node identifier |
The M position indicates the UUID version (1-5), and the N position indicates the variant (typically 8, 9, a, or b for RFC 4122 UUIDs).
UUID Versions Explained
UUID v1 (Time-based)
UUID v1 is generated using the current timestamp and the MAC address of the computer.
Structure:
- 60-bit timestamp (100-nanosecond intervals since October 15, 1582)
- 14-bit clock sequence (handles clock adjustments)
- 48-bit node ID (usually MAC address)
Advantages:
- Sortable by creation time
- Guaranteed uniqueness on a single machine
- Can extract creation timestamp
Disadvantages:
- Reveals MAC address (privacy concern)
- Reveals creation time
- Requires access to MAC address
Example:
6ba7b810-9dad-11d1-80b4-00c04fd430c8
^
Version 1
UUID v4 (Random)
UUID v4 is generated using random or pseudo-random numbers. This is the most commonly used version.
Structure:
- 122 random bits
- 4 bits for version (0100)
- 2 bits for variant (10)
Advantages:
- Simple to generate
- No privacy concerns
- No hardware dependencies
- Works in any environment
Disadvantages:
- Not sortable by time
- Slightly higher collision probability (still negligible)
Example:
f47ac10b-58cc-4372-a567-0e02b2c3d479
^
Version 4
Other UUID Versions
| Version | Name | Description |
|---|---|---|
| v2 | DCE Security | Rarely used, includes POSIX UID/GID |
| v3 | Name-based (MD5) | Generated from namespace + name using MD5 |
| v5 | Name-based (SHA-1) | Generated from namespace + name using SHA-1 |
| Nil | Nil UUID | All zeros: 00000000-0000-0000-0000-000000000000 |
Code Examples
JavaScript
// Using crypto.randomUUID() (modern browsers and Node.js 14.17+)
const uuid = crypto.randomUUID();
console.log(uuid); // e.g., "f47ac10b-58cc-4372-a567-0e02b2c3d479"
// Manual UUID v4 generation (fallback)
function generateUUIDv4() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
const r = Math.random() * 16 | 0;
const v = c === 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
}
// Using uuid library
// npm install uuid
import { v4 as uuidv4, v1 as uuidv1 } from 'uuid';
const randomUUID = uuidv4(); // Random UUID
const timeUUID = uuidv1(); // Time-based UUID
Python
import uuid
# Generate UUID v4 (random)
random_uuid = uuid.uuid4()
print(random_uuid) # e.g., f47ac10b-58cc-4372-a567-0e02b2c3d479
# Generate UUID v1 (time-based)
time_uuid = uuid.uuid1()
print(time_uuid)
# Generate UUID v5 (name-based with SHA-1)
namespace = uuid.NAMESPACE_DNS
name_uuid = uuid.uuid5(namespace, 'example.com')
print(name_uuid)
# Convert to different formats
uuid_str = str(random_uuid) # String format
uuid_hex = random_uuid.hex # Without hyphens
uuid_bytes = random_uuid.bytes # 16 bytes
uuid_int = random_uuid.int # 128-bit integer
Java
import java.util.UUID;
public class UUIDExample {
public static void main(String[] args) {
// Generate UUID v4 (random)
UUID randomUUID = UUID.randomUUID();
System.out.println(randomUUID);
// Generate UUID from string
UUID parsedUUID = UUID.fromString("550e8400-e29b-41d4-a716-446655440000");
// Get UUID components
long mostSigBits = randomUUID.getMostSignificantBits();
long leastSigBits = randomUUID.getLeastSignificantBits();
int version = randomUUID.version(); // Returns 4 for v4
int variant = randomUUID.variant(); // Returns 2 for RFC 4122
// Convert to string without hyphens
String uuidWithoutHyphens = randomUUID.toString().replace("-", "");
}
}
UUID Best Practices
1. Choose the Right Version
| Use Case | Recommended Version |
|---|---|
| General purpose | UUID v4 |
| Need time-ordering | UUID v1 or ULID |
| Deterministic generation | UUID v5 |
| Privacy-sensitive | UUID v4 |
2. Database Considerations
-- PostgreSQL: Use native UUID type
CREATE TABLE users (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
name VARCHAR(100)
);
-- MySQL: Use BINARY(16) for better performance
CREATE TABLE users (
id BINARY(16) PRIMARY KEY,
name VARCHAR(100)
);
3. Performance Tips
- Index carefully: UUID indexes can be large; consider partial indexes
- Use binary storage: Store as BINARY(16) instead of VARCHAR(36)
- Consider alternatives: For high-performance needs, consider ULID or Snowflake IDs
4. Security Considerations
- Never use UUIDs as security tokens alone
- UUID v1 leaks timestamp and MAC address
- UUID v4 is cryptographically random when using secure generators
Common Use Cases
-
Database Primary Keys
- Distributed databases without coordination
- Merge data from multiple sources
- Avoid sequential ID guessing
-
Session Identifiers
- Web session tokens
- API request correlation IDs
-
File and Resource Naming
- Uploaded file names
- Temporary file identifiers
-
Distributed Systems
- Message queue identifiers
- Microservice request tracing
- Event sourcing
-
API Design
- Resource identifiers in REST APIs
- Idempotency keys
Frequently Asked Questions
What is the probability of UUID collision?
For UUID v4, the probability of collision is approximately 1 in 2^122 (about 5.3 x 10^36). To have a 50% chance of collision, you would need to generate about 2.71 quintillion UUIDs. In practical terms, UUID collisions are virtually impossible.
Should I use UUID v1 or v4?
Use UUID v4 for most applications due to its simplicity and privacy. Use UUID v1 only when you need time-based ordering or need to extract the creation timestamp.
Are UUIDs good for database primary keys?
Yes, UUIDs are excellent for distributed databases. However, consider these trade-offs:
- Pros: No coordination needed, globally unique, good for sharding
- Cons: Larger than integers (16 bytes vs 4-8 bytes), random UUIDs can cause index fragmentation
How do I generate UUIDs without code?
You can use online tools like our Free UUID Generator to quickly generate UUIDs in various formats without writing any code.
What is the difference between UUID and GUID?
UUID and GUID are essentially the same thing. GUID (Globally Unique Identifier) is Microsoft's term for UUID. Both follow the same RFC 4122 standard.
Conclusion
UUIDs are a fundamental tool in modern software development, providing a reliable way to generate unique identifiers without central coordination. Understanding the different versions and their trade-offs helps you make informed decisions for your specific use case.
Quick Summary:
- Use UUID v4 for most general-purpose applications
- Use UUID v1 when you need time-based ordering
- Use UUID v5 for deterministic, reproducible identifiers
- Consider storage format and indexing strategies for databases
Ready to generate UUIDs? Try our free online tool: