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.
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.