What is a UUID?
UUID (Universally Unique Identifier) is a 128-bit identifier standardized in RFC 9562. UUIDs are formatted as 32 hexadecimal digits in 5 groups separated by hyphens: xxxxxxxx-xxxx-Mxxx-Nxxx-xxxxxxxxxxxx, where M is the version and N indicates the variant.
UUIDs are used everywhere: database primary keys, API resource identifiers, session tokens, file names, and distributed system IDs. They can be generated independently on any machine without coordination.
UUID v4: Random
UUID v4 is the most widely used version. It fills 122 of the 128 bits with cryptographically secure random data (6 bits are reserved for version and variant). The probability of generating two identical v4 UUIDs is astronomically low: you would need to generate about 2.71 quintillion UUIDs for a 50% collision chance.
The generator above works as a uuid4 generator: click Generate (or Bulk Generate) to generate a random UUID online with no setup, then copy it with one click.
UUID v4 is suitable for most use cases but has one drawback: random values cause fragmentation in B-tree database indexes. For database primary keys, consider UUID v7 instead.
UUID v7: Timestamp-Based
UUID v7 is the newest version, combining a 48-bit Unix timestamp (millisecond precision) with random bits. This makes UUIDs both unique and chronologically sortable. Because new IDs are always greater than previous ones, they perform well as database primary keys without causing B-tree page splits.
UUID v7 is the recommended choice for new applications that need unique identifiers with natural time ordering. For more on time-sortable distributed IDs, see our post on How Snowflake IDs Work.
UUID vs Snowflake ID
- Size: UUID is 128 bits (36 characters with hyphens). Snowflake ID is 64 bits (up to 19 digits).
- Sortability: UUID v7 and Snowflake IDs are both time-sortable. UUID v4 is not.
- Database performance: Snowflake IDs are more compact and efficient as primary keys. UUID v7 is a good middle ground.
- Adoption: UUIDs are universally supported. Snowflake IDs require custom generation logic.
Use our Snowflake ID Decoder to decode Discord, Twitter, and Instagram IDs.
UUIDs as Primary Keys in Cloud Databases and Microservices
Many teams use UUIDs as primary keys in managed PostgreSQL or MySQL services because each service can mint IDs without a central allocator. That fits microservices and public APIs: a new row or resource gets a stable identifier before any cross-service round trip.
The tradeoff is index layout. Random UUID v4 keys scatter inserts across a B-tree; UUID v7 (or other time-ordered schemes) keeps new keys mostly at the end of the index. For how indexes behave on disk, see database indexing explained. When you are choosing a managed engine, PostgreSQL vs MongoDB vs DynamoDB compares common operational models; for a concrete production-style stack, Django and PostgreSQL from zero to production walks through a full relational setup.
For REST-style APIs, UUIDs also work well in URLs and JSON bodies as opaque resource IDs, as long as you treat them as strings and keep generation cryptographically sound (this tool uses crypto.getRandomValues() in the browser).