UUID Use Case
UUID as a Primary Key
A UUID primary key lets any node mint a unique row ID without a central counter — ideal for distributed systems and offline-first apps.
Distributed-safeMerge-safePrefer v7
Generated locally with the Web Crypto API — nothing leaves your browser.
What Is UUID as a Primary Key?
- A primary key must be unique and stable. UUIDs satisfy both without a database sequence, so two services can insert rows that never collide even before they sync.
- Use v7 for keys: its leading Unix-millisecond timestamp keeps inserts sequential, which keeps B-tree indexes compact. Plain v4 works but scatters inserts across the index.
How to generate
- Pick v7 so new rows sort by creation time.
- Default the column to a generator (gen_random_uuid() equivalent or app-side v7).
- Index it; consider a BRIN index for very large time-ordered tables.
Use Cases
- Multi-region databases where a single sequence is a bottleneck.
- Offline-first apps that create records before reaching the server.
- Public-facing IDs that should not reveal row count or order.
Code Examples
PostgreSQL
CREATE TABLE orders (
id UUID PRIMARY KEY DEFAULT uuid_generate_v7(),
total numeric
);JavaScript
import { v7 as uuidv7 } from 'uuid';
const id = uuidv7(); // time-orderedFrequently Asked Questions
UUID or BIGSERIAL for a primary key?
UUID scales across nodes and hides volume; BIGSERIAL is smaller and faster but centralised and guessable. Choose UUID when you need distribution or opaque IDs.
Why v7 over v4 for keys?
v7's time prefix makes inserts sequential, avoiding the page splits and fragmentation that random v4 causes in B-tree indexes.
Related tools
UUID v1 GeneratorUUID v3 GeneratorUUID v4 GeneratorUUID v5 GeneratorUUID v6 GeneratorUUID v7 GeneratorUUID v8 GeneratorBulk UUID GeneratorShort UUID GeneratorUUID ValidatorUUID FormatterUUID as a Primary KeyUUID Database IDUUID Foreign KeyUUID Shard KeyUUID Partition KeyUUID in a Composite KeyUUID Surrogate KeyUUID API KeyUUID Session TokenUUID Access TokenUUID Refresh TokenUUID Idempotency KeyUUID Request IDUUID Correlation IDUUID Trace IDUUID CSRF TokenUUID NonceUUID FilenameUUID Object KeyUUID S3 KeyUUID Upload IDUUID Cache KeyUUID Blob NameUUID Temp FileUUID User IDUUID Account IDUUID Customer IDUUID Device IDUUID Tenant IDUUID Client IDUUID Anonymous IDUUID Member IDUUID Message IDUUID Event IDUUID Job IDUUID Task IDUUID Queue Message IDUUID Transaction IDUUID Order IDUUID Invoice IDUUID Payment IDUUID Reset TokenUUID Verification TokenUUID Invite CodeUUID API SecretUUID Share LinkUUID Magic LinkUUID Node IDUUID Worker IDUUID Lock IDUUID Cluster IDUUID Test DataUUID Seed DataUUID FixtureUUID Mock IDUUID Placeholder IDUUID Sample ID