UUID Generator: What is a UUID and When Should You Use One?
UUIDs appear in every codebase, but developers often use them without fully understanding when they're the right choice — and when they're not. This guide explains what UUIDs are, the different versions, and the practical tradeoffs for using them as database primary keys.
What is a UUID?
A UUID (Universally Unique Identifier) is a 128-bit number, standardised in RFC 4122, represented as 32 hexadecimal digits in 5 groups separated by hyphens:
a3f8b2c1-4e5d-6f7a-8b9c-0d1e2f3a4b5c
^^^^^^^^ ^^^^ ^^^^ ^^^^ ^^^^^^^^^^^^
8 chars 4 4 4 12 charsThe "universally unique" part means two independently generated UUIDs should never collide — the probability of a collision is astronomically small (about 1 in 2^122).
UUID Versions
| Version | How Generated | Sortable? | Use When |
|---|---|---|---|
| v1 | Timestamp + MAC address | Partially | Legacy systems |
| v4 | Purely random | No | Most use cases — simple, widely supported |
| v5 | SHA-1 hash of namespace + name | No | Deterministic IDs from known inputs |
| v7 | Timestamp-prefixed random | Yes | Database PKs — better index performance |
v4 — The Default Choice
UUID v4 is purely random and the most commonly used version. It's what most libraries generate by default and what Dummy JSON Generator produces for the UUID field type.
// JavaScript (native, no library)
const id = crypto.randomUUID();
// "a3f8b2c1-4e5d-4f7a-8b9c-0d1e2f3a4b5c"
// Note: version digit is always 4 (the first char of the 3rd group)v7 — Better for Database Primary Keys
UUID v7 encodes a millisecond timestamp in the first 48 bits, making it lexicographically sortable. This dramatically improves B-tree index performance compared to v4:
npm install uuidv7
import { uuidv7 } from 'uuidv7';
const id = uuidv7();
// "018f4a2b-c3d4-7e5f-a6b7-8c9d0e1f2a3b"
// First segment encodes creation time — rows insert in orderUUID vs Auto-Increment ID — Which Should You Use?
| UUID (v4) | Auto-increment INT | |
|---|---|---|
| Globally unique | ✅ Yes | ❌ Only within one database |
| Enumerable | ✅ Not enumerable (security win) | ❌ Sequential (easy to enumerate) |
| Generate before insert | ✅ Yes (client-generated) | ❌ Must round-trip to DB |
| Index performance | ❌ Random = index fragmentation | ✅ Sequential = optimal |
| Storage size | ❌ 16 bytes (as binary) or 36 chars (as text) | ✅ 4 bytes (INT) / 8 bytes (BIGINT) |
| Distributed systems | ✅ Perfect — no coordination needed | ❌ Requires coordination |
The practical recommendation: Use UUID v7 (or ULID — a similar alternative) when you need globally unique IDs. Use auto-increment BIGINT when you have a single database, don't need to merge data from multiple sources, and want maximum query performance.
Generating UUIDs in Different Languages
// JavaScript / TypeScript (native)
crypto.randomUUID()
// Node.js (native)
import { randomUUID } from 'crypto';
randomUUID()
// Python
import uuid
str(uuid.uuid4())
// PHP
Str::uuid() // Laravel
// or: sprintf('%04x...', ...) for vanilla PHP
// Go
import "github.com/google/uuid"
uuid.New().String()
// Java
UUID.randomUUID().toString()
// C# / .NET
Guid.NewGuid().ToString()
// SQL (PostgreSQL)
gen_random_uuid() -- or uuid_generate_v4() with pgcrypto
// SQL (MySQL 8+)
UUID()Storing UUIDs in Databases
PostgreSQL
Use the native uuid type — stores as 16 bytes, not 36-character text:
CREATE TABLE users (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
email TEXT NOT NULL
);MySQL
Store as BINARY(16) for efficiency, or CHAR(36) for readability:
CREATE TABLE users (
id BINARY(16) PRIMARY KEY DEFAULT (UUID_TO_BIN(UUID(), 1)),
email VARCHAR(255) NOT NULL
);
-- UUID_TO_1 reorders bytes to be time-ordered (like v7)SQLite
Store as TEXT — SQLite has no native UUID type:
CREATE TABLE users (
id TEXT PRIMARY KEY,
email TEXT NOT NULL
);Generate Bulk UUIDs for Testing
Need hundreds of UUIDs for test data? Dummy JSON Generator generates UUID fields as part of a larger dataset — set the UUID field as your ID column, add other fields, and generate as many records as you need. Each record gets a unique v4 UUID automatically.
For just a list of UUIDs with no other fields, generate a single-field schema with uuid type and export as JSON or CSV.