Tutorial2024-03-14

What Is a UUID? A Beginner's Guide to Unique Identifiers

UUIDs are everywhere in modern software — from database keys to API tokens. But what exactly are they, and why not just use auto-incrementing numbers?

#uuid#database#beginner#web-development

Hey! If you've ever seen a string like 550e8400-e29b-41d4-a716-446655440000 and wondered "what is that?" — you're in the right place. I'm going to explain UUIDs in plain English, step by step.

What Is a UUID?

UUID stands for Universally Unique Identifier. It's a 128-bit number that's (practically) guaranteed to be unique across all computers, all networks, all time.

Think of it like a social security number, but for data instead of people. Every piece of data gets its own UUID, and no two pieces of data will ever have the same one.

A UUID looks like this: 550e8400-e29b-41d4-a716-446655440000

That's 32 hexadecimal characters, grouped as 8-4-4-4-12 with hyphens.

Why Not Just Use 1, 2, 3, 4...?

Auto-incrementing IDs (1, 2, 3...) seem simpler. And they are! But they have problems:

  1. Predictable — If your order ID is #1234, I know #1235 exists. That's a security issue.
  2. Centralized — You need a database to generate them. No database = no IDs.
  3. Merge conflicts — Combine two databases? IDs collide. Game over.

UUIDs solve all three problems. They're random, generated without a central authority, and virtually impossible to collide.

How Unique Is "Universally Unique"?

The math: there are 2^122 possible UUIDs (version 4). That's about 5.3 × 10^36.

To put that in perspective: if you generated 1 billion UUIDs per second, it would take ~168 quintillion years to have a 50% chance of a single collision. The universe is only 13.8 billion years old.

So yeah. They're unique enough.

The 5 Versions of UUIDs

Version How It's Generated Use Case
v1 Timestamp + MAC address Logging, ordering by time
v2 DCE security (rare) Legacy systems
v3 MD5 hash of namespace + name Deterministic IDs
v4 Random (most common) General purpose
v5 SHA-1 hash of namespace + name Deterministic, safer than v3

Version 4 (random) is what you'll use 95% of the time.

How to Generate UUIDs

In JavaScript

// Modern browsers and Node.js 19+
const id = crypto.randomUUID();
console.log(id); // "550e8400-e29b-41d4-a716-446655440000"

In Python

import uuid
print(uuid.uuid4())  # Random UUID

Online

Use a UUID Generator to generate single or bulk UUIDs instantly.

UUIDs as Database Keys

Here's my recommendation for beginners:

  • Use UUIDs for anything exposed to users (API keys, order IDs, public-facing resources)
  • Use auto-increment for internal-only tables (audit logs, internal counters)
  • Never use UUIDs for high-frequency insert tables without understanding B-tree fragmentation

Common Mistakes

  1. Storing UUIDs as VARCHAR(36) — Use UUID or BINARY(16) type instead. Saves 50% storage.
  2. Using UUIDs as clustered index — Random UUIDs cause page splits. Use sequential UUIDs (v1 or v7) for the primary key.
  3. Parsing UUIDs manually — Don't split on hyphens. Use your language's UUID library.

Generate unique UUIDs instantly with our free UUID Generator — supports v4, bulk generation, and copy-to-clipboard.

🛠

Try It Yourself

Put what you've learned into practice with our free online tools.