UUID v4 — Why Random UUIDs Matter and How to Generate Them
Understand UUID v4 random identifiers, why they're essential for distributed systems, and how to generate them reliably for your applications.
What Is a UUID?
A UUID (Universally Unique Identifier) is a 128-bit number used to uniquely identify information in computer systems. UUIDs are designed to be unique across both space and time, making them ideal for distributed systems where centralized ID generation isn't feasible.
The textual representation of a UUID is a string of 32 hexadecimal digits, displayed in five groups separated by hyphens, in the form:
xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
Example:
`
550e8400-e29b-41d4-a716-446655440000
`
UUID Versions Explained
There are several versions of UUIDs, each generated differently:
| Version | Name | Description | Use Case |
|---|---|---|---|
| v1 | Time-based | Based on timestamp and MAC address | Legacy systems, not recommended for security |
| v2 | DCE Security | Similar to v1 with POSIX UIDs | Rarely used |
| v3 | MD5 hash | Name-based using MD5 hashing | Deterministic IDs from names |
| v4 | Random | Randomly generated (recommended) | Most common, best for general use |
| v5 | SHA-1 hash | Name-based using SHA-1 hashing | Deterministic IDs from names |
UUID v4 is the most widely used version because it's simple, doesn't leak information, and has an extremely low collision probability.
Why UUID v4 Matters
1. No Central Coordination Required
Unlike auto-incrementing integer IDs, UUIDs can be generated independently by any node in a distributed system without risk of collision (for all practical purposes).
2. Security Through Obscurity
UUID v4 is randomly generated, making it impossible to guess valid IDs by enumeration. This protects against: - Insecure direct object references (IDOR) attacks - Information leakage about database size - Timing attacks based on ID patterns
3. Scalability
UUIDs work seamlessly across: - Multiple database shards - Microservices architectures - Multi-region deployments - Offline-first applications
4. Merge-Friendly
When merging data from different sources (databases, offline syncs, imports), UUIDs eliminate ID conflicts that would occur with sequential integers.
UUID v4 Structure
A UUID v4 looks like this:
`
123e4567-e89b-42d3-a456-556642440000
^^^^
||||
|||+-- Variant (10xx = RFC 4122)
||+--- Version (0100 = v4)
|+---- Random
+----- Random
`
Key characteristics:
- 128 bits total
- Version 4 indicated by 0100 in bits 48-51 of the third group
- Variant 1 indicated by 10xx in bits 64-65 of the fourth group
- 122 bits of randomness
Collision Probability
With 122 bits of randomness, the probability of collision is astronomically low:
| Number of UUIDs | Collision Probability |
|---|---|
| 1 billion | 1 in 103 trillion |
| 10 billion | 1 in 1 trillion |
| 1 trillion | 1 in 10 billion |
This is safe enough that you can generate UUIDs without any coordination and virtually guarantee uniqueness.
> Fun fact: You could generate 1 billion UUIDs per second for 85 years and have only a 50% chance of one collision.
How to Generate UUIDs
In JavaScript/Node.js
// Modern browsers and Node.js 14.17+
const uuid = crypto.randomUUID();
// Legacy approach (not recommended)
const uuidv4 = require('uuid/v4');
`
In Python
# Generate a UUID v4 new_uuid = uuid.uuid4() print(str(new_uuid)) # "550e8400-e29b-41d4-a716-446655440000"
# Generate multiple
for _ in range(5):
print(uuid.uuid4())
`
In Java
UUID uuid = UUID.randomUUID();
System.out.println(uuid.toString());
`
In PostgreSQL
-- Generate UUID v4
-- Use as primary key
CREATE TABLE users (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
name TEXT
);
`
In MySQL
-- MySQL 8.0+
-- As primary key
CREATE TABLE users (
id CHAR(36) PRIMARY KEY DEFAULT (UUID()),
name VARCHAR(255)
);
`
Using FreeToolJet's UUID Generator
Our UUID Generator tool provides:
- Bulk generation — Create multiple UUIDs at once
- Copy to clipboard — One-click copying for easy use
- No registration required — Generate UUIDs instantly
- Client-side only — Your data never leaves your browser
Step-by-Step Guide
- Open the UUID Generator tool
- Select how many UUIDs you need (1-100)
- Click "Generate"
- Copy the UUIDs you need
Best Practices for Using UUIDs
✅ DO:
- Use UUID v4 for most use cases
- Store UUIDs as
UUIDtype in databases that support it (PostgreSQL) - Use UUIDs as string IDs in JSON APIs
- Generate UUIDs client-side when appropriate (offline support)
❌ DON'T:
- Use UUIDs as database primary keys if you need sequential ordering (use ULIDs instead)
- Store UUIDs in case-sensitive fields without normalization
- Use UUID v1 (leaks MAC address and timestamp)
- Rely on UUIDs for security (they're identifiers, not secrets)
UUIDs vs. Other ID Strategies
| Strategy | Pros | Cons | Best For |
|---|---|---|---|
| Auto-increment INT | Simple, fast queries | Not distributed, guessable | Single-database apps |
| UUID v4 | Distributed, unguessable | Larger storage, slower indexes | Distributed systems |
| ULID | Time-ordered, unique | Less standard | Time-sorted distributed IDs |
| NanoID | Compact, URL-friendly | Less standard than UUID | Short URLs, lightweight apps |
Performance Considerations
UUIDs as primary keys can impact database performance:
- Index fragmentation: Random UUIDs cause B-tree indexes to fragment
- Storage size: 36 characters vs. 4-8 bytes for integers
- Query performance: Larger indexes = slower lookups
Mitigation strategies:
- Use UUID native type (PostgreSQL) instead of VARCHAR(36)
- Consider time-ordered UUIDs (UUID v7) for better index behavior
- Use integers for internal references, UUIDs for external APIs
UUID v7 — The Future
UUID v7 combines the benefits of UUID v4 with time-ordering:
018ecd6e-8c7a-7d63-8c7a-018ecd6e8c7a
^^^^^^^^
Timestamp (milliseconds)
Benefits: - Time-ordered (better for database indexes) - Still randomly unique - Compatible with UUID v4 tooling
Common Use Cases
- Database primary keys in distributed systems
- Session IDs for web applications
- Trace IDs for distributed tracing (OpenTelemetry)
- File/image IDs in storage systems
- API resource identifiers (RESTful URLs)
- Message IDs in event-driven architectures
- Transaction IDs for audit trails
Related Tools
- UUID Generator — Generate random UUID v4 identifiers
- Password Generator — Create secure random passwords
- Hash Generator — Generate hashes for data integrity
- Slug Generator — Create URL-friendly identifiers