Sub topic 13
Choosing a scheme for your system
This chapter assumes you have read the earlier chapters on counters, UUIDs, time-ordered IDs, and Snowflake IDs. You should also be comfortable with primary keys and B-tree indexes. Here we turn those pieces into a decision procedure.
Start with requirements, not with a favorite
No scheme wins on every property. Write down what your system needs, then eliminate options that fail a hard requirement. Ask these questions in order:
- Can generators talk to a coordinator at all, for example during a network partition or while offline?
- Does the ID need to sort by creation time, or only be unique?
- How large can the ID be? A 64-bit integer is half the size of a 128-bit UUID in every index and foreign key.
- Can users see or guess the ID?
- Can you operate extra infrastructure, such as a ticket server or a worker-ID registry?
Comparing the options
| Scheme | Coordination | Size | Time-ordered | Main risk |
|---|---|---|---|---|
| Database auto-increment | Every insert hits one database | 64-bit | Roughly, by insert order | Bottleneck and single point of failure |
| Block allocation (ticket server) | One call per block | 64-bit | Only loosely | Gaps after crashes, allocator outage |
| UUID v4 | None | 128-bit | No | Poor index locality |
| UUID v7 or ULID | None | 128-bit | Yes, to the millisecond | Order within a millisecond is not guaranteed |
| Snowflake-style | Worker ID assignment only | 64-bit | Yes | Duplicate worker IDs, clock rollback |
A decision path
Treat this as a starting point. Security and operations can override it.
Worked scenarios
A single-region internal tool with modest write volume. A database sequence is the right answer. It is small, ordered, and needs no extra code. Move away from it only when write volume or availability requirements force you to.
A mobile app that creates records offline. The client cannot reach any coordinator, so it needs a scheme with no coordination. Use UUID v7 if the records go into a database index, because the timestamp prefix keeps inserts near the right edge of the B-tree. Use UUID v4 if insert locality does not matter.
A high-volume service that stores IDs in billions of rows. A 64-bit ID saves space in every index and join. A Snowflake-style generator fits. You must then handle worker ID assignment and clock rollback, as the earlier chapters showed. The generator in this tutorial refuses to issue IDs when the clock moves backwards. That choice favors correctness over availability.
Public identifiers in URLs. A sequential ID lets anyone enumerate your records and estimate your volume. A Snowflake ID also reveals its creation time and worker. Either expose a random public ID such as UUID v4, or keep the ordered ID internal and add an authorization check on every lookup.
Common mistakes
- Choosing UUID v4 as a clustered primary key by default. It works at small scale and then slows inserts as the index outgrows memory.
- Assuming ordering within a millisecond. UUID v7 and ULID, as implemented in the earlier example, order only across milliseconds. If you need a strict order, add a sequence field or a separate column.
- Reusing worker IDs after a restart or autoscaling event. Two live generators with the same worker ID and the same millisecond can issue the same ID. Assign worker IDs from a registry or a lease.
- Optimizing for collisions that cannot happen. With 122 random bits, a billion UUID v4 values give a collision probability of about 9.4e-20. Worry about index performance and predictability first.
Exercise
Exercise
You are building an order service. Orders are created in three regions with no cross-region coordination on the write path. IDs must be 64-bit, roughly sortable by time, and the service runs on autoscaled containers that start and stop often. Which scheme do you pick, and what must you add to make it safe?
Show answer
Use a Snowflake-style ID. It fits in 64 bits, sorts by time, and needs no per-ID coordination. Because containers autoscale, you must assign worker IDs safely, for example from a registry with leases, so two live containers never share one. You must also handle clock rollback by refusing to issue IDs, or by waiting, when the clock moves backwards. If orders are also exposed in public URLs, use a separate random public identifier or enforce authorization on every lookup.