UUID Version 4 (Random UUID) is the most widely used version of UUID, generated based on cryptographically strong random numbers. It is created according to Section 4.4 of the RFC 4122 standard, "Algorithms for Creating a UUID from Truly Random or Pseudo-Random Numbers."
Example: 9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6d
UUID Version 4 is entirely based on random or pseudo-random numbers and does not rely on timestamps or MAC addresses. This means that the generation of UUID Version 4 is completely random, with no order or pattern, making it ideal for security-sensitive applications.
UUID Version 4 adheres to the standard UUID format, which is 32 characters in the pattern 8-4-4-4-12, displayed as xxxxxxxxxxxx-xxxx-4xxx-xxxx-xxxxxxxxxxxx. Here, 'M' represents the version number, and for Version 4 UUIDs, 'M' is fixed at 4.
UUID Version 4 is suitable for scenarios where completely random unique identifiers need to be generated, such as database primary keys, temporary file names, etc. Due to its randomness, it is also very useful in distributed systems, especially when avoiding ID conflicts is required.
clock_seq_hi_and_reserved part, set the most significant bits (i.e., the 6th and 7th bits) to 0 and 1, respectively.time_hi_and_version field, set the most significant four bits (i.e., bits 12 to 15) to the 4-bit version number 0100.During the generation process of UUID Version 4, specific bits in certain fields are first fixed to indicate the version and clock sequence characteristics, and then all other bits are filled with values through a random or pseudo-random process. This ensures that the generated UUID has global uniqueness and strong randomness.
UUID Version 4 is perfect for:
The probability of UUID v4 collision is extremely low:
In practical terms, you'll never experience a collision in production unless you're generating trillions of UUIDs.
Advantages:
Trade-offs:
Tip: For database primary keys with billions of rows, consider UUID v7 for better indexing performance while maintaining uniqueness.