FTJ
← Blog
Developer

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:

VersionNameDescriptionUse Case
v1Time-basedBased on timestamp and MAC addressLegacy systems, not recommended for security
v2DCE SecuritySimilar to v1 with POSIX UIDsRarely used
v3MD5 hashName-based using MD5 hashingDeterministic IDs from names
v4RandomRandomly generated (recommended)Most common, best for general use
v5SHA-1 hashName-based using SHA-1 hashingDeterministic 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 UUIDsCollision Probability
1 billion1 in 103 trillion
10 billion1 in 1 trillion
1 trillion1 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:

  1. Bulk generation — Create multiple UUIDs at once
  2. Copy to clipboard — One-click copying for easy use
  3. No registration required — Generate UUIDs instantly
  4. Client-side only — Your data never leaves your browser

Step-by-Step Guide

  1. Open the UUID Generator tool
  2. Select how many UUIDs you need (1-100)
  3. Click "Generate"
  4. Copy the UUIDs you need

Best Practices for Using UUIDs

✅ DO:

  • Use UUID v4 for most use cases
  • Store UUIDs as UUID type 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

StrategyProsConsBest For
Auto-increment INTSimple, fast queriesNot distributed, guessableSingle-database apps
UUID v4Distributed, unguessableLarger storage, slower indexesDistributed systems
ULIDTime-ordered, uniqueLess standardTime-sorted distributed IDs
NanoIDCompact, URL-friendlyLess standard than UUIDShort URLs, lightweight apps

Performance Considerations

UUIDs as primary keys can impact database performance:

  1. Index fragmentation: Random UUIDs cause B-tree indexes to fragment
  2. Storage size: 36 characters vs. 4-8 bytes for integers
  3. 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

  1. Database primary keys in distributed systems
  2. Session IDs for web applications
  3. Trace IDs for distributed tracing (OpenTelemetry)
  4. File/image IDs in storage systems
  5. API resource identifiers (RESTful URLs)
  6. Message IDs in event-driven architectures
  7. Transaction IDs for audit trails

Related Tools

Try These Tools

More Articles