UUID versions and when to use each
- UUID v1 combines a timestamp with the machine's MAC address — guaranteed unique but reveals creation time and hardware identity, creating privacy concerns.
- UUID v3 and v5 are deterministic hashes: v3 uses MD5, v5 uses SHA-1. Given the same namespace and name, they always produce the same UUID — useful for generating consistent identifiers from natural keys (e.g., converting email addresses to user IDs).
- UUID v4 uses 122 bits of random data, providing the strongest uniqueness guarantee without leaking any information.
- UUID v7 (RFC 9562, 2024) embeds a Unix timestamp in the first 48 bits while keeping 74 random bits — sortable by creation time, making it ideal for database primary keys where index performance matters.
For most applications, v4 is the default choice; switch to v7 for database primary keys where insertion order matters.