← All articles

Unique ID generation, from auto-increment to Snowflake

Updated Nov 10, 2026 · 4 min read

Every message, order and photo needs an id. On one database that is easy, because an auto-increment column counts up for you. Once data lives on many shards and is written by hundreds of servers in several data centers, one counter becomes the thing everyone waits on. The fix is to let each server make its own ids, in a way that can never clash.

What you usually want from an id

  • Unique across every server and data center, forever.
  • Made without a network call, so a write never waits on another box.
  • Sorted roughly by time, so “newest first” is a cheap range scan.
  • Small. A 64-bit number fits in a BIGINT and indexes well.

The options

ApproachCall per id?Sorts by time?SizeCatch
Auto-increment in one databaseYesYes64 bitsOne box takes every write and is a single point of failure
Blocks from a central counterNo, one per blockOnly within a server64 bitsIds from different servers interleave out of order
UUIDv4NoNo128 bitsRandom keys scatter inserts across the index
UUIDv7 or ULIDNoYes, to the millisecond128 bitsTwice the size of a 64-bit id
SnowflakeNoYes, to the millisecond64 bitsNeeds a unique machine id and a sane clock

Inside a Snowflake id

Twitter built Snowflake in 2010, when tweets outgrew a single database’s auto-increment ids. An id packs three numbers into 64 bits. The top bit stays 0 so the id is positive in languages with signed integers. Then come 41 bits of milliseconds since a start date you choose, 10 bits of machine id and 12 bits of sequence.

01 bitTimestamp41 bits of msMachine10 bitsSequence12 bits
The timestamp sits in the top bits, so sorting ids sorts them by time.
if now == last_ms:
    seq = (seq + 1) & 4095
    if seq == 0: wait until the next ms
else:
    seq = 0
last_ms = now
id = (now - epoch) << 22 | machine << 12 | seq

The machine id keeps servers apart, and the sequence keeps ids apart within one millisecond on one server. Nothing is shared, so a server can make millions of ids a second without talking to anyone. Instagram uses the same idea with 13 bits for a shard number and 10 for the sequence.

The bit math

Bits are a budget. Every field gets a power of two, and taking a bit from one field halves another.

FieldBitsWhat it buys
Sequence122¹² = 4,096 ids a millisecond per server, about 4 million a second
Machine id102¹⁰ = 1,024 servers making ids at once
Timestamp412⁴¹ ms ≈ 2.2 × 10¹² ms, about 69 years from your start date

Need room for 5,000 servers? That takes 13 machine bits, and the 3 extra bits come out of the timestamp. 38 bits of milliseconds last under 9 years. Pick the start date as late as you can, and decide early how many servers you might ever run.

10 machine bits (41 for time)≈ 70 years
11 machine bits (40 for time)≈ 35 years
12 machine bits (39 for time)≈ 17 years
13 machine bits (38 for time)≈ 9 years
How long the timestamp lasts as the machine id grows, with 12 sequence bits.

Random keys and the index

Databases keep a primary key in a B-tree, sorted. A time-ordered key always lands at the right edge, so inserts keep touching the same few pages, and those stay in memory. A random UUIDv4 lands on a random page every time. Once the index is bigger than memory, most inserts first read a cold page from disk and often split it, and inserts slow to a crawl.

If you want UUIDs, use UUIDv7 (standardized in RFC 9562 in 2024). It starts with a 48-bit Unix timestamp in milliseconds and fills the rest with random bits, so it sorts by time and needs no machine id. The price is 16 bytes instead of 8, in the table and in every index that stores the key.

When the clock goes backwards

Snowflake trusts the clock. NTP usually slews a clock gently, but it can step it back, and a virtual machine can resume with an old time. If a generator last used millisecond 1,000 and the clock now says 995, making ids for 995 again can repeat ids it already handed out.

  • Remember the last millisecond used, and never make an id for an earlier one.
  • If the clock is a little behind, wait until it passes the last millisecond used.
  • If it is far behind, stop making ids and alert. A broken clock needs a human.
  • Don’t restart to “fix” it. A fresh process forgets the last millisecond it used.

Handing out machine ids

Two live processes with the same machine id can make the same ids. Small fleets set the id in config per server. Larger ones lease ids from a coordination service such as ZooKeeper or etcd when a process starts and give them back when it stops. If a lease is lost, the process must stop making ids before another one takes the number.

Practical details

  • JavaScript numbers are exact only up to 2⁵³, so send 64-bit ids to browsers as strings. Twitter’s API added id_str for this reason.
  • Ids leak when something was made and roughly how busy you are. If that matters, don’t show them, or show a scrambled form.
  • Ids from different servers in the same millisecond are not in exact order. “Sorted by time” means to the millisecond, which is plenty for a chat screen.