Generación de IDs Únicos
Snowflake · UUIDv7 · ULID
Escrito por Darjus Kazakevic · revisado el
Unique on every server, sorted by time, and no central counter.
One database can hand out IDs with an auto-increment column. That stops working with many shards or many writers: two shards both create ID 1001, and one central counter becomes a bottleneck and a single point of failure. You need IDs that any server can create alone, with no real chance of a collision.
Random UUIDs (version 4) solve uniqueness but not order. New rows land at random places in a B-tree index, which slows inserts once the index no longer fits in memory. Time-ordered IDs put a timestamp first: Twitter's Snowflake, UUIDv7 and ULID. They stay unique and also sort by creation time.
En esencia — Put the time first and a machine or random part after it, so any server can create an ID alone and IDs still sort by when they were made.
Cuándo recurrir a él
Recurre a él cuando
- Many servers or shards insert rows and need IDs without a round trip
- You want primary keys that sort by time and keep indexes fast
- Clients or offline devices must create IDs before they reach a server
Reconsidéralo cuando
- One database takes all writes: a sequence is shorter and simpler
- The ID is public and must not reveal when it was created
- You need numbers with no gaps, such as invoice numbers required by law
Conceptos clave
- Auto-increment and its limits
- A database sequence gives short, ordered IDs, but it needs one writer. With shards, each needs its own range or step. Sequential IDs also reveal volume: order 1005 tells a competitor you have about a thousand orders.
- UUIDv4
- 128 bits, 122 of them random. A collision is practically impossible: you need about 2.7 × 10^18 IDs for a 50% chance of one. But random order spreads inserts across the whole index.
- Snowflake
- A 64-bit integer: 1 unused sign bit, 41 bits of milliseconds since a chosen epoch, 10 bits of machine ID and 12 bits of sequence. Twitter released it in 2010; Discord and Instagram use variants of it.
- UUIDv7 and ULID
- UUIDv7 (RFC 9562, 2024) is a standard UUID with a 48-bit millisecond timestamp first and random bits after. ULID is the same idea written as 26 characters. Neither needs a machine ID, so clients can create them too.
- The clock going backwards
- NTP (the clock sync service) can move a server's clock back, and so can restoring a VM snapshot. A generator that trusts it repeats IDs. Remember the last timestamp, and wait or fail when the clock is behind it.
Reglas de diseño
- Put the time first
- IDs that sort by time add new rows at the right edge of a B-tree index, so inserts stay fast and recent rows stay in cache. Use UUIDv7, ULID or Snowflake for primary keys.
- Never let two generators share a machine ID
- Two Snowflake generators with the same machine ID create the same IDs. Assign machine IDs from etcd or ZooKeeper, or from the pod ordinal in a Kubernetes StatefulSet.
- Handle the clock going backwards
- Store the last timestamp you used. If the clock returns an earlier one, wait until it catches up or fail loudly. Never create IDs from the earlier time.
- Know what your public IDs reveal
- Sequential IDs in URLs reveal volume and let anyone guess the next one. Time-ordered IDs reveal when a row was created. Use random IDs where either matters.
Errores habituales
- UUIDv4 as the primary key of a large, write-heavy table. Insert speed drops once the index no longer fits in memory.
- Two pods with the same machine ID after a restart or a scale-up. They create the same IDs, and inserts fail with duplicate keys.
- Sending 64-bit IDs to JavaScript as numbers. Above 2^53 they lose precision, so send them as strings, as Twitter's id_str field does.
- Ignoring clock jumps. A VM restored from a snapshot moves its clock back, and the generator repeats IDs.