What Is UUID and How It Works
UUID (Universally Unique Identifier) is a standardized 128-bit identifier format defined in RFC 9562 (formerly RFC 4122). The core idea is the ability to generate unique identifiers on any device, at any time, without needing to verify uniqueness through a central server.
Format
A UUID is written as 32 hexadecimal characters separated by four hyphens into five groups: 8-4-4-4-12. For example: 550e8400-e29b-41d4-a716-446655440000. The character at position 13 indicates the version (1–7), and the first character of the fourth group defines the variant (8, 9, a, or b for a standard UUID).
Why UUID v4 Is the Most Popular
UUID v4 uses a cryptographically secure pseudorandom number generator (CSPRNG). Of 128 bits, 6 are reserved for version and variant, leaving 122 bits of entropy. This yields 5.3 × 10^36 possible combinations. UUID v4 is not tied to time or hardware, ensuring complete anonymity of generation.
UUID in Databases
Using UUID as a primary key has advantages: safe data merging from different sources, the ability to generate IDs on the client without a database round-trip, and no information leak about record creation order. Downsides include larger size (16 bytes versus 4–8 for INT) and index fragmentation for v4. UUID v7 solves the fragmentation issue with its time prefix.
UUID v7 — The New Standard
UUID v7, standardized in RFC 9562 (2024), combines the advantages of UUID v1 (time-based sorting) and v4 (randomness). The first 48 bits are a Unix timestamp in milliseconds, the rest are random. This makes v7 ideal for database primary keys where insert order matters.