Loading Developer Playground

Loading ...

Skip to main content
Backendβ€’
18 min read

UUID Guide: Generate Unique Identifiers for Databases & APIs (2025)

Master UUIDs (Universally Unique Identifiers) for databases, APIs, and distributed systems. Learn UUID v1, v4, v7 with practical examples. Free UUID generator included.

halfAccessible Team
β€’

Introduction

UUIDs (Universally Unique Identifiers) are 128-bit numbers used to uniquely identify information in distributed systems. Unlike auto-incrementing IDs, UUIDs can be generated anywhere without coordination, making them perfect for modern distributed architectures, APIs, and database migrations.

In this comprehensive guide, you'll learn everything about UUIDs: what they are, different versions, when to use them, performance considerations, and best practices. You'll discover practical techniques for generating, validating, and using UUIDs in your applications.

Generate UUIDs instantly: Use our UUID Generator to create v1, v4, or v7 UUIDs in bulk. It's free, works offline, and requires no signup!


What is a UUID?

A UUID (Universally Unique Identifier), also called GUID (Globally Unique Identifier), is a 128-bit number represented as 36 characters:

550e8400-e29b-41d4-a716-446655440000

Format: xxxxxxxx-xxxx-Mxxx-Nxxx-xxxxxxxxxxxx

  • 8-4-4-4-12 hexadecimal digits
  • 36 characters total (32 hex + 4 hyphens)
  • M = version number
  • N = variant (usually 8, 9, a, or b)

Key Properties

1. Global Uniqueness

UUIDs can be generated independently on any system without coordination:

// Generate on Server 1
const uuid1 = generateUUID() // '550e8400-e29b-41d4-a716-446655440000'
 
// Generate on Server 2 (different machine, same time)
const uuid2 = generateUUID() // 'f47ac10b-58cc-4372-a567-0e02b2c3d479'
 
// Collision probability: ~1 in 2^122 (astronomically unlikely)

2. Can Generate Client-Side

// Browser-generated UUID (no server call needed)
const clientUUID = crypto.randomUUID()
await fetch('/api/users', {
  method: 'POST',
  body: JSON.stringify({ id: clientUUID, name: 'Alice' }),
})

3. Mergeable Databases

// Database 1
users.insert({ id: '550e8400-...', name: 'Alice' })
 
// Database 2
users.insert({ id: 'f47ac10b-...', name: 'Bob' })
 
// Merge databases - no ID conflicts!

πŸ’‘ Think of UUIDs like Social Security Numbers: Unique across the entire system, can be generated without a central authority.

Generate UUIDs: UUID Generator - Create multiple UUIDs instantly!


UUID Versions

UUID v1 (Time-Based)

Format: Timestamp + MAC address + clock sequence

javascript

Pros:

  • βœ… Time-ordered (sortable)
  • βœ… Can extract creation timestamp
  • βœ… Good for log entries

Cons:

  • ❌ Exposes MAC address (privacy concern)
  • ❌ Predictable (security risk)
  • ❌ Not suitable for public-facing IDs

Use when:

  • Logging and auditing
  • Distributed event ordering
  • Internal systems only

UUID v4 (Random)

Format: 122 bits of randomness

javascript

Pros:

  • βœ… Cryptographically random
  • βœ… No privacy concerns
  • βœ… No predictability
  • βœ… Most common version

Cons:

  • ❌ Not sortable
  • ❌ No embedded information
  • ❌ Random distribution (poor index performance)

Use when:

  • Public-facing IDs
  • API keys
  • Session tokens
  • General-purpose unique IDs

Generate v4 UUIDs: UUID Generator

Format: Timestamp + randomness (best of both worlds)

javascript

Pros:

  • βœ… Time-ordered (sortable)
  • βœ… Good for database indexes
  • βœ… Random component (secure)
  • βœ… No privacy concerns
  • βœ… Better performance than v4

Cons:

  • ❌ Relatively new (less support)

Use when:

  • Database primary keys
  • Distributed systems
  • Event sourcing
  • Any time-ordered data

🎯 Best Practice (2025): Use UUID v7 for database IDs and v4 for API tokens. Avoid v1 for public-facing systems.

Version Comparison

Featurev1 (Time-Based)v4 (Random)v7 (Time-Ordered)
Sortableβœ… Yes❌ Noβœ… Yes
Secure⚠️ Predictableβœ… Randomβœ… Random
DB Performanceβœ… Good⚠️ Poorβœ… Excellent
Privacy❌ Exposes MACβœ… Privateβœ… Private
Use CaseLoggingPublic IDsDatabase IDs

Generating UUIDs

Node.js

javascript

Bulk generate: UUID Generator - Generate 1000s at once!

Browser (Native)

javascript

PostgreSQL

sql

Design schemas: Database Schema Generator

MySQL

sql

Python

python

UUID vs Auto-Increment

Auto-Increment IDs

sql

Pros:

  • βœ… Small (4 bytes)
  • βœ… Sequential (great for indexing)
  • βœ… Human-readable
  • βœ… Predictable ordering

Cons:

  • ❌ Centralized (requires database)
  • ❌ Exposes business metrics
  • ❌ Enumeration attacks
  • ❌ Merge conflicts in distributed systems

UUID IDs

sql

Pros:

  • βœ… Decentralized generation
  • βœ… Globally unique
  • βœ… No enumeration risk
  • βœ… Mergeable databases
  • βœ… Client-side generation

Cons:

  • ❌ Larger (16 bytes)
  • ❌ Random (poor index performance with v4)
  • ❌ Less human-friendly

When to Use Each

Use Auto-Increment when:

  • Single database (no distributed system)
  • Small to medium scale
  • Sequential ordering important
  • Storage space critical

Use UUIDs when:

  • Distributed systems
  • Offline-first applications
  • API-first architecture
  • Security/privacy important
  • Database merging needed
  • Client-side ID generation required

🎯 Hybrid Approach: Use auto-increment for internal ordering (e.g., sequence column) and UUID for public-facing IDs.


Database Performance

UUID v4 Performance Issues

javascript

UUID v7 Solves This

javascript

Optimization Techniques

1. Binary Storage

sql

2. Index Optimization

sql

3. Partitioning

sql

Test database design: Database Schema Generator


Common Use Cases

1. Database Primary Keys

javascript

2. API Resources

javascript

3. File Names

javascript

4. Session Tokens

javascript

5. Distributed Events

javascript

6. Idempotency Keys

javascript

UUID Validation

Regex Pattern

javascript

Test patterns: Regex Tester

Validation Library

javascript

Express Middleware

javascript

Security Considerations

1. UUIDs Are NOT Secrets

javascript

2. Predictability of v1

javascript

3. Enumeration Attacks

javascript

Generate secure tokens: Hash Generator


Best Practices

1. Choose the Right Version

javascript

2. Generate Before Insert

javascript

3. Use Binary Storage

sql

4. Index Properly

sql

5. Validate Input

javascript

Migration from Auto-Increment

Step-by-Step Migration

sql

Gradual Migration

javascript

Performance Benchmarks

Insert Performance

javascript

Storage Size

javascript

Conclusion

UUIDs are essential for modern distributed systems, providing globally unique identifiers that can be generated anywhere without coordination. Understanding different UUID versions and their trade-offs helps you choose the right approach for your use case.

Key Takeaways:

  • Use UUID v7 for database primary keys (time-ordered, great performance)
  • Use UUID v4 for public-facing IDs and tokens (random, secure)
  • Avoid UUID v1 for public systems (privacy concerns)
  • Store as BINARY(16) in databases (saves space)
  • Generate UUIDs before database inserts (enables parallel operations)
  • Always validate UUID format in APIs

Generate UUIDs: Use our UUID Generator to create v1, v4, or v7 UUIDs in bulk instantly!

What will you build with UUIDs? Whether it's a distributed system, RESTful API, or scalable database, you now have the knowledge to implement robust unique identifiers!


Build better systems with these free tools:

All tools are 100% free, require no signup, and respect your privacy.

Further Reading


Frequently Asked Questions

What's the difference between UUID v1, v4, and v7?

UUID v1 is time-based but exposes MAC address (avoid for public systems). UUID v4 is completely random (secure but not sortable). UUID v7 is time-ordered random (best of both: sortable like v1, secure like v4)β€”recommended for most use cases.

Can UUIDs collide?

Theoretically yes, but probability is astronomically low (~1 in 2^122 for v4). You'd need to generate 1 billion UUIDs per second for 100 years to have a 50% chance of one collision. In practice, collisions never happen.

Should I use UUID or auto-increment for my database?

Use UUIDs for distributed systems, API-first architectures, or when you need client-side ID generation. Use auto-increment for simple applications with a single database. For best of both worlds, use UUID v7 (time-ordered like auto-increment).

How do I store UUIDs in databases efficiently?

In PostgreSQL, use the UUID type (16 bytes). In MySQL, use BINARY(16) with UUID_TO_BIN/BIN_TO_UUID functions. Never store as CHAR(36)β€”it wastes space and hurts performance.

Are UUIDs secure enough for API tokens?

UUID v4 provides 122 bits of randomness, which is secure for most use cases. For higher security tokens (like authentication), consider combining UUIDs with additional entropy or using dedicated token generation libraries.

Can I generate UUIDs in the browser?

Yes! Modern browsers support crypto.randomUUID() (Chrome 92+, Firefox 95+, Safari 15.4+). For older browsers, use the uuid npm package or our UUID Generator web tool.


Build globally! 🌍

Generate UUIDs instantly with our UUID Generatorβ€”create v1, v4, v7 UUIDs in bulk, validate formats, and copy with one click!

Related Articles