Skip to main content
Developer Tools August 3, 2026 · 5 min read

What Is a UUID and How to Generate One

Any time you need a unique identifier that can be generated independently — without a database or central server — you need a UUID. They appear everywhere: database primary keys, session IDs, file names, API resources, distributed system events. Here's everything you need to know.

What Is a UUID?

A UUID (Universally Unique Identifier) is a 128-bit number represented as 32 hexadecimal characters in a specific format:

550e8400-e29b-41d4-a716-446655440000
^^^^^^^^ ^^^^ ^^^^ ^^^^ ^^^^^^^^^^^^
   time   time vers  var    random

It's always 36 characters long (32 hex + 4 hyphens). The probability of generating two identical UUIDs (v4) is astronomically small — approximately 1 in 5.3 × 10³⁶. In practice, collisions never happen.

GUID (Globally Unique Identifier) is Microsoft's term for the same concept. They're identical in format and purpose.

UUID Versions

v1 — Time-based

Generated from the current timestamp + MAC address. Sortable by creation time. Contains the generating machine's MAC address — a privacy concern in some contexts.

v3 — Name-based (MD5)

Generated by hashing a namespace + name with MD5. Deterministic: same input always produces the same UUID. Used for stable IDs based on names.

v4 — Random (most common)

Entirely random except for version/variant bits. The default choice for most use cases. Generated with cryptographically secure random number generators.

v5 — Name-based (SHA-1)

Like v3 but uses SHA-1 hashing. Preferred over v3 for new projects since SHA-1 is stronger than MD5.

v7 — Time-ordered (new in 2022)

Combines Unix timestamp in the high bits with random data. Sortable chronologically — much better for database index performance than v4.

Generating UUIDs in Code

JavaScript / Node.js

// Browser (built-in, no library)
const id = crypto.randomUUID(); // v4

// Node.js 14.17+
import { randomUUID } from 'crypto';
const id = randomUUID();

// Older approach: uuid package
import { v4 as uuidv4, v7 as uuidv7 } from 'uuid';
const id4 = uuidv4();  // random
const id7 = uuidv7();  // time-ordered

Python

import uuid

v4 = uuid.uuid4()          # random
v5 = uuid.uuid5(uuid.NAMESPACE_DNS, 'example.com')  # name-based

print(str(v4))  # 550e8400-e29b-41d4-a716-446655440000
print(v4.hex)   # without dashes

SQL

-- PostgreSQL
SELECT gen_random_uuid();   -- v4 (built-in)

-- MySQL 8+
SELECT UUID();              -- v1

-- SQLite (no native UUID, store as TEXT)
-- Use application-layer UUID generation

UUID as a Database Primary Key

UUIDs are popular as primary keys in distributed systems because they don't require a central authority to assign IDs. However, they have tradeoffs:

✓ Advantages

  • Generated client-side, no round-trip
  • Works across distributed systems
  • IDs don't reveal insertion order
  • Safe to expose in URLs

Tradeoffs

  • Random v4 UUIDs fragment indexes
  • Larger than integer keys (16 bytes vs 4–8)
  • Less human-readable in logs
  • Solve with v7 (time-ordered) or ULID

Generate UUIDs instantly — free