UUID Generator: How to Create and Use UUIDs in Your Projects

DevToolbox

Generate UUIDs (v1, v4, v7) online. Learn the differences between UUID versions, when to use each, and best practices — all in your browser.

UUIDs (Universally Unique Identifiers) are 128-bit identifiers used everywhere in software: database primary keys, distributed systems, API resources, session tokens, and file names. They're designed to be unique without requiring a central authority to assign them — any machine can generate a UUID, and the probability of collision is effectively zero.

This guide explains the different UUID versions, when to use each, and how to generate them in your projects. Need UUIDs right now? Our UUID Generator creates them instantly in your browser — no data ever leaves your device.

What Is a UUID?

A UUID is a 128-bit number displayed as 32 hexadecimal characters in the format:

550e8400-e29b-41d4-a716-446655440000
    ↑        ↑    ↑    ↑            ↑
 time-low  mid  hi+ver  variant    node

The format is always 8-4-4-4-12 hex characters (36 total including hyphens). The version number is encoded in the 13th character (the "hi+ver" nibble), and the variant is encoded in the first bits of the 17th character.

UUID Versions Explained

UUID v1 — Time-Based

Generated from the current timestamp (100-nanosecond intervals since October 15, 1582) and the machine's MAC address. This makes v1 UUIDs:

  • Roughly sortable by creation time
  • Traceable to the originating machine
  • Potentially a privacy concern (leaks MAC address)

Use when: You need time-ordering but don't care about privacy. Mostly replaced by v7 in modern systems.

UUID v4 — Random

Generated from 122 bits of cryptographically secure random data. This is the most widely used UUID version because it's simple, fast, and doesn't leak any information about when or where it was created.

// JavaScript
crypto.randomUUID()
// "a1b2c3d4-e5f6-4a7b-8c9d-0e1f2a3b4c5d"
//                    ↑ version nibble is always 4

The probability of collision is astronomically low: you'd need to generate about 2.71 × 10¹⁸ UUIDs (2.71 quintillion) to have a 50% chance of a single duplicate.

Use when: You need a unique identifier and don't need time-ordering. This is the default choice for most applications.

UUID v5 — Name-Based (SHA-1)

Generated by hashing a namespace UUID + a name with SHA-1. Given the same namespace and name, v5 always produces the same UUID. This makes them deterministic.

// Same inputs always give the same UUID
uuidv5("https://example.com", DNS_NAMESPACE)
// Always: "cfbff0d1-9375-5685-968c-48ce8b15ae17"

Use when: You need a deterministic ID derived from a name or URL (e.g., generating consistent IDs for known resources).

UUID v7 — Time-Ordered Random (New!)

Introduced in RFC 9562 (2024), v7 UUIDs combine a Unix timestamp (millisecond precision) with random data. They're the best of both worlds:

  • Naturally sortable by creation time (great for database indexes)
  • Random enough to avoid collisions
  • Don't leak machine identity (unlike v1)
// Structure
0190a6e2-5c44-7b12-8f3e-1a2b3c4d5e6f
└─ timestamp ─┘ └v7┘ └─ random ─────┘

Use when: You need sortable IDs for databases (especially as primary keys). v7 is becoming the recommended default for new projects.

UUID vs Other ID Formats

UUID vs Auto-Increment Integer

  • UUID: No coordination needed, works in distributed systems, doesn't leak information about record count
  • Auto-increment: Smaller storage (4-8 bytes vs 16), naturally ordered, simpler to debug

UUID vs ULID

ULID (Universally Unique Lexicographically Sortable Identifier) is similar to UUID v7 but uses Crockford's Base32 encoding for a more compact, sortable string representation:01ARZ3NDEKTSV4RRFFQ69G5FAV. Choose ULID if you want shorter, URL-friendly IDs.

UUID vs Snowflake ID

Snowflake IDs (used by Twitter/Discord) encode a timestamp + machine ID + sequence number in a 64-bit integer. Choose Snowflake if you need compact integers and can manage machine ID assignment.

Generating UUIDs in Different Languages

JavaScript

// UUID v4 (built-in, Node 19+ and modern browsers)
const id = crypto.randomUUID();

// With the uuid package (for v1, v5, v7)
import { v4, v7 } from 'uuid';
const random = v4();
const timeBased = v7();

Python

import uuid

# UUID v4
random_id = uuid.uuid4()
# UUID("a1b2c3d4-e5f6-4a7b-8c9d-0e1f2a3b4c5d")

# UUID v1
time_id = uuid.uuid1()

# UUID v5
name_id = uuid.uuid5(uuid.NAMESPACE_DNS, "example.com")

PostgreSQL

-- UUID v4 (built-in since PostgreSQL 13)
SELECT gen_random_uuid();

-- As a default primary key
CREATE TABLE users (
  id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  name TEXT NOT NULL,
  email TEXT UNIQUE NOT NULL
);

Command Line

# Linux
uuidgen

# macOS
uuidgen | tr '[:upper:]' '[:lower:]'

# Python one-liner
python3 -c "import uuid; print(uuid.uuid4())"

UUID Best Practices for Databases

1. Use UUID v7 for Primary Keys

Random UUIDs (v4) cause fragmented B-tree indexes because new values are inserted at random positions. UUID v7 is time-ordered, so new values are always appended — just like auto-increment, but distributed.

2. Store as Binary, Not String

A UUID string is 36 characters (288 bits). Binary storage uses only 128 bits — less than half the size. Most databases have native UUID types:

-- PostgreSQL: native UUID type (128 bits)
id UUID PRIMARY KEY

-- MySQL: BINARY(16) or the UUID type in MySQL 8+
id BINARY(16) PRIMARY KEY

3. Consider Indexed Performance

If you have millions of rows and use v4 UUIDs as primary keys, index fragmentation can degrade insert performance. Switch to v7 UUIDs or use a composite key strategy.

Generate UUIDs Instantly

Our UUID Generator creates v4 and v7 UUIDs instantly. Generate single IDs or bulk batches, and copy them with one click. Everything runs in your browser — no server, no tracking, no data collection.

Need related tools? Try the Hash Generator for checksums, or the Password Generator for secure random strings.

Summary

UUIDs are the standard for generating unique identifiers in distributed systems. Use v4 for general-purpose randomness, v7 for time-sortable database keys, and v5 for deterministic name-based IDs. Store them as binary in databases, and prefer v7 over v1 for new projects.

Generate UUIDs instantly with DevToolbox's UUID Generator — free, private, and always available.