What a UUID is
A UUID (Universally Unique Identifier, also called a GUID) is a 128-bit value written as 32 hexadecimal digits in five groups: 8-4-4-4-12. RFC 9562 (2024, replacing RFC 4122) defines eight versions. Two of them matter for new systems: v4, which is random, and v7, which starts with a timestamp so that values sort by creation time.
Uniqueness is probabilistic. A v4 UUID has 122 random bits; you would need to generate about 261 of them (2.3 quintillion) before the chance of a single collision reaches 50%. No coordination between generators is required.
v4 or v7
| v4 | v7 | |
|---|---|---|
| Content | 122 random bits | 48-bit Unix millisecond timestamp, then 74 bits of counter and random data |
| Sortable by time | No | Yes, lexically and as bytes |
| Reveals creation time | No | Yes, to the millisecond |
| Database index behaviour | Random inserts scatter across a B-tree, which fragments the index and hurts write throughput at scale | Mostly append-only inserts, similar to an auto-increment key |
| Choose when | The ID must not leak timing; or the system already standardised on v4 | IDs are primary keys, or you want natural time ordering in logs and storage |
The v7 values generated here are monotonic within a session: when several are created in the same millisecond, a 12-bit counter in the rand_a field increments (RFC 9562 §6.2, method 1), so they still sort in generation order.
Layout
v4: xxxxxxxx-xxxx-4xxx-Nxxx-xxxxxxxxxxxx
v7: tttttttt-tttt-7ccc-Nrrr-rrrrrrrrrrrr
t = 48-bit Unix time in milliseconds
4 / 7 = version nibble
N = variant: 8, 9, a or b (binary 10xx)
c = 12-bit counter or random (rand_a)
x / r = random
Two special values are defined: the Nil UUID (all zeros) and the Max UUID (all f). Paste either into the inspector above.
Generating UUIDs in code
JavaScript
crypto.randomUUID(); // v4, browsers and Node 19+
// v7: Node 24+ ships it in node:crypto; otherwise use the "uuid" package
import { v7 as uuidv7 } from "uuid";
uuidv7();
Python
import uuid
uuid.uuid4()
uuid.uuid7() # Python 3.14+; earlier versions: pip install uuid7
SQL
-- PostgreSQL 13+
SELECT gen_random_uuid(); -- v4
SELECT uuidv7(); -- PostgreSQL 18+
-- MySQL 8
SELECT UUID(); -- v1 (time-based, MAC-derived node)
Command line
uuidgen # macOS and util-linux: v4 by default
cat /proc/sys/kernel/random/uuid
Storage
Store UUIDs as 16 raw bytes (uuid in PostgreSQL, BINARY(16) in MySQL) rather than as a 36-character string. It halves the index size and makes comparisons cheaper. Serialise to the hyphenated lowercase form at the API boundary; RFC 9562 specifies lowercase output and case-insensitive input.
Notes on this implementation
All bits come from crypto.getRandomValues, the browser's cryptographically secure generator. Nothing is derived from Math.random. Generation happens in your browser; the values are never sent anywhere and are not stored.