Should I use UUID v4 or v7?
Use UUID v7 for database primary keys or any application where sortability is important. Unlike v4 (which is purely random), v7 includes a timestamp, making it lexicographically sortable. Use UUID v4 when you need pure randomness and don’t care about the order of creation.
The Indexing Nightmare of UUID v4
When you use v4 as a database key, your database has to insert records into random locations in its index. This causes "page fragmentation" and slows down writes as your table grows. When I switched a high-volume logging service from v4 to v7, we saw a measurable decrease in disk I/O because the new records were always being appended to the end of the index.
Collision Resistance
Don’t worry about collisions—v7 still has plenty of randomness (over 70 bits) to ensure uniqueness across distributed systems, just like v4. You get the best of both worlds: sortability and uniqueness.
Frequently Asked Questions
Is UUID v7 a standard?
Yes, it was officially standardized in RFC 9562 in May 2024. It is now the recommended version for most modern applications.