Programming•10 min read
UUID Generation: Complete Guide for Developers
Everything about UUIDs - versions, formats, generation methods, and best practices for unique identifiers in your applications.
By Andy Pham
UUID Generation: Complete Guide for Developers
UUIDs (Universally Unique Identifiers) are 128-bit numbers used to identify information in computer systems. This guide covers everything you need to know about UUIDs.
What is a UUID?
A UUID is a 128-bit identifier that is unique across space and time. The standard format is:
xxxxxxxx-xxxx-Mxxx-Nxxx-xxxxxxxxxxxx
Where:
- M indicates the UUID version (1-5)
- N indicates the variant (8, 9, A, or B for RFC 4122)
UUID Versions
Version 1: Time-based
- Uses current timestamp and MAC address
- Guarantees uniqueness but reveals generation time and machine
// Node.js example
const { v1: uuidv1 } = require('uuid');
console.log(uuidv1()); // e.g., 6ec0bd7f-11c0-43da-975e-2a8ad9ebae0b
Version 4: Random
- Most commonly used version
- Generated from random numbers
- Extremely low collision probability
// Browser-safe UUID v4
function uuidv4() {
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 crypto API (recommended)
function cryptoUUID() {
return crypto.randomUUID();
}
Version 5: Name-based (SHA-1)
- Deterministic - same input always produces same UUID
- Uses namespace and name hashed with SHA-1
const { v5: uuidv5 } = require('uuid');
const MY_NAMESPACE = '1b671a64-40d5-491e-99b0-da01ff1f3341';
console.log(uuidv5('Hello, World!', MY_NAMESPACE));
UUID Comparison Table
| Version | Based On | Unique | Deterministic | Use Case |
|---|---|---|---|---|
| v1 | Time + MAC | Yes | No | Distributed systems |
| v4 | Random | Practically | No | General purpose |
| v5 | Name + SHA-1 | Yes | Yes | Consistent IDs |
Collision Probability
UUID v4 collision probability is astronomically low:
Total possible UUIDs: 2^122 ≈ 5.3 × 10^36
To have a 50% chance of collision, you'd need to generate about 2.71 quintillion UUIDs.
Best Practices
1. Use Appropriate Version
- v4 for most applications
- v5 when you need deterministic IDs
- v1 for distributed systems requiring time ordering
2. Storage Considerations
-- Store as BINARY(16) for efficiency
CREATE TABLE users (
id BINARY(16) PRIMARY KEY,
name VARCHAR(255)
);
-- Or as CHAR(36) for readability
CREATE TABLE products (
id CHAR(36) PRIMARY KEY,
name VARCHAR(255)
);
3. Indexing UUIDs
- Use BINARY format for better index performance
- Consider ordered UUIDs (UUIDv7) for clustered indexes
Language Implementations
Python
import uuid
# Version 4 (random)
print(uuid.uuid4())
# Version 5 (name-based)
print(uuid.uuid5(uuid.NAMESPACE_DNS, 'example.com'))
Java
import java.util.UUID;
// Random UUID
UUID uuid = UUID.randomUUID();
// From string
UUID parsed = UUID.fromString("123e4567-e89b-12d3-a456-426614174000");
Go
import "github.com/google/uuid"
// Generate v4 UUID
id := uuid.New()
fmt.Println(id.String())
Common Mistakes to Avoid
- Don't use sequential IDs when UUIDs are needed for security
- Don't assume UUIDs are secret - they're unique, not random
- Don't store as VARCHAR(255) - use appropriate size
- Don't generate client-side for sensitive operations
Try Our UUID Tools
- UUID Generator - Generate UUIDs instantly
- Random String - Generate random strings
- Random Number - Generate random numbers
Conclusion
UUIDs are essential for modern applications requiring unique identifiers. Choose the right version for your use case, store efficiently, and follow best practices for optimal performance.