What Is a UUID v4 and When Should You Use It?

A friendly guide to those weird long IDs you see everywhere

🆔 🎲 🔑 🌍

TL;DR: A UUID v4 is a randomly generated 128-bit ID that's practically guaranteed to be unique across the entire universe. Use it when you need IDs without a central authority (distributed systems, client-side generation, security). Skip it when a simple auto-incrementing integer will do.

You've seen them before. Those impossibly long strings of letters and numbers lurking in URLs, database rows, and API responses. Something like 550e8400-e29b-41d4-a716-446655440000. They look like a cat walked across a keyboard, but they're actually one of the most useful tools in a developer's toolkit.

Let's break down what UUID v4 is, why it exists, and when you should (and shouldn't) reach for one.

So... What Exactly Is a UUID?

UUID stands for Universally Unique Identifier. It's a 128-bit value typically shown as a 36-character string with hyphens: 8-4-4-4-12 hex digits.

550e8400-e29b-41d4-a716-446655440000

Think of it like a Social Security Number, but for data. Except instead of 9 digits, you get 32 hex characters, and instead of the government assigning them, anyone can generate one at any time, anywhere, and it'll be unique.

🎰 UUID v4 generation: basically rolling a 122-sided cosmic die

There are several UUID versions, each with a different recipe:

How UUID v4 Actually Works

Of those 128 bits, 122 are randomly generated. The remaining 6 bits are reserved to identify the version (v4) and variant (RFC 4122). That gives you 2122 possible values — roughly 5.3 x 1036. That's 5.3 undecillion. Yes, that's a real word.

You can spot a v4 UUID by the 4 at the start of the third group:

550e8400-e29b-41d4-a716-446655440000
                ^
          "Hey, I'm version 4!"

Will I Ever Get a Duplicate?

😄

Fun Fact: To have a 50% chance of a UUID collision, you'd need to generate 2.71 quintillion of them. If you made a billion per second, that would take about 86 years. You'd retire long before your UUIDs would collide.

Short answer: no. The math is overwhelmingly in your favor. For all practical purposes, UUID v4 collisions don't happen. You're more likely to be struck by a meteorite while winning the lottery.

Generating UUIDs — It's Easier Than You Think

JavaScript (Browser)

Modern browsers have this built right in:

// One line. That's it. Seriously.
const id = crypto.randomUUID();
// "3b241101-e2bb-4d7a-8702-9e057c10f73a"

// Need to support older browsers? Here's a fallback:
function uuidv4() {
  return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(
    /[xy]/g,
    function (c) {
      const r = (Math.random() * 16) | 0;
      const v = c === 'x' ? r : (r & 0x3) | 0x8;
      return v.toString(16);
    }
  );
}

Node.js

import { randomUUID } from 'node:crypto';
const id = randomUUID();
// "1b9d6bcd-bbfd-4b2d-9b5d-ab8dfbbd4bed"

Python

import uuid
id = uuid.uuid4()
print(id)  # e.g. "6ba7b810-9dad-11d1-80b4-00c04fd430c8"

PostgreSQL

-- Built-in since PostgreSQL 13
SELECT gen_random_uuid();

-- Or with the extension for older versions
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
SELECT uuid_generate_v4();
🐍 Python, JS, SQL — every language speaks UUID

When UUIDs Are Your Best Friend

When UUIDs Are Overkill

⚠️

Watch Out: UUIDs are 16 bytes (or 36 as a string) compared to just 4 bytes for an integer. On tables with billions of rows and multiple foreign keys, that storage difference adds up fast.

The New Hotness: UUID v7

UUID v7 (RFC 9562, published in 2024) is like v4's cooler younger sibling. It embeds a timestamp in the first 48 bits, which means your IDs are roughly sortable by creation time. Database indexes love sequential data, so v7 gives you the best of both worlds: uniqueness AND performance.

💡

Pro Tip: Starting a new project? Consider UUID v7 as your default. Use v4 only when you specifically don't want any temporal information embedded in the ID.

Storing UUIDs: Don't Use VARCHAR!

Always store UUIDs in their native binary format when your database supports it:

-- PostgreSQL: native uuid type (16 bytes, efficient)
CREATE TABLE users (
  id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  email TEXT NOT NULL
);

-- MySQL: BINARY(16) for performance
CREATE TABLE users (
  id BINARY(16) PRIMARY KEY,
  email VARCHAR(255) NOT NULL
);
INSERT INTO users (id, email)
VALUES (UUID_TO_BIN(UUID(), true), 'user@example.com');

Storing UUIDs as VARCHAR(36) wastes over twice the space and slows down comparisons. If your ORM defaults to string storage, override it. Your future self will thank you.

Quick Reference Table

PropertyAuto-incrementUUID v4UUID v7
Size4-8 bytes16 bytes16 bytes
SortableYesNoYes (by time)
GuessableYesNoPartially
Distributed-safeNoYesYes
Index perfExcellentPoorGood
🏆 UUID v7: the "best of both worlds" champion

Wrapping Up

UUID v4 is still one of the most practical ways to generate unique IDs in distributed systems. No coordination needed, virtually zero collision risk, and every major language and database supports it out of the box. Just be mindful of storage costs and index performance — and give UUID v7 a serious look if you're starting something new.

Now go forth and generate some beautifully random identifiers!

Try It Yourself

Generate UUID v4 values instantly in your browser — no sign-up, no backend, completely free.

Open UUID Generator →