UUID has multiple versions, each with different characteristics:
• v1: Based on time and node ID, ordered but may leak information
• v3: Based on name and namespace, using MD5 hash
• v4: Completely randomly generated, the most commonly used version
• v5: Based on name and namespace, using SHA-1 hash, more secure than v3
• v6: Improved version of v1, time part arranged in big-endian order, more suitable for sorting
• v7: New version based on Unix timestamp, combining time sequence and randomness
How to choose the right UUID version
Choose the most suitable version based on the usage scenario:
• Need security: Choose v4 (random) or v5 (name based on SHA-1)
• Need sorting: Choose v6 or v7 (time-based and easy to sort)
• Need determinism: Choose v3 or v5 (same input produces same output)
• Need performance: Choose v1 (fast generation speed)
• Special values: NIL (all 0) or MAX (all F) for boundary cases
Randomness vs determinism
UUID v4 is completely random, suitable for most application scenarios. v3 and v5 are deterministic, the same name and namespace always generate the same UUID, suitable for scenarios requiring consistent mapping. v1, v6, and v7 contain time information and have sequentiality under specific conditions.
Temporality and sequentiality
v1, v6, and v7 all contain time information but handle it in different ways. v6 improves the time sorting issue of v1, v7 uses Unix timestamp to provide simpler temporality. If you need UUIDs sorted by generation time, v6 and v7 are better choices.
v1 may leak generation time and node information (MAC address). v4 is completely random and is the best choice for privacy-sensitive scenarios. v3 uses MD5 hash, which is less secure than v5 using SHA-1. NIL and MAX are fixed values and should not be used in security scenarios requiring uniqueness.
UUID is mainly used in scenarios requiring globally unique identifiers: distributed systems, database primary keys, session identifiers, file names, API tokens, etc. Choosing the right version can optimize performance, security, and data organization.