๐ Technical Guide: What is a UUID and how to choose the right version?
1. What is a UUID / GUID?
A UUID (Universally Unique Identifier) or GUID (Globally Unique Identifier in the Microsoft ecosystem) is a 128-bit number designed to guarantee global uniqueness without requiring a central coordination authority.
2. Comparison: UUID v4 vs UUID v7 for Databases
UUID v4 is entirely random. In relational databases with B-Tree indexes (such as PostgreSQL, MySQL, or SQLite), inserting millions of random v4 keys causes severe index fragmentation and degrades disk throughput.
UUID v7 (RFC 9562) solves this by placing a 48-bit Unix millisecond timestamp at the beginning of the identifier. This enables sequential, append-only disk writes with the speed of auto-incrementing integers and the security of decentralized UUIDs.
3. Is there a realistic probability of UUID collisions?
UUID v4 offers 2^122 unique combinations (approx 5.3 ร 10^36). The probability of collision is practically zero across real-world systems.
โ Frequently Asked Questions about UUID & GUID
What is the difference between a UUID and a GUID?
They are structurally identical (128-bit numbers). GUID is Microsoft terminology, whereas UUID is the open IETF standard governed by RFC 4122 and RFC 9562.
Why should you migrate from UUID v4 to UUID v7?
UUID v7 is naturally time-ordered. By inserting sequentially into B-Tree indexes, it avoids random page splits and can reduce write latency by up to 40%.
How to efficiently store UUIDs in SQL databases?
In PostgreSQL use the native `uuid` type (16 bytes). In MySQL/MariaDB or SQLite, use `BINARY(16)` instead of `VARCHAR(36)` to save 55% storage and index cache.