← All Articles Β· Β· 2 min read

UUID v4 Guide: When to Use UUIDs Instead of Auto-Increment IDs

What are UUIDs, why use them over auto-increment integers, and how to generate them in JavaScript, Python, and Node.js. Includes performance tradeoffs and use cases.

uuiddatabaseapijavascriptbackend

UUID v4 Guide: When to Use UUIDs Instead of Auto-Increment IDs

What is a UUID?

A UUID (Universally Unique Identifier) is a 128-bit identifier formatted as 36 characters.

xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx
Example: 550e8400-e29b-41d4-a716-446655440000

UUID v4 (Random)

UUID v4 generates 122 random bits + 4 fixed bits. Probability of collision is essentially zero for any practical application.

// Browser
const uuid = crypto.randomUUID();  // native browser API
// Output: "f47ac10b-58cc-4372-a567-0e02b2c3d479"

// Node.js
import { v4 as uuidv4 } from 'uuid';
console.log(uuidv4());  // requires 'uuid' package

Why Not Auto-Increment?

FeatureAuto-IncrementUUID v4
Exposes record countβœ… Yes❌ No
URLs are guessableβœ… Yes❌ No
Merge across databases❌ Difficultβœ… Easy
Distributed generation❌ Requires coordinationβœ… Independent
Security❌ Sequentialβœ… Random

When UUIDs Make Sense

βœ… Microservices: Each service generates IDs independently βœ… Offline-first apps: IDs created without database connection βœ… Public APIs: Don’t expose internal record counts βœ… Merging datasets: No ID collisions across systems

Performance Tradeoffs

AspectAuto-IncrementUUID v4
Index size4-8 bytes36 bytes
Insert performanceβœ… Fast (sequential)⚠️ Slower (random, index fragmentation)
Read performanceβœ… Fastβœ… Same
Database sizeSmaller~5x larger indexes

Solution for performance: Use UUID v7 (time-ordered UUID) β€” combines time sorting with uniqueness.

Python Example

import uuid

# UUID v4 (random)
uid = uuid.uuid4()
print(uid)  # UUID('f47ac10b-58cc-4372-a567-0e02b2c3d479')
print(str(uid))  # f47ac10b-58cc-4372-a567-0e02b2c3d479

# UUID v7 (time-ordered, recommended for databases)
uid7 = uuid.uuid7()

When to Stick with Auto-Increment

Internal-only records with no public API exposure High-volume inserts where index performance matters Single-database applications with no merging needs

Free Newsletter

Level Up Your Dev Workflow

Get new tools, guides, and productivity tips delivered to your inbox.

Plus: grab the free Developer Productivity Checklist when you subscribe.

Found this guide useful? Check out our free developer tools.