What is a UUID or GUID?
A UUID (Universally Unique Identifier), also called a GUID (Globally Unique Identifier) in the Microsoft ecosystem, is a standardized 128-bit unique identifier. It is usually represented by 32 hexadecimal characters, separated by 4 hyphens in the form 8-4-4-4-12 (example: 550e8400-e29b-41d4-a716-446655440000).
Why use a UUID v4 in a database?
Historically, databases (such as MySQL or PostgreSQL) use an auto-incremented integer (1, 2, 3...) as the primary key (ID). While efficient, this poses a major security risk: anyone can guess the URL of a user or an invoice simply by changing the number in the address bar (e.g.: /invoice/12 replaced by /invoice/13).
L'UUID version 4 is generated completely at random. It is impossible to guess, which effectively protects the resources of your API and web applications (non-sequential ID).
Is there a risk of creating the same UUID twice? (Collision)
The probability of generating two identical UUID v4 values (a collision) is so close to zero that it is considered mathematically negligible. The number of possible combinations is 2^122 (about 5.3 × 10^36). To have a 1 in 1 billion chance of getting a duplicate, you would need to generate 103 trillion UUIDs!