UUID v4 vs v7: which ID format should you use?
8 min read
UUIDs (Universally Unique Identifiers) give you 128-bit IDs you can create without a central counter. Developers reach for them as primary keys, request IDs, file names, and correlation tokens. The two versions you will see most often today are v4 (random) and v7 (time-ordered). Choosing well affects index health, privacy, and how easy logs are to reason about.
UUID v4: random and familiar
Version 4 fills most bits from a cryptographic random source. Collision risk is negligible for normal application volumes. The upside is simplicity and broad library support. The downside for databases is write locality: random primary keys scatter inserts across B-tree pages, which can increase fragmentation and IO under heavy insert load compared with sequential keys.
Use v4 when you need opaque IDs, do not care about sort order, or are integrating with systems that already expect classic random UUIDs.
UUID v7: time-ordered for databases
Version 7 embeds a Unix timestamp in the high bits so IDs roughly sort by creation time. That makes them friendlier as primary keys: newer rows tend to land near each other on disk. You still get uniqueness from random bits in the remainder of the value.
Trade-offs exist. Time-ordered IDs leak approximate creation time to anyone who can see them. If that metadata is sensitive in your threat model, prefer v4 or another opaque scheme. Also confirm your language and database stack support v7 before standardizing — adoption is growing but not universal yet.
Practical recommendations
- Default to v4 for public tokens, client-visible IDs, and mixed legacy systems.
- Prefer v7 for high-volume database primary keys when timestamp leakage is acceptable and your stack supports it.
- Do not treat UUIDs as secrets by themselves. If an ID grants access, protect the endpoint with auth — guessing is hard, but leakage still happens via logs and referrers.
- Standardize formatting in APIs (lowercase vs uppercase, hyphens vs compact) so clients do not invent variants.
Generate them locally
When you need a batch for fixtures, migrations, or manual testing, use a free UUID generator that runs in the browser. GenShelf supports v4 and v7 with formatting toggles (hyphens, uppercase, braces) so you can match the style your codebase expects without pasting from random blogs.
For demo datasets, pair UUIDs with realistic placeholder people from the fake name generator so staging screens do not look empty or obviously synthetic.
Try it on GenShelf: Free UUID Generator