Everything you need to use UUIDs correctly in a real project: how to store them efficiently, what they do and do not protect, and how to generate them across languages and frameworks.

What these guides cover

UUID generation is a single function call in most languages. The harder questions are about what happens next: which column type to use, why your database index slows down after a million rows, what a UUID in a URL actually leaks, and when to reach for v7 instead of v4. These guides answer those questions with concrete recommendations rather than general overviews.

Choosing the right UUID version

The practical choice for new projects in 2026 is between v4 and v7. UUID v4 is 122 bits of cryptographic randomness — unpredictable, no timestamp, nothing derivable about when or where it was created. UUID v7 embeds a 48-bit Unix millisecond timestamp in its leading bits, making it time-sortable and index-friendly. The UUID Versions guide covers all eight versions defined by RFC 9562, including which are deprecated (v1, v2, v6) and when name-based v5 is the right choice.

UUIDs and database performance

Random UUID v4 primary keys cause B-tree page splits on every insert because each new value lands at a random position in the index. At scale — hundreds of thousands of rows — this fragments the index, increases cache misses, and measurably slows both writes and range reads. UUID v7 eliminates this problem: its timestamp prefix means consecutive inserts cluster near the end of the index, producing behaviour close to an auto-increment integer. The UUID in Databases guide covers storage types, index behaviour, and migration paths for PostgreSQL, MySQL, SQLite, SQL Server, and MongoDB.

Security and what UUIDs are not

A UUID is an identifier, not a secret. Even UUID v4, with its 122 bits of randomness, should not be used as a session token or capability grant on its own — UUIDs appear in URLs, server logs, referrer headers, and anywhere HTTP traffic is recorded. The UUID Security guide explains what v4 actually guarantees, what v7 leaks (creation timestamp to the millisecond), and what to use instead when you genuinely need a secret.

Language guides

Each language guide shows the idiomatic way to generate, parse, and store UUIDs — covering the standard library where it exists and the canonical third-party package where it does not. All guides include v7 examples, database storage patterns, and framework-specific integration (ORMs, web frameworks, type systems).

All guides

Fundamentals UUID Versions

All eight RFC 9562 versions explained — what each one does, which are deprecated, and how to choose between v4 and v7 for new work.

Databases UUID in Databases

Storage types, B-tree index behaviour, PostgreSQL and MySQL setup, and how to migrate an existing table from v4 to v7.

Security UUID Security

What the randomness in v4 actually guarantees, what v7 leaks, and when to use a real secret instead of a UUID.

Fundamentals UUID vs GUID

GUID is Microsoft's name for the same 128-bit standard. How the terms differ, SQL Server's byte-order caveat, and .NET's Guid type.

Comparison UUID vs ULID

Both are 128-bit time-sortable identifiers. How encoding, monotonicity, and database support differ, and when to choose each.

JavaScript UUID in JavaScript

crypto.randomUUID(), the uuid npm package, React and Vue patterns, Prisma, Drizzle, and a TypeScript branded-type recipe.

Python UUID in Python

The built-in uuid module, v7 in Python 3.13+, the uuid6 backport, and patterns for Django, SQLAlchemy, Pydantic and FastAPI.

Go UUID in Go

google/uuid for v4 and v7, parsing and validation, pgx and database/sql integration, GORM hooks, and typed UUID patterns.

Rust UUID in Rust

uuid crate Uuid::now_v7(), Diesel and SQLx integration, serde JSON serialization, and choosing v4 vs v7 for database primary keys.

Java UUID in Java

java.util.UUID for v4, Hibernate 6.5 @UuidGenerator for v7, the java-uuid-generator (JUG) library, Spring Boot JPA entity patterns, and PostgreSQL storage.

C# UUID in C# and .NET

System.Guid.NewGuid() for v4, Guid.CreateVersion7() in .NET 9+ for v7, EF Core entity key patterns, and the SQL Server byte-order caveat.

PHP UUID in PHP and Laravel

ramsey/uuid for v4 and v7, Laravel HasUuids trait, Str::uuid(), MySQL BINARY(16) storage, and wp_generate_uuid4() for WordPress.

Fundamentals UUID Namespaces

Deterministic UUIDs with v3 and v5 — how namespaces scope the hash, predefined RFC 9562 namespaces, and when to use name-based UUIDs.

Architecture UUID in Kubernetes

How Kubernetes assigns UUID v4 to resources, using UUID v7 for request correlation IDs, and OpenTelemetry trace ID patterns in cloud-native deployments.

Architecture UUID in GraphQL

Using UUID as the GraphQL ID scalar, defining a custom UUID scalar with validation, UUID v7 for cursor-based pagination, and client-side ID generation for optimistic mutations.

Fundamentals UUID Byte Layout

The complete 128-bit UUID field layout — every bit group, version and variant positions, and how v4 and v7 differ at the byte level with annotated hex examples.

Architecture UUID in Mobile Apps

Generating UUID v4 and v7 in iOS (Swift), Android (Kotlin/Java), and React Native — offline ID creation, sync patterns, and primary key choices.

Frequently asked questions

Should I use v4 or v7?

Use v7 for database primary keys (time-sortable, index-friendly) and v4 for anything where creation order could leak information, like tokens or share links.

Where can I learn more about UUID best practices?

The Guides section covers UUID versions, databases, security, JavaScript, Python, Go, UUID vs GUID, and UUID vs ULID — each with concrete recommendations for real projects.

What is the best UUID version to use as a database primary key?

UUID v7 is the recommended choice for new projects. Its 48-bit millisecond timestamp prefix keeps B-tree index inserts sequential, avoiding the page fragmentation that UUID v4 causes at scale. PostgreSQL 18 ships a native uuidv7() function; earlier versions can use the pg_uuidv7 extension. Use UUID v4 for external-facing identifiers where the embedded timestamp would be a privacy concern.

How do I store UUIDs efficiently in a database?

Use the native UUID column type where available: PostgreSQL's uuid (16 bytes), MySQL's BINARY(16) with UUID_TO_BIN(), or SQLite's BLOB. Never store UUIDs as VARCHAR(36) — it wastes 2.25× the space and slows index comparisons. The full storage guide with SQL examples is at UUID in Databases. The GeeksforGeeks UUID primary key guide covers the performance trade-offs.

Is UUID generation safe to do in a browser?

Yes. Modern browsers provide crypto.randomUUID() (Chrome 92+, Firefox 95+, Safari 15.4+) backed by the OS CSPRNG. This generates RFC 9562-compliant UUID v4. For UUID v7 in the browser, use the uuid npm package. Never use Math.random() for UUID generation — it is not cryptographically secure.