UUID Generator Guide: Understanding Universally Unique Identifiers
What UUIDs are, the different versions (v1, v4, v7), when to use each, and how to generate UUIDs in JavaScript, Python, and other languages.
UUIDs (Universally Unique Identifiers) are everywhere in modern software. They identify database records, API resources, session tokens, and distributed system components. Understanding how they work helps you choose the right version for your use case.
What Is a UUID?
A UUID is a 128-bit identifier formatted as 32 hexadecimal characters separated by hyphens:
550e8400-e29b-41d4-a716-446655440000
The format is 8-4-4-4-12 (total 36 characters including hyphens). With 128 bits, there are 2^128 possible UUIDs — approximately 3.4 × 10^38. The probability of generating a duplicate is effectively zero for any practical application.
UUID Versions
UUID v1 (Timestamp + MAC)
Generated from the current timestamp and the machine's MAC address. The timestamp is the number of 100-nanosecond intervals since October 15, 1582.
Pros: Time-sortable, includes node identity Cons: Exposes MAC address and timestamp — privacy concern Use case: Legacy systems, need time-ordering
UUID v4 (Random)
The most common version. All 128 bits are randomly generated (with 6 bits fixed for version and variant).
Pros: Simple, no dependencies, good randomness Cons: Not sortable, no embedded information Use case: Database primary keys, API identifiers, session tokens
UUID v7 (Timestamp + Random)
The newest version (RFC 9562, 2024). Combines a millisecond timestamp with random bits, making it time-sortable.
Pros: Time-sortable, privacy-safe, database-friendly Cons: Newer — not all libraries support it yet Use case: Database primary keys (better than v4 for indexing), event IDs
Comparison
| Version | Bits | Sortable | Includes | Privacy | |---------|------|----------|----------|---------| | v1 | 128 | By time | Timestamp + MAC | Poor | | v4 | 128 | No | Random only | Good | | v7 | 128 | By time | Timestamp + random | Good |
When to Use What
Use UUID v4 when:
- You need a simple unique identifier
- Sort order doesn't matter
- You want maximum compatibility
Use UUID v7 when:
- You need time-sortable IDs (database primary keys)
- You want better insert performance (sequential IDs)
- Your database and ORM support v7
Avoid UUID v1 unless you're maintaining a legacy system.
Generate UUIDs in Code
JavaScript
// UUID v4 (built-in)
const uuid = crypto.randomUUID();
// "550e8400-e29b-41d4-a716-446655440000"
// UUID v7 (requires library)
import { v7 as uuidv7 } from 'uuid';
const sortableId = uuidv7();
Python
import uuid
# UUID v4
uuid.uuid4()
# UUID('550e8400-e29b-41d4-a716-446655440000')
# UUID v7 (Python 3.13+)
uuid.uuid7()
Command Line
# Linux/macOS
uuidgen
# 550E8400-E29B-41D4-A716-446655440000
# Lowercase
uuidgen | tr '[:upper:]' '[:lower:]'
SQL
-- PostgreSQL
gen_random_uuid()
-- MySQL
UUID()
UUIDs vs Other ID Strategies
| Strategy | Unique | Sortable | Compact | Secure | |----------|--------|----------|---------|--------| | UUID v4 | Yes | No | No | Yes | | UUID v7 | Yes | Yes | No | Yes | | Auto-increment | Yes (per DB) | Yes | Yes | No | | Nano ID | Yes | No | Partial | Yes | | CUID2 | Yes | Yes | Partial | Yes |
Auto-increment is simpler but doesn't work in distributed systems. UUID v7 gives you the best of both worlds — uniqueness across systems and database-friendly ordering.
Best Practices
- Use UUID v7 for new projects — it's the modern standard
- Store as binary (16 bytes) not string (36 bytes) in databases
- Index UUID columns — random UUIDs (v4) cause index fragmentation
- Don't expose internal UUIDs in URLs — use slugs or separate public IDs
- Validate UUID format on input — don't trust user-provided IDs
Try It Now
Use our free UUID Generator to generate UUIDs instantly — v4, batch generation, and copy-to-clipboard support.