Which version to pick
v4 is 122 random bits: the safe default for ids that must not reveal anything, such as tokens in URLs. v7 starts with a millisecond timestamp, so ids sort in creation order: database indexes stay compact and inserts stay fast, which is why it suits primary keys. Both are standard RFC 9562 UUIDs.
How they are generated
Random bits come from the browser's cryptographic generator (Web Crypto), not Math.random. v7 ids generated in the same millisecond still increase, so a batch keeps its order. Copy one, or up to 100 at a time, upper or lower case, with or without dashes.
Anatomy of a UUID
A UUID is displayed as 32 hexadecimal characters in five groups separated by dashes: 8-4-4-4-12. For example: 550e8400-e29b-41d4-a716-446655440000. The third group's first character indicates the version (4 for v4, 7 for v7). The fourth group's first character indicates the variant (8, 9, a, or b for the standard variant of RFC 9562).
The total is 128 bits (16 bytes). In v4, 122 of these bits are random. In v7, the first 48 bits are a millisecond Unix timestamp, 4 bits are the version, 2 bits the variant, and the remaining 74 bits are random; here they count up within the same millisecond, so a list of v7 UUIDs is always in creation order.
UUIDs as database primary keys
Using UUIDs as primary keys offers several advantages: no collisions when merging data from multiple sources, no need for a centralized ID generator, no sequential IDs that reveal business metrics (order count, user count), and the ability to generate IDs on the client side before inserting into the database.
The main drawbacks are size (16 bytes vs 4 bytes for a 32-bit integer) and, for v4, random distribution that can cause B-tree index fragmentation and slower inserts. UUID v7 solves the fragmentation problem by being time-ordered, making inserts sequential and index-friendly.