Lab · Distributed data

IDs: from auto-increment to UUIDs and Snowflake

Every row needs an ID that no other row has. Try the five main ways to make one, in the order they appeared, and measure what each costs: repeated IDs, order, trips over the network and the index.

Lab time 3.0 s
App server A, saves rowsApp server Asaves rowsApp server B, saves rowsApp server Bsaves rowsDB 1, 31 rows · 1 pageDB 131 rows · 1 pagelast ID 31
  • App server A — saves rows — healthy
  • App server B — saves rows — healthy
  • DB 1 — 31 rows · 1 page — healthy — last ID 31
  • A row to save
Same ID on two rows
0
Healthy: every ID once
IDs out of time order
0%
smaller than the ID before
Index page fill
97%
1 page of 32 keys
Pages written
1
by the last 100 rows, of 1
Trips for IDs
0.0
per 100 rows: none needed
Bytes per ID
8bytes
64 bits

Numbers add up:31 saved + 0 failed + 0 in progress = 31 rows asked forThe lab checks this sum as it runs, so a counting mistake would show here.

Controls

How IDs are made

The database numbers rows 1, 2, 3 as it saves them.

Setup

Server A saves to DB 1, server B to DB 2. Starts empty tables.

1 s behind server A. Matters for IDs that start with the time.

12/s

The five ways side by side

What each way of making IDs gives and costs
WaySizeShared counter?In time order?Safe across databases?Can be guessed?
Auto-increment (running)8 bytesYes: the databaseYesNo: each database repeats 1, 2, 3Yes: the next is one more
Blocks from a ticket server8 bytesYes: one trip per blockRoughlyYesMostly
UUID v4: random16 bytesNoNoYesNo: 122 random bits
UUID v7: time first16 bytesNoYes, if clocks agreeYesPartly: the time shows
Snowflake: 64 bits8 bytesOnly to hand out machine idsYes, if clocks agreeYesPartly: time and machine show

The measures above come from the running model. This table states what each method gives by design.

What just happened

Nothing yet. Start traffic or run an experiment.

What this model simplifies
  • An index page here holds 32 keys, so splits show sooner. Real pages hold hundreds, and the effect is the same.
  • Only the leaf pages of the index are modelled. The pages above them are small and usually stay in memory.
  • A row whose server has no numbers fails at once. A real server would wait for a timeout first.
  • Changing how IDs are made, or splitting the rows, starts with empty tables, so each setup is measured from its start.