What is a UUID and How to Generate One — Developer Guide
What is a UUID?
A UUID (Universally Unique Identifier) is a 128-bit identifier in the format xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx. UUIDs are designed to be unique across all systems and time without requiring a central registry.
UUID v1 vs v4
UUID v1 is generated from timestamp plus MAC address — time-sortable but exposes hardware identifiers. UUID v4 is purely random with 122 bits of randomness. UUID v4 is the default for most use cases.
When to use UUIDs
Database primary keys: allows records created across systems without ID conflicts. API resource identifiers: avoids exposing sequential database IDs. Session tokens and idempotency keys. File names for uploads to prevent collisions.
Generating UUIDs
Use the UUID Generator to create one or more UUIDs instantly. In code: JavaScript uses crypto.randomUUID() natively in modern browsers. Python uses uuid.uuid4(). Go uses the google/uuid package.
UUID as database primary key
UUID v4 keys have a performance trade-off — random insertion causes B-tree index fragmentation. For write-heavy tables, consider time-ordered identifiers like ULID or UUID v7 instead.
FAQ
Can two UUIDs ever be the same? Theoretically yes, practically no. UUID v4 has 2 to the power of 122 possible values — collision probability is negligibly small.
Are UUIDs suitable for use in URLs? Yes. Standard UUID format is URL-safe. Remove hyphens for shorter URLs.