What is a UUID?
A UUID (Universally Unique Identifier), also called a GUID (Globally Unique Identifier), is a 128-bit identifier standardized by RFC 4122. The standard format is 32 hexadecimal digits arranged in 5 groups: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx.
UUIDs are used in databases as primary keys, in distributed systems to avoid ID collisions, in file names, and in API tokens. The probability of generating two identical v4 UUIDs is astronomically small — roughly 1 in 5.3×10³⁶.
FAQ
What is the difference between v4 and v1?
UUID v4 is randomly generated — it's the most commonly used version for security and uniqueness. UUID v1 is based on the current timestamp and MAC address, making it sortable by time but potentially revealing system information.
Is it safe to use UUIDs as database primary keys?
Yes, UUIDs are widely used as primary keys. However, they are larger than integer IDs (16 bytes vs 4–8 bytes) and random UUIDs (v4) can cause index fragmentation in MySQL/PostgreSQL due to non-sequential insertion. Consider ULIDs or v7 UUIDs for better database performance.