UUIDs explained: v1 vs v4 vs v7, and when to use each
A UUID is a 128-bit identifier you can generate anywhere without coordination and still expect to be unique. But "UUID" is a family of versions with very different properties — picking the wrong one hurts your database.
The format
A UUID is 128 bits, shown as 32 hex digits in five dash-separated groups:
550e8400-e29b-41d4-a716-446655440000
^
version digitThe version digit (the first character of the third group) tells you how it was generated. Generate any version in the UUID Generator.
v4 — random
Version 4 is 122 random bits. It is the default choice: no machine info leaks, and collisions are astronomically unlikely (you would need billions of UUIDs before the odds become meaningful).
Downside: randomness means new IDs scatter across a database index, hurting insert performance and locality on large tables.
v1 — time + MAC
Version 1 encodes a timestamp and the machine’s MAC address. IDs are roughly sortable by creation time, but they can leak the MAC address and creation time — a privacy concern. Rarely the right choice today.
v7 — time-ordered (the modern pick)
Version 7 puts a Unix millisecond timestamp in the high bits, followed by random bits. You get the uniqueness of v4 and roughly sortable, index-friendly IDs — ideal for database primary keys.
- Use v4 for general-purpose IDs, tokens, and when ordering does not matter.
- Use v7 for database primary keys where insert locality matters.
- Avoid v1 unless you specifically need its legacy behavior.
Frequently asked questions
- Are UUID collisions something I should worry about?
- For v4, practically no. With 122 random bits you would need to generate on the order of a billion UUIDs per second for many years before a collision becomes likely.
- What is the difference between UUID and GUID?
- They are the same thing. GUID is Microsoft’s name for the same 128-bit identifier standard.
- Which UUID version is best for database primary keys?
- UUID v7. Its time-ordered prefix keeps new rows near each other in the index, avoiding the write amplification that random v4 keys cause.
- Does UUID v4 leak any information?
- No. It is almost entirely random, unlike v1 which embeds a timestamp and MAC address.
Try it yourself
Put this guide into practice — these tools run free in your browser.