UUID v7 vs UUID v4
| Feature | UUID v7 | UUID v4 |
|---|---|---|
| Generated from | Timestamp + random | Random only |
| Sort order | By creation time | Random |
| Database index performance | Excellent (sequential inserts) | Poor at scale (random inserts) |
| Reveals creation time | Yes | No |
| Random bits | 74 | 122 |
| Best for | Primary keys, event IDs, logs | Tokens, IDs that must not leak timing |
UUID v7 bit layout
017f22e2-79b0-7cc3-98c4-dc0c0c07398f
|-----------| | | ||-----------------|
unix_ts_ms ver rand_a var rand_b
48 bits 4 12 2 62 bits
The version nibble is always 7 (the 13th hex digit). The variant bits make the 17th hex digit one of
8, 9, a or b. This generator uses rand_a as a
counter within each millisecond, as RFC 9562 allows, so every list it produces is already sorted.
Generate UUIDs in code
PostgreSQL
-- PostgreSQL 18+
SELECT uuidv7();
CREATE TABLE orders (
id uuid PRIMARY KEY DEFAULT uuidv7(),
...
);
-- Older versions: gen_random_uuid() gives v4
JavaScript / Node.js
// Node.js / browsers: v4 is built in
crypto.randomUUID();
// v7: npm install uuid
import { v7 as uuidv7 } from 'uuid';
uuidv7(); // '019a0b6e-...'
Python
import uuid
uuid.uuid4()
uuid.uuid7() # Python 3.14+
Want shorter, URL-friendly time-sortable IDs? Try the ULID generator.
Frequently asked questions
What is UUID v7?
UUID v7 is a time-ordered UUID defined in RFC 9562 (2024). Its first 48 bits are a Unix timestamp in milliseconds, followed by random bits. New IDs sort after older ones, which keeps database B-tree indexes compact and inserts fast.
Should I use UUID v7 or v4 for primary keys?
Prefer v7 for database primary keys. Random v4 IDs scatter inserts across the whole index, causing page splits and poor cache locality. v7 appends to the end like an auto-increment while staying globally unique. Use v4 when you don’t want the ID to reveal its creation time.
Can I extract the timestamp from a UUID?
Yes for v7, v1 and v6. Paste the UUID into the inspector above. v7 stores Unix milliseconds directly, while v1 and v6 store 100-nanosecond intervals since 1582-10-15. v4 UUIDs are fully random and contain no timestamp.
Are these UUIDs unique and secure?
They are generated with crypto.getRandomValues, the browser’s cryptographically secure random number generator. A v4 has 122 random bits. A v7 has 74 random bits per millisecond, with a 12-bit counter that keeps bulk-generated IDs strictly increasing.
Is a GUID the same as a UUID?
Yes. GUID is Microsoft’s name for a UUID. Use the UPPERCASE and {Braces} options to match the format used in .NET and the Windows registry.