Published on

UUID Guide — What They Are, How to Generate Them, and When to Use Them

Authors
  • Name
    Twitter

A UUID (Universally Unique Identifier) is a 128-bit identifier formatted as a 32-character hexadecimal string separated into five groups:

550e8400-e29b-41d4-a716-446655440000

They're designed to be generated independently across distributed systems without any central coordination — and still be unique. The probability of generating a duplicate UUID v4 is astronomically small (around 1 in 5.3 × 10³⁶).

Use Intoolhub's UUID Generator to generate UUIDs in any version.

UUID Versions Explained

There are five official UUID versions, each using a different generation strategy:

UUID v1 — Timestamp + MAC address

Combines the current timestamp with the generating machine's MAC address. This makes v1 UUIDs time-ordered and potentially traceable back to a specific machine — which is a privacy concern in public-facing systems.

Use when: You need sortable IDs in a trusted internal environment.

UUID v4 — Random

The most widely used version. 122 bits of cryptographically random data. No timestamp, no MAC address — just entropy.

f47ac10b-58cc-4372-a567-0e02b2c3d479

Use when: You need a unique identifier and don't need sortability. The right default for most applications.

UUID v5 — Namespace + SHA-1 hash

Deterministic: given the same namespace and name, it always produces the same UUID. Uses SHA-1 internally.

// Always produces the same UUID for the same inputs
uuidv5('https://example.com', uuidv5.URL)
// → 'c74a196f-f19d-5ea9-bffd-a2742199079c'

Use when: You need a repeatable, collision-resistant ID for a known value (e.g., turning a URL or username into a UUID).

UUID v3 — Namespace + MD5 hash

Same concept as v5 but uses MD5 instead of SHA-1. MD5 is considered cryptographically broken — prefer v5 for new systems.

UUID v7 (Draft RFC)

A modern, time-ordered version designed to replace v1 without its privacy issues. Embeds a Unix millisecond timestamp in the first 48 bits, making UUIDs naturally sortable as database primary keys.

018e57d2-2c00-7000-8000-000000000000
└──────────────┘
 ms timestamp (sortable)

Use when: You need database-friendly, sortable UUIDs without exposing MAC addresses.

UUID Structure

Every UUID follows the same format:

xxxxxxxx-xxxx-Mxxx-Nxxx-xxxxxxxxxxxx
  • M — version digit (1, 3, 4, 5, or 7)
  • N — variant digit (always 8, 9, a, or b for RFC 4122 UUIDs)

Generating UUIDs in Code

JavaScript / TypeScript

// Node.js 14.17+ and modern browsers
import { randomUUID } from 'crypto'
const id = randomUUID() // v4

// Or use the uuid package for more control
import { v4 as uuidv4, v5 as uuidv5 } from 'uuid'
const id = uuidv4()

Python

import uuid

uuid.uuid4()  # v4 random
uuid.uuid5(uuid.NAMESPACE_URL, 'https://example.com')  # v5

PostgreSQL

-- Enable the extension
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";

-- Generate
SELECT uuid_generate_v4();

-- Or with pgcrypto (v4 only)
SELECT gen_random_uuid();

MySQL

SELECT UUID();  -- generates v1

UUIDs as Database Primary Keys

The classic advice is to use sequential integers (AUTO_INCREMENT, SERIAL) as primary keys because they perform better for B-tree indexes. UUIDs are random, causing index fragmentation.

UUID v7 solves this by being time-ordered, giving you near-sequential inserts without the coordination overhead of a centralized counter.

If you must use v4 UUIDs as primary keys in high-insert databases, consider:

  • Storing as BINARY(16) not VARCHAR(36) (saves 20 bytes per row)
  • Using ULIDs as an alternative (also sortable, slightly more compact)

UUIDs vs ULIDs vs NanoIDs

PropertyUUID v4UUID v7ULIDNanoID
Sortable
URL-safe string
Standard✅ RFC 4122✅ DraftCommunityCommunity
Database efficiencyFairGoodGoodFair

For most new applications, UUID v7 or ULID is the better choice over UUID v4 when using IDs as database primary keys.

Open UUID Generator