Generate UUIDs Instantly
Use our free UUID generator to create v4 and v7 UUIDs for your projects.
Open UUID Generator →What is a UUID?
A Universally Unique Identifier (UUID) is a 128-bit label used to uniquely identify information in computer systems. UUIDs are standardized by RFC 4122 and RFC 9562, ensuring they can be generated independently without a central authority while maintaining a negligible probability of duplication.
UUID Format
UUIDs are typically represented as 32 hexadecimal digits, displayed in five groups separated by hyphens:
xxxxxxxx-xxxx-Mxxx-Nxxx-xxxxxxxxxxxx Example: 550e8400-e29b-41d4-a716-446655440000 Where: - M indicates the UUID version (1-7) - N indicates the variant (8, 9, a, or b for RFC 4122)
UUID vs GUID: What's the Difference?
The terms UUID and GUID (Globally Unique Identifier) are often used interchangeably, and for good reason—they refer to the same concept. The main difference is historical and contextual:
- UUID: The term used in most programming languages and the official RFC specification
- GUID: Microsoft's terminology, commonly used in Windows and.NET ecosystems
Both follow the same format and serve the same purpose. Use whichever term your platform prefers.
UUID Versions Explained
| Version | Generation Method | Best For |
|---|---|---|
| v1 | Timestamp + MAC address | When you need time-ordering and have unique hardware |
| v3 | MD5 hash of namespace + name | Deterministic IDs from names (legacy) |
| v4 | Random | General purpose, most common choice |
| v5 | SHA-1 hash of namespace + name | Deterministic IDs from names (preferred over v3) |
| v6 | Reordered v1 for sorting | When you need v1 features with better sorting |
| v7 | Unix timestamp + random | ⭐ Modern choice: sortable + random |
UUID v4: The Classic Choice
UUID v4 is the most widely used version. It generates 122 random bits (6 bits are used for version and variant), providing approximately 5.3×10^36 possible unique values. The probability of collision is astronomically low.
// JavaScript UUID v4 generation
function generateUUIDv4() { return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => { const r = Math.random() * 16 | 0; const v = c === 'x'? r: (r & 0x3 | 0x8); return v.toString(16); });
} // Using crypto API (more secure)
crypto.randomUUID(); // "550e8400-e29b-41d4-a716-446655440000"UUID v7: The Modern Choice
UUID v7 is the newest version, combining the best of both worlds: it embeds a Unix timestamp in the first 48 bits, making it naturally sortable by creation time, while the remaining bits are random for uniqueness.
// UUID v7 structure | 48 bits | 4 | 12 bits | 2 | 62 bits | | timestamp | v | random | var| random | // Benefits: // ✅ Time-sortable (great for database indexes) // ✅ No MAC address exposure (unlike v1) // ✅ Distributed generation (no coordination needed) // ✅ Monotonic within same millisecond
Using UUIDs in Databases
UUID as Primary Key: Pros and Cons
✅ Advantages
- Globally unique without coordination
- Safe for distributed systems
- No sequential ID guessing attacks
- Easy data merging from multiple sources
- Can be generated client-side
❌ Disadvantages
- Larger storage (16 bytes vs 4-8 bytes)
- Random UUIDs fragment indexes (v4)
- Not human-readable
- Slower joins on large tables
- Harder to debug/communicate verbally
Database-Specific Considerations
-- PostgreSQL: Native UUID support CREATE TABLE users ( id UUID PRIMARY KEY DEFAULT gen_random_uuid(), email VARCHAR(255) NOT NULL ); -- MySQL 8.0+: Binary storage recommended CREATE TABLE users ( id BINARY(16) PRIMARY KEY, email VARCHAR(255) NOT NULL ); -- Use UUID_TO_BIN() and BIN_TO_UUID() functions -- SQLite: Store as TEXT or BLOB CREATE TABLE users ( id TEXT PRIMARY KEY, email TEXT NOT NULL );
Best Practices
- Choose UUID v7 for new projects: It provides the best balance of randomness and sortability, improving database performance.
- Use native database UUID types: PostgreSQL's UUID type is more efficient than VARCHAR.
- Consider hybrid approaches: Use UUIDs for external-facing IDs, auto-increment for internal references.
- Index appropriately: UUID primary keys work well with B-tree indexes, especially v7 UUIDs.
- Validate input: Always validate UUID format when receiving from external sources.
Frequently Asked Questions
Can two UUIDs ever be the same?
Theoretically yes, but the probability is so low it's effectively impossible. With UUID v4, you'd need to generate about 2.71×10^18 UUIDs to have a 50% chance of one collision.
Should I use UUID or auto-increment for primary keys?
Use UUIDs for distributed systems, public-facing IDs, or when merging data from multiple sources. Use auto-increment for simple applications, when storage is a concern, or when you need sequential ordering.
Which UUID version should I use?
For most new applications, UUID v7 is recommended. It's time-sortable (better for database indexes) and doesn't expose hardware information like v1. If v7 isn't available in your language/framework, v4 is the safe default choice.
Conclusion
UUIDs are essential tools for modern distributed systems. Whether you call them UUIDs or GUIDs, understanding when and how to use them—especially the newer v7 version—can significantly impact your application's scalability and performance.
Related Tools & Resources
- UUID Generator - Generate v4 and v7 UUIDs
- Hash Generator - Generate MD5, SHA hashes
- Hash Functions Explained - Understanding cryptographic hashes