Skip to content

URL shortener · 1 of 6

Step 1 · The pattern

Ask the counter

Each new link needs a unique ID. The obvious source is one counter in a database: every server asks it for the next number. Unique by construction.

  • create request
  • got a unique ID

In short

Snowflake IDs: how it works and how it breaks

Every new short link needs a unique ID. The simplest source is one auto-incrementing counter in a database, but then every create on every server makes a round trip to that one place. It caps throughput, and if it fails nothing can be created. Letting each server count for itself removes the bottleneck and immediately produces duplicates, because two servers both think the next number is 1,042. Twitter's Snowflake design gets uniqueness without coordination by building the ID from parts that cannot clash. A 64-bit number holds a millisecond timestamp, then a worker ID unique to each server, then a sequence that counts within one millisecond. Two servers never share a worker ID, and one server never reuses a sequence within a millisecond. IDs also sort roughly by creation time. The thing to protect is the clock: if it jumps backwards, a server must wait.

How this comes up in interviews

"Design the ID generator for a service where many servers create records at once and every ID must be unique."

Then they ask

  1. How does each server get its worker ID, and what happens if two servers end up with the same one?
  2. A server's clock is moved backwards by two seconds. What must the generator do?
  3. What do you gain and lose compared with one auto-incrementing counter in a database?

A strong answer covers

  • One central counter is unique by construction, but it is a bottleneck and a single point of failure for every create.
  • Per-server counters remove the round trip and immediately produce duplicates.
  • A Snowflake ID packs a millisecond timestamp, a worker ID unique to each server, and a per-millisecond sequence into 64 bits.
  • Uniqueness needs no coordination per ID, and IDs sort roughly by creation time.
  • Protect the clock: if it goes backwards, refuse to issue IDs until it passes the last timestamp used.

Points to cover, not a script. Say them in your own words and tie them to a system you have worked on.

On one page: the track cheat sheet.

Last updated: Engineering write-up

Diagrams are our own simulations: the timings, rates and counts in them are illustrative, not measurements. Captions are original wording.