Menu

UUID Gotchas: v4 vs v7, Index Bloat, and Other Things Worth Knowing
📰

UUID Gotchas: v4 vs v7, Index Bloat, and Other Things Worth Knowing

DEV Community: webdev·Sanjaya Sahu·10 days ago
#XsUUiese
#devtowebdev#devto#dev#uuid#random
Long article (273 words) — no AI summary yet.
Reading 0:00
15s threshold

Every backend dev has typed uuid() a thousand times. Fewer have asked which UUID version they actually need. Here are the traps I see most.

1. v4 is random, not unique-by-design

UUID v4 has 122 random bits. Collisions are astronomically unlikely, but only if your random source is good. Use a CSPRNG ( crypto.randomUUID() in browsers and Node), never Math.random() -based snippets copied from old Stack Overflow answers.

2. Random UUIDs wreck database index locality

Random v4 primary keys scatter inserts across your B-tree, causing page splits and a bloated index. If you insert a lot of rows, prefer time-ordered IDs such as UUID v7 (or ULID). You keep global uniqueness and get roughly sequential inserts.

3. Case and hyphens are not part of the identity

550E8400-E29B-41D4-A716-446655440000 and 550e8400e29b41d4a716446655440000 are the same UUID. Normalize before comparing or using as a cache key: lowercase, canonical hyphenated form.

4.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.