Home›Learn›Unique IDs at scale
Course · Spread the dataLesson 18 of 330 read

Unique IDs at scale

Which primary key should I use, and why does it matter?

Three keys, animated and measured.

Press Play, or step with the arrows. Switch between the three keys, or show them side by side.

System design lessons treat unique IDs as a coordination problem: how do many servers make IDs that never collide. That matters. But the bigger cost is usually inside the database, in the index those IDs go into.

Try it · real PostgreSQL in your browser

Run it: three kinds of key

Insert 200,000 rows three times, once per key type, then compare the size of each primary key index. Each insert takes a second or two in the browser.

  1. Insert with a sequence
    INSERT INTO seq_keys (n) SELECT g FROM generate_series(1, 200000) g;
  2. Insert with random UUIDv4
    INSERT INTO v4_keys (n) SELECT g FROM generate_series(1, 200000) g;
  3. Insert with UUIDv7
    INSERT INTO v7_keys (n) SELECT g FROM generate_series(1, 200000) g;
  4. Compare the index sizes
    SELECT relname AS index,
           pg_size_pretty(pg_relation_size(oid)) AS size
    FROM pg_class
    WHERE relname LIKE '%keys_pkey'
    ORDER BY pg_relation_size(oid);

Measured: Notebook 10

Go deeper
Primary keyPostgreSQL indexLeaf pages fullPostgreSQL insertMySQL tableMySQL insert
bigint identity / AUTO_INCREMENT21.4 MB90.0%3.4 s137.7 MB6.2 s
UUIDv7 / UUID_TO_BIN(UUID(), 1)30.1 MB90.0%3.2 s145.8 MB5.7 s
UUIDv4 / 16 random bytes38.4 MB70.6%4.6 s236.0 MB55.5 s

1,000,000 rows in 100 commits of 10,000, stock settings, one GitHub runner. Transcript and method in Notebook 10. Watch the splits happen in Watch an index grow.

IDs across many servers

Go deeper
ApproachHow it worksWatch out for
One sequenceThe database hands out 1, 2, 3One writer only; guessable; leaks your row count
Sequence rangesEach server takes a block, or auto_increment_increment and offset in MySQLGaps; reconfigure when servers are added
UUIDv4122 random bits, no coordinationRandom inserts: size and speed, as measured above
UUIDv748-bit millisecond time, then random bits16 bytes wide; reveals creation time
Snowflake style64-bit: time, machine number, a counterEach machine needs a unique number; clocks must not go backwards

For most new systems: bigint identity when one database writes, UUIDv7 when many writers or clients create IDs. PostgreSQL 18 has uuidv7() built in. In MySQL, UUID_TO_BIN(UUID(), 1) reorders a v1 UUID so the time comes first, which gives the same effect.

-- PostgreSQL 18
CREATE TABLE orders (id uuid DEFAULT uuidv7() PRIMARY KEY, ...);
-- MySQL 8
CREATE TABLE orders (id BINARY(16) DEFAULT (UUID_TO_BIN(UUID(), 1)) PRIMARY KEY, ...);
Connected

Where this shows up

The same mechanism, keys and ids, sharding and partitioning, elsewhere on the site: in real incidents, in measurements, and in designs.

Check yourself · 3 questions

Check yourself

Pick an answer. You will see whether it is right, and why.

1. Why did random 16-byte keys make MySQL inserts ten times slower in Notebook 10?

2. Why was the UUIDv7 index bigger than the bigint one in PostgreSQL?

3. Which ID keeps inserts at the right edge of the index without a central counter?

Part 1 of 4 · Learn · 23 pagesAll pages

How do databases actually work? From one row on disk to transactions: the words you need, PostgreSQL, MySQL and MongoDB from the files up, then Redis, Cassandra and vector search, side by side, and the query and concurrency traps.