Sub topic 2
What to ask of an ID: uniqueness, ordering, size, and coordination
This chapter assumes you know what a primary key and an index are, can read binary and bit shifts, and understand that distributed nodes can fail independently. Before comparing schemes, you need a checklist for judging them. Every scheme in the rest of the tutorial is a different set of answers to the same few questions.
The properties to judge
Uniqueness
An ID scheme must never hand out the same value twice for different things. There are two strengths of guarantee:
- Guaranteed unique. The structure of the scheme rules out duplicates, as long as its assumptions hold. A single database sequence is an example.
- Probabilistically unique. Duplicates are possible but so unlikely that you can ignore them. Random UUIDs are an example.
Both can be acceptable. What matters is that you know which one you have, and what assumptions it relies on, such as "worker IDs are never reused" or "clocks never move backwards".
Ordering
Ask what kind of order you need, because the options differ a lot:
- No order. IDs are only labels.
- Roughly time-ordered. An ID created later usually sorts after an earlier one. Good enough for "newest first" pages and for index locality.
- Strictly ordered. Every ID is greater than all earlier ones, across all nodes. This needs central coordination and is rarely worth the cost.
Ordering by ID is not the same as ordering by event. Two nodes with slightly different clocks can issue IDs whose order disagrees with the real order of events. Do not use a time-ordered ID as proof of what happened first.
Size
An ID is stored in the row, in every index that contains it, and in every table that references it. Size affects memory and disk use directly.
| Scheme | Typical size |
|---|---|
| Auto-increment integer | 4 or 8 bytes |
| Snowflake-style ID | 8 bytes |
| UUID or ULID (binary) | 16 bytes |
| UUID as text | 36 characters |
Storing a UUID as text more than doubles its size compared with the 16-byte binary form. An 8-byte ID also fits in a 64-bit integer, which is convenient. JavaScript numbers, for example, lose precision above 2^53, so large 64-bit IDs are often sent as strings in JSON.
Coordination
Coordination is how much the generators must talk to each other, or to a shared service, to produce an ID. This is the property that makes the problem hard:
- Per-ID coordination. Each ID needs a round trip to a central service. Simple, but that service limits throughput and availability.
- Occasional coordination. A node asks for a block of IDs or a worker number once, then generates locally.
- None. Each node generates IDs entirely on its own.
Less coordination usually means better availability and lower latency, but weaker guarantees or less ordering.
Predictability
Some schemes produce IDs that an outsider can guess or that reveal information, such as how many records exist or when one was created. This matters when IDs appear in URLs or APIs. A later chapter covers it in detail.
How the properties pull against each other
No scheme gets every property for free. Strict ordering costs coordination. Zero coordination costs either size (random bits) or ordering. Compact 64-bit IDs need a plan for assigning worker numbers and handling clocks.
Turning requirements into questions
When you evaluate a scheme, ask these questions in order:
- Can a duplicate ever happen, and under what assumptions?
- Does anything need to sort by ID, and how exact must that sort be?
- How many bytes can each ID cost, given how many indexes and foreign keys carry it?
- What happens to ID generation if the network is partitioned or a coordinator is down?
- Would it hurt if someone could guess the next ID?
Exercise
A service issues IDs from three regions that must keep accepting writes during a network partition between regions. Name two properties from this chapter that a single central auto-increment counter violates for this service, and say why.
Show answer
It violates the coordination and availability requirement: every ID needs a round trip to one counter, so regions cut off from it cannot create IDs. It also hurts latency for distant regions, since each ID costs a cross-region call. Schemes with no or occasional coordination, such as random or Snowflake-style IDs, avoid this.