10. Unique ID Generation
1
The picture to keep in your head2
10.1 What makes a good ID?3
10.2 What's wrong with auto-increment?4
10.3 Why does auto-increment break across many servers?5
10.4 What are UUIDs, and which version should you use?6
10.5 How do Snowflake IDs work?7
10.6 What are ticket servers and ID ranges?8
10.7 Why does clock skew matter for time-based IDs?9
10.8 How do you make short IDs with base62?10
10.9 Should IDs be guessable or unguessable?11
Memory card and where the analogy breaks12
Practice: five levels13
Interview questions14
Solutions15
Sources16
Code lab10. Unique ID Generation
10.9 Should IDs be guessable or unguessable?
3 min read+10 XP
At the bank: If tokens are numbered 1, 2, 3, anyone holding token 57 knows someone has token 58. Harmless at a counter. Dangerous if the token is also the key to a safety deposit box.
In the system: Sequential IDs in public URLs leak information and invite attacks:
- Enumeration: if
/orders/1001is yours, an attacker tries/orders/1002,/orders/1003... If the server forgets to check ownership on even one endpoint, they read other people's orders. This is a classic security bug (often called IDOR: insecure direct object reference). - Business leakage: competitors can estimate your order volume by placing an order today and another next week and comparing IDs.
The rules:
- Always check authorisation, whatever the ID looks like. "Hard to guess" is a second line of defence, never the first.
- For anything that works as "whoever has the link can see it" (private wishlists, password-reset links, file shares), use long random IDs or tokens: a UUID v4, or random bytes from a secure random generator.
- Internally, keep the small sortable ID for the database, and expose a separate public random ID if needed. Many systems store both.
Memory code: "Sequential inside, random outside, and always check who's asking."