UUIDs Explained: v4, v7, and When to Use Them
Understand what UUIDs are, the differences between v4 (random) and v7 (time-ordered), when to use UUIDs vs auto-increment IDs, and alternatives like NanoID and ULID.
What is a UUID?
A UUID (Universally Unique Identifier), also called a GUID on Windows, is a 128-bit number used to identify resources without a central authority assigning the ID. The canonical string format is 32 hexadecimal digits arranged in five groups separated by hyphens:
`550e8400-e29b-41d4-a716-446655440000` `xxxxxxxx-xxxx-Mxxx-Nxxx-xxxxxxxxxxxx`
The `M` nibble indicates the version (1–8) and the `N` nibble indicates the variant (most modern UUIDs use the RFC 4122 variant, where N starts with `8`, `9`, `a`, or `b`).
UUID v4: Random
Version 4 is the most widely used UUID. It uses 122 bits of cryptographically random data (the remaining 6 bits encode the version and variant).
Collision probability: if you generate 1 billion UUIDs per second for 100 years, the probability of a single collision is approximately 50%. For practical purposes, v4 UUIDs are unique enough that you never need to check for duplicates.
When to use v4: - You need a unique ID and don't care about ordering - IDs are generated across multiple machines or services without coordination - You're exposing IDs in URLs and want them to be opaque (non-guessable sequence)
// JavaScript (crypto module or crypto.randomUUID)
const id = crypto.randomUUID();
// → '9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6d'
// Python
import uuid
id = str(uuid.uuid4())
# PostgreSQL
SELECT gen_random_uuid();UUID v7: Time-Ordered
Version 7 encodes a Unix millisecond timestamp in the first 48 bits, followed by random data. This makes v7 UUIDs sortable by creation time — newer UUIDs sort lexicographically after older ones.
Why does this matter? Database B-tree indexes perform best when new rows are inserted at the end of the index (sequential keys). Random v4 UUIDs cause page splits — the database must insert new rows throughout the entire B-tree, leading to index fragmentation and slower writes at scale.
v7 is the right choice when: - You use UUIDs as primary keys in PostgreSQL, MySQL, or SQLite - You need to sort records by creation time without a separate timestamp column - You're building distributed systems that need both uniqueness and ordering
-- PostgreSQL 17+ has native uuid_generate_v7()
SELECT uuid_generate_v7();
-- Or use the uuid-ossp extension for v7
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
-- Sorting by UUID v7 gives chronological order
SELECT * FROM events ORDER BY id; -- works correctly with v7UUIDs vs Auto-Increment IDs
Auto-increment integers (SERIAL, IDENTITY, AUTO_INCREMENT): - Predictable and enumerable (attackers can guess neighboring IDs) - Single point of ID generation (the database) - Smaller storage footprint (4 or 8 bytes vs 16 bytes) - Join-friendly and human-readable
UUIDs: - Opaque and non-guessable (better privacy) - Generated anywhere — client, service, or database - Safe to merge data from multiple sources - Work naturally in distributed and event-sourced systems - 16 bytes storage (4× larger than a 32-bit integer)
A common pattern: use an auto-increment integer as the internal primary key for efficiency, but expose a UUID as the external identifier in APIs and URLs.
UUIDs vs NanoID and ULID
Alternatives to UUID for specific use cases:
- NanoID — generates shorter URL-safe strings (e.g. 21 characters by default) using a custom alphabet. Smaller in JSON payloads and URLs. Same collision resistance as UUID v4. Popular in JavaScript ecosystems.
- ULID (Universally Unique Lexicographically Sortable Identifier) — like UUID v7 but encoded in Crockford Base32 (26 characters), making it more compact and URL-safe. A good alternative when you want sortable IDs without the standard UUID format.
// NanoID — 21 chars, URL-safe
import { nanoid } from 'nanoid';
const id = nanoid(); // → 'V1StGXR8_Z5jdHi6B-myT'
// ULID
import { ulid } from 'ulid';
const id = ulid(); // → '01ARZ3NDEKTSV4RRFFQ69G5FAV'Generating UUIDs with DevForge
The DevForge UUID Generator lets you produce single or bulk batches of v4 or v7 UUIDs with one click. You can copy individual IDs or copy all at once — useful when seeding a database, creating test fixtures, or generating IDs for API testing without needing a running application.
Try it on DevForge
Free online tools related to this tutorial — no signup required.
Related Tutorials
REST API Design Basics
Learn how to design clean, consistent REST APIs. Covers resource naming, HTTP methods, status codes, pagination, and versioning.
SQL Joins Visualized
Understand SQL joins with clear examples. Learn INNER JOIN, LEFT JOIN, RIGHT JOIN, FULL JOIN, and CROSS JOIN with practical use cases.
Understanding JWT Authentication
Learn how JSON Web Tokens work, what the three parts mean, how signatures are verified, and the most common security mistakes developers make with JWTs.