UUID v4 or v7?
| v4 | v7 | |
|---|---|---|
| Built from | 122 random bits | 48-bit Unix time in ms + 74 random bits |
| Sort order | Random | By creation time |
| Best for | Tokens, public IDs where order leaks nothing | Database primary keys: inserts stay in index order |
v7 was standardised in RFC 9562 (2024). Because new values sort after old ones, it avoids the index fragmentation that random v4 keys cause in databases such as PostgreSQL and MySQL. Note that a v7 UUID reveals when it was created.
UUID vs GUID
They are the same thing. GUID is Microsoft's name, often written in uppercase and wrapped in braces: {3F2504E0-4F89-41D3-9A0C-0305E82C3301}.
Can two UUIDs collide?
In practice, no. With 122 random bits you would need to generate about a billion v4 UUIDs a second for 85 years to have a 50% chance of one duplicate.
Generate a UUID in code
JavaScript crypto.randomUUID() Python import uuid; uuid.uuid4() (uuid.uuid7() in 3.14+) PostgreSQL gen_random_uuid() (uuidv7() in 18+) Bash uuidgen C# Guid.NewGuid() (Guid.CreateVersion7() in .NET 9+)