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. Do not use UUIDs as secrets
A UUID is an identifier, not an authorization token. v1 even leaks a timestamp and MAC-derived node id. Use 128+ bits from a proper token generator for reset links and API keys.
5. Store them as 16 bytes
A UUID stored as CHAR(36) is 36 bytes plus overhead. Use the native uuid type in Postgres or BINARY(16) in MySQL.
Quick generation and validation
When I just need a batch of IDs for a fixture, or want to check what version a UUID is, I use a client-side tool so nothing leaves the browser: UUID Generator. It runs locally using the Web Crypto API.
Disclosure: I built WebDevToolbox, a free collection of 500+ browser-based dev tools. Everything runs client-side.
Top comments (0)