DEV Community

arenasbob2024-cell
arenasbob2024-cell

Posted on • Originally published at viadreams.cc

UUID v4 vs v7: Which Should You Use in 2026? (With Generator)

UUIDs are everywhere — database primary keys, distributed systems, API request tracing. But with UUID v7 now standardized in RFC 9562, the landscape has changed. Here's what you need to know.

UUID Versions at a Glance

Version Based On Sortable When to Use
v1 Timestamp + MAC Partially Legacy systems
v4 Random No General purpose (most common)
v5 SHA-1 hash No Deterministic IDs from names
v7 Timestamp + Random Yes New projects (recommended)

Why UUID v7 Is the New Default

UUID v7 combines the best of both worlds:

// UUID v4 (random - not sortable)
"9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6d"

// UUID v7 (timestamp-prefixed - sortable!)
"01903b6e-34a5-7b8a-8d3f-4e2c1a9f5d7b"
//^^^^^^^^ timestamp portion
Enter fullscreen mode Exit fullscreen mode

Database benefits of v7:

  • B-tree index friendly (sequential inserts)
  • 30-40% faster INSERT performance vs v4
  • Built-in temporal ordering
  • No index fragmentation

Generate UUIDs in JavaScript

// Native (v4) - available in all modern browsers
const id = crypto.randomUUID();

// UUID v7 with the uuid package
import { v7 as uuidv7 } from 'uuid';
const id = uuidv7(); // "01903b6e-..."
Enter fullscreen mode Exit fullscreen mode

Generate UUIDs in Python

import uuid

# v4 (random)
id = uuid.uuid4()

# v7 (Python 3.12+ proposal, or use uuid6 package)
from uuid6 import uuid7
id = uuid7()
Enter fullscreen mode Exit fullscreen mode

UUID as Database Primary Keys

-- PostgreSQL with native UUID type
CREATE TABLE users (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    email TEXT NOT NULL UNIQUE,
    created_at TIMESTAMPTZ DEFAULT NOW()
);

-- For UUID v7 (better performance)
CREATE EXTENSION IF NOT EXISTS pg_uuidv7;
CREATE TABLE orders (
    id UUID PRIMARY KEY DEFAULT uuid_generate_v7(),
    user_id UUID REFERENCES users(id),
    total DECIMAL(10,2)
);
Enter fullscreen mode Exit fullscreen mode

UUID vs ULID vs NanoID

Feature UUID v4 UUID v7 ULID NanoID
Length 36 chars 36 chars 26 chars 21 chars
Sortable No Yes Yes No
Standard RFC 9562 RFC 9562 Spec -
URL-safe No No Yes Yes
DB-friendly OK Great Great OK

Try It Online

Need to quickly generate UUIDs for testing? Try our free UUID Generator — supports v1, v4, v5, and v7 with bulk generation and instant copy.

Key Takeaways

  1. New projects: Use UUID v7 for better database performance
  2. Existing projects: UUID v4 is fine, no need to migrate
  3. URL-friendly IDs: Consider NanoID or ULID
  4. Deterministic IDs: Use UUID v5 with a namespace
  5. Always use your language's crypto-safe generator, never Math.random()

What UUID version are you using? Drop a comment below!

Top comments (0)