Understanding UUIDs
A Universally Unique Identifier (UUID) is a 128-bit label used for information in computer systems. The probability of a duplicate UUID is so close to zero that for all practical purposes, it is zero.
Why use UUIDs over Integers?
Traditional databases often use auto-incrementing integers (1, 2, 3...) for IDs. However, UUIDs offer distinct advantages in distributed systems:
- Uniqueness: You can generate IDs on different servers without coordinating a central counter.
- Security: UUIDs are not predictable. A hacker cannot guess the URL of resource #100 by seeing resource #99.
V4 vs. V7 Performance
Version 4 (Random)
Completely random. Excellent for security but bad for database performance (B-Tree fragmentation) because new IDs are inserted randomly.
Version 7 (Time-Sorted)
Includes a timestamp. This makes new IDs "greater than" old ones, allowing databases to append them efficiently. Best for Primary Keys.