Unique identifiers are the backbone of modern distributed systems, microservice architectures, and relational databases.
Historically, auto-incrementing integer IDs (1, 2, 3...) were standard. However, integer IDs expose sensitive business metrics (such as total order volume) and cannot be generated independently on distributed client devices without database centralization.
Enter UUIDs (Universally Unique Identifiers) and GUIDs.
This guide covers UUID formatting, the difference between random UUID v4 and time-ordered UUID v7 (RFC 9562), database indexing performance, and generator tools.
Anatomy of a 128-bit UUID
A standard UUID is a 128-bit number formatted as a string of 32 hexadecimal digits separated by hyphens into 5 groups (8-4-4-4-12):
123e4567-e89b-12d3-a456-426614174000
- Group 1: 8 hex digits (32 bits)
- Group 2: 4 hex digits (16 bits)
- Group 3: 4 hex digits (16 bits) — Version digit encoded here
- Group 4: 4 hex digits (16 bits) — Variant bits encoded here
- Group 5: 12 hex digits (48 bits)
UUID v4: Pure Randomness
UUID v4 is the most popular unique identifier on the web. It replaces almost all bits with pseudo-random or cryptographically random data:
- 122 bits of randomness
- 4 bits reserved for Version (
4) - 2 bits reserved for Variant (
10)
Example UUID v4:
f47ac10b-58cc-4372-a567-0e02b2c3d479
Best Use Cases for UUID v4
- API request correlation IDs (
X-Request-ID). - Session tokens and user tracking IDs.
- Client-side id generation where items are not indexed sequentially in SQL.
UUID v7: Time-Ordered Sequential Identifiers (RFC 9562)
Published in RFC 9562, UUID v7 solves the biggest flaw of UUID v4 in database design: index fragmentation.
UUID v7 combines a 48-bit Unix millisecond timestamp with 74 bits of random data.
Structure of UUID v7:
- Bits 0–47: Unix Epoch timestamp in milliseconds (48 bits)
- Bits 48–51: Version
7(0111) - Bits 52–63: Sub-millisecond sequence / randomness (12 bits)
- Bits 64–65: Variant
10(2 bits) - Bits 66–127: Random bits (62 bits)
Example UUID v7:
018c5e64-789a-7123-8abc-def012345678
Notice that the first group (018c5e64...) advances sequentially over time because it reflects UTC clock time.
Database Indexing: Why UUID v7 Outperforms UUID v4
Relational databases like PostgreSQL, MySQL (InnoDB), and SQL Server store primary keys using B-Tree indexes.
[ Random UUID v4 Insert ] [ Time-Ordered UUID v7 Insert ]
--------------------------- --------------------------------
Inserts hit random pages in DB. Inserts always hit the right-most
Causes page splits, disk I/O, page of the B-Tree index.
and high WAL bloat. Minimal cache misses & fast insert.
Performance Impact Benchmark Summary
- Insert Speed: UUID v7 inserts are up to 3x–5x faster than random UUID v4 when dataset size exceeds RAM.
- Index Cache Efficiency: Time-ordered UUIDs keep hot index pages grouped together in memory.
Code Examples: Generating UUIDs
JavaScript / Node.js (Web Crypto API)
// Native Web Crypto API (UUID v4)
const uuidV4 = crypto.randomUUID();
console.log(uuidV4); // e.g. "9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6d"
PostgreSQL
-- PostgreSQL 13+ UUID v4
SELECT gen_random_uuid();
-- PostgreSQL UUID v7 (or via extension)
SELECT uuid_generate_v7();
Generate single or batch UUIDs instantly with the free ToolzStack UUID Generator and GUID Generator.