What is a UUID, and which version should you use?
UUIDs let applications create identifiers independently, without asking a central database for the next number. The version you choose determines where the UUID gets its uniqueness and whether it carries ordering or time information.
What is a UUID?
A Universally Unique Identifier is a 128-bit value. It is commonly written
as 32 hexadecimal characters split into five groups, such as
550e8400-e29b-41d4-a716-446655440000. The version digit and
variant bits describe how the value was constructed.
UUIDs are useful in distributed systems because different devices or services can generate IDs locally with an extremely low risk of collision. They work well for records that may be created offline, merged later, or written by several application instances.
UUID v1 vs v4 vs v7
The short version: compatibility, randomness, or time ordering.
| Version | Built from | Ordering | What it reveals | Best fit |
|---|---|---|---|---|
| v1 | Gregorian timestamp, clock sequence, and node identifier | Time-derived, but its legacy field layout is not ideal for modern indexes | Creation time and potentially node-related information | Legacy protocols and systems that explicitly require UUID v1 |
| v4 | Random or pseudorandom data | No chronological order | No embedded timestamp | General-purpose IDs, API resources, and distributed records |
| v7 | Unix timestamp in milliseconds followed by random data | Time-ordered and naturally sortable | Approximate creation time | Database keys, event streams, and new time-ordered systems |
Which UUID version should you choose?
Choose UUID v4
Use v4 when you need an opaque, general-purpose identifier and do not need IDs to sort by creation time.
Choose UUID v7
Use v7 for new database keys or event data where chronological sorting and better index locality are useful.
Choose UUID v1
Use v1 when an existing protocol or integration requires it. This generator randomizes the node identifier instead of exposing hardware details.