UUID Generator
UUID Generator
Use the UUID generator to quickly create various versions of unique identifiers, with support for batch generation and multiple output formats.
What is UUID?
UUID (Universally Unique Identifier) is a 128-bit number used to uniquely identify information in computer systems. UUID is also known as GUID (Globally Unique Identifier).
UUID Characteristics
- Uniqueness: Extremely low probability of generating duplicate UUIDs in distributed systems
- Standardization: Follows RFC 4122 standard
- Cross-platform: Can be used across different operating systems and programming languages
- Decentralized: No central coordination authority required
UUID Application Scenarios
- Database primary keys: As primary keys in distributed databases
- File identification: Identify files in file systems
- Session management: Session identifiers in web applications
- Message queues: Unique identifiers for messages
- API interfaces: Identifiers for API requests and responses
UUID Version Details
UUID v1 (Time and Node Based)
Format: xxxxxxxx-xxxx-1xxx-yxxx-xxxxxxxxxxxx
Characteristics:
- Based on current time and node ID (usually MAC address)
- Contains timestamp information, can be sorted by time
- May leak MAC address information
Generation Algorithm:
// Pseudocode example
function generateUUIDv1() {
const timestamp = Date.now();
const nodeId = getMACAddress();
const clockSeq = random16Bit();
return formatUUID(timestamp, clockSeq, nodeId);
}
UUID v3 (Namespace and MD5 Based)
Format: xxxxxxxx-xxxx-3xxx-yxxx-xxxxxxxxxxxx
Characteristics:
- Based on namespace UUID and MD5 hash of name
- Same namespace and name always produce the same UUID
- Suitable for scenarios requiring deterministic UUIDs
Generation Algorithm:
function generateUUIDv3(namespace, name) {
const namespaceBytes = parseUUID(namespace);
const nameBytes = Buffer.from(name, 'utf8');
const hash = crypto
.createHash('md5')
.update(Buffer.concat([namespaceBytes, nameBytes]))
.digest();
return formatUUIDv3(hash);
}
UUID v4 (Random Generation)
Format: xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx
Characteristics:
- Completely randomly generated
- Most commonly used UUID version
- Contains no meaningful information
Generation Algorithm:
function generateUUIDv4() {
const bytes = crypto.randomBytes(16);
bytes[6] = (bytes[6] & 0x0f) | 0x40; // Version 4
bytes[8] = (bytes[8] & 0x3f) | 0x80; // Variant
return formatUUID(bytes);
}
UUID v5 (Namespace and SHA-1 Based)
Format: xxxxxxxx-xxxx-5xxx-yxxx-xxxxxxxxxxxx
Characteristics:
- Based on namespace UUID and SHA-1 hash of name
- More secure than v3 (uses SHA-1 instead of MD5)
- Suitable for scenarios requiring deterministic UUIDs
UUID NIL (Null UUID)
Format: 00000000-0000-0000-0000-000000000000
Characteristics:
- All bits are 0
- Used to represent "no UUID" or "unknown UUID"
- Used as default value in database design
Predefined Namespaces
Standard Namespace UUIDs
- DNS namespace:
6ba7b810-9dad-11d1-80b4-00c04fd430c8
- URL namespace:
6ba7b811-9dad-11d1-80b4-00c04fd430c8
- OID namespace:
6ba7b812-9dad-11d1-80b4-00c04fd430c8
- X.500 DN namespace:
6ba7b814-9dad-11d1-80b4-00c04fd430c8
Usage Examples
// Generate UUID v3 using DNS namespace
const dnsNamespace = '6ba7b810-9dad-11d1-80b4-00c04fd430c8';
const uuid = generateUUIDv3(dnsNamespace, 'example.com');
// Result: 5df41881-3aed-3515-88a7-2f4a814cf09e
API Interface
Generate Single UUID
// Generate UUID v4
const uuid = generateUUID('v4');
// Generate UUID v1
const uuid = generateUUID('v1');
// Generate UUID v3
const uuid = generateUUID('v3', {
namespace: '6ba7b810-9dad-11d1-80b4-00c04fd430c8',
name: 'example.com',
});
Batch Generate UUIDs
// Batch generate UUIDs
const uuids = generateMultipleUUIDs('v4', 10);
// Generate UUIDs of different versions
const mixedUUIDs = generateMultipleUUIDs(['v1', 'v4', 'v5'], 5);
UUID Validation
// Validate UUID format
const isValid = validateUUID('550e8400-e29b-41d4-a716-446655440000');
// Get UUID version
const version = getUUIDVersion('550e8400-e29b-41d4-a716-446655440000');
Performance Considerations
Generation Speed
- v4 (Random): Fastest, approximately 100,000 UUIDs/second
- v1 (Time): Medium, approximately 50,000 UUIDs/second
- v3/v5 (Hash): Slower, approximately 10,000 UUIDs/second
Memory Usage
- Each UUID occupies 16 bytes
- Pay attention to memory usage when batch generating
Concurrency Safety
- v4 uses cryptographically secure random number generators
- v1 requires ensuring uniqueness of clock sequence
Best Practices
Choosing UUID Version
- General purpose: Use UUID v4
- Need time sorting: Use UUID v1
- Need determinism: Use UUID v3 or v5
- Database primary keys: Recommend using UUID v4
Storage Optimization
-- UUID storage in PostgreSQL
CREATE TABLE users (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
name VARCHAR(255)
);
-- Create index
CREATE INDEX idx_users_id ON users(id);
Performance Optimization
// Use object pool to reduce memory allocation
const uuidPool = new Array(1000).fill(null).map(() => generateUUIDv4());
// Use Web Workers for batch generation
const worker = new Worker('uuid-worker.js');
worker.postMessage({ type: 'generate', count: 1000 });
Common Questions
Q: What's the difference between UUID and GUID?
A: UUID and GUID are essentially the same, just different names. UUID is the RFC standard term, GUID is Microsoft's term.
Q: Can UUIDs repeat?
A: Theoretically possible, but extremely low probability. The probability of v4 UUID repetition is approximately 2.71 × 10^-36.
Q: How to choose UUID version?
A: Choose based on requirements:
- Need randomness: v4
- Need time information: v1
- Need determinism: v3 or v5
Q: Do UUIDs affect database performance?
A: May affect, because UUIDs are larger than integer primary keys. Recommendations:
- Use B-tree indexes
- Consider using UUID v1 (time-sorted)
- Use integer primary keys when appropriate
Related Tools
Technical Specifications
RFC Standards
- RFC 4122: UUID specification
- RFC 4122 Section 4.1.3: UUID version definitions
Programming Language Support
- JavaScript:
crypto.randomUUID()
(Node.js 14.17+) - Python:
uuid
module - Java:
java.util.UUID
- C#:
System.Guid
Last updated: January 20, 2024