Data & storage / 7 / 11
Unique IDs
An orders service gets its ids from a database counter. Then the data is split across two databases, and the counters start handing out the same numbers. How to make ids that are unique everywhere, sort well in an index and survive a clock that jumps back.
- 12 min
- Intermediate
- 10 steps
- Insert p95
- 4 ms
- Duplicate ids
- 0 /day
- Index page splits
- 2 /s
- request
- reply
- redelivery
- duplicate
- lost
Numbers are a model scenario, not measurements
The real request
Orders API → Shard 1Request
INSERT INTO orders (user_id, total)
VALUES ('u_42', 125000)
RETURNING id;1
Response
id = 482132
RETURNING— The database tells the API which id it just assigned. The API never picks the number itself.48213— The next value of the counter. An 8-byte integer, sorted in insert order.
Course: 42 lessons
01 Foundations
02 Scaling
03 Data & storage
04 Communication
05 Reliability
06 Architecture
07 Security
08 Shipping
09 Whole systems
10 Business processes