DEV Community

zhihu wu
zhihu wu

Posted on

Why UUIDs Belong in Every Developer's Toolbelt (Not Just Your Database)

Most developers first encounter UUIDs when setting up database primary keys. But UUIDs solve problems far beyond the database — and once you start using them, you'll wonder why you ever reached for auto-increment.

The Problem with Sequential IDs

Auto-increment IDs are fast and small, but they leak information. Every /users/42 tells an attacker there are at least 42 users. Every /orders/1087 reveals your order volume. In a REST API, sequential IDs are an enumeration attack waiting to happen.

UUIDs fix this by being unguessable. /users/550e8400-e29b-41d4-a716-446655440000 tells an attacker nothing.

Beyond the Database: Where UUIDs Shine

Idempotency Keys. When processing payments or API calls, the client generates a UUID as an idempotency key before sending the request. If the network drops and the client retries, the server recognizes the same key and avoids double-charging. Stripe's API requires this pattern.

Optimistic UI. In collaborative apps like Figma or Notion, the client generates UUIDs for new elements before the server confirms. This gives instant feedback — no waiting for a database round-trip to get an ID.

File Uploads. Use UUIDs for uploaded filenames instead of the user's original filename. /uploads/550e8400-e29b-41d4-a716-446655440000.pdf avoids naming conflicts and doesn't leak the original filename.

Distributed Systems. When multiple services generate IDs independently, auto-increment fails — they'd collide. UUIDs let every service create IDs without coordination.

Generating UUIDs in Your Stack

// Browser & Node.js 19+
const id = crypto.randomUUID();
// => "550e8400-e29b-41d4-a716-446655440000"
Enter fullscreen mode Exit fullscreen mode
import uuid
id = str(uuid.uuid4())
Enter fullscreen mode Exit fullscreen mode
id := uuid.New().String()
Enter fullscreen mode Exit fullscreen mode

Are They Too Big?

A UUID is 16 bytes as binary, or 36 characters as a string. For modern databases like PostgreSQL (which has a native uuid type storing only 16 bytes), the overhead is negligible. For MySQL, store UUIDs as BINARY(16) instead of CHAR(36) to save space.

The Bottom Line

UUIDs aren't just for databases. They're for idempotent APIs, offline-first apps, file storage, and any system where multiple pieces need to generate IDs without talking to each other. If you need to quickly generate UUIDs for testing or development, I keep this free UUID generator bookmarked — it supports bulk generation up to 100 at a time, and all processing happens locally in your browser.

Top comments (0)