1 min read

The Distributed Nightmare of Auto-Numbers

Why auto-increment fails when you try to shard a database or merge data from multiple regions
Street photograph by Stalin Pereira
Photo by Stalin Pereira

Your IDs Are A Timebomb

Why your database's favorite trick, auto-increment, fails at scale.

  • The Raffle Ticket Analogy — You have two ticket sellers. Both start their ticket roll at #1. When you draw the winning number, who really wins if you pull ticket #5?

The Single-Server Dream

On one database, life is simple and predictable.

  • Simple & Predictable — The next ID is always MAX(id) + 1. The database guarantees no duplicates. It just works.
  • Fast & Built-in — No application logic needed. The database handles ID generation with near-zero overhead.
  • Human Readable — IDs like 1, 2, 3... are easy for developers to read, query, and debug.

The Multi-Server Nightmare

When you add a second server (for sharding or regions), chaos begins.

  • ID Collisions — Your US and EU servers both create 'Order #101'. Now your data is corrupt and unreliable.
  • Broken Relationships — User #42 in the US database is not the same person as User #42 in the EU database. Foreign keys become meaningless.
  • Painful Merges — Combining data requires re-numbering every conflicting record. This is slow, expensive, and risks data loss.

Generate Smarter IDs

How to create identifiers that are unique across the globe.

  • UUIDs — Think of it as a globally unique 'serial number'. The chance of a collision is practically zero, but they aren't sortable by time.
  • Snowflake IDs — A clever recipe: Timestamp + Machine ID + Sequence. They are time-sortable and guaranteed unique across your fleet.
  • ULIDs — The best of both worlds. Sortable like a Snowflake, but encoded to be URL-friendly and compatible with UUID formats.

The Takeaway

Think about your ID strategy before it's a scaling emergency.

Local numbers create global problems.

What's your go-to for generating unique IDs? UUIDs, Snowflakes, or something else?