Lesson 2 of 16
10. Unique ID Generation

10.1 What makes a good ID?

3 min read+10 XP

The problem

At the bank: A good token is never repeated, easy to read out, and ideally tells staff who came first. It shouldn't let a stranger guess someone else's token and walk up to the counter.

In the system: Decide what you need before picking a scheme. The properties that matter:

PropertyWhy it mattersExample need
UniqueThe whole point. Never two rows with one IDAlways
Generated without a bottleneckThousands of servers creating IDs can't all wait on one counterHigh write rates
Sortable by time"Newest first" for free; new rows land at the end of an index (Chapter 9)Feeds, orders, messages
Compact64-bit numbers are smaller and faster than 128-bit or long stringsHuge tables, short URLs
UnguessableStops people walking through /orders/1001, /orders/1002...Private links, public URLs

You can't have everything at once. Sortable IDs leak creation order; unguessable IDs are long and random. The schemes in this chapter are different trade-offs between these columns.

Memory code: "Unique always; then pick from fast, sorted, small, secret."