UUID, ULID and Snowflake IDs

How to give every record a unique ID without one central counter: random UUIDs, time-ordered UUIDv7 and ULID, and 64-bit Snowflake IDs, and what each choice does to your database index.

Overview

Every row, message and object needs an identifier that no other one shares. On a single database the answer is easy: an auto-increment column hands out 1, 2, 3 and so on. That stops working when many machines create records at once. Either they all ask one counter, which becomes a bottleneck and a single point of failure, or they count on their own and hand out the same numbers.

Distributed ID schemes solve this by letting each machine make IDs alone. UUIDv4 uses 122 random bits, so collisions are practically impossible. UUIDv7 and ULID put a millisecond timestamp first and randomness after it, so IDs sort roughly by creation time. Snowflake, built by Twitter in 2010, packs a timestamp, a machine number and a counter into 64 bits.

The choice matters more than it looks. An ID usually becomes a primary key, and the order in which keys arrive decides how a B-tree index grows. Time-ordered IDs keep inserts at one end of the index; random ones scatter them across it.

What you will be able to do

  • Explain why auto-increment does not scale out, and name the ways around it.
  • Draw the bit layouts of UUIDv4, UUIDv7, ULID and Snowflake, and derive their limits.
  • Explain why random keys make B-tree inserts slower and indexes larger.
  • Handle Snowflake's failure modes: clocks going backwards, duplicate machine IDs, and a used-up sequence.
  • Choose an ID scheme for a service and defend it.

Key concepts

  • Auto-increment limits
  • UUIDv4
  • UUIDv7
  • ULID
  • Snowflake
  • Custom epoch
  • Clock going backwards
  • B-tree index locality
  • IDs leak time

Interview questions

  • Why not just use auto-increment IDs in a distributed system?
  • Walk through the Snowflake ID layout and its limits.
  • What happens if a Snowflake server's clock goes backwards?
  • Why do random UUIDs hurt database performance?
  • Compare UUIDv7 and ULID. Which would you choose?
  • Design an ID scheme for a multi-region order service.
  • Can you tell when a record was created from its ID? Is that a problem?

Sign up free to read the full topic

More System Design topics

Start free on Cariara · Coding problems · System design studio