download

How do you make unique, sortable IDs on many servers?

System design~15 minReviewed 29 Oct 2026Free · No sign-in

Question

Your shop runs 40 app servers. Each one must create order IDs by itself, without calling a central service for every order. The IDs must:

  • be unique,
  • fit in a signed 64-bit integer column,
  • sort roughly by creation time, so new orders land at the end of the index.

Design the ID. Say which parts it has, how many bits each part gets, and what that allows. Then say what a server should do when its clock jumps back 5 ms.

Show hints (3)
  1. "WHAT SORTS FIRST"

    Integers compare from the highest bit down. Which part should sit in the highest bits so that IDs sort by time?

  2. "SAME MILLISECOND"

    Two servers can create an ID in the same millisecond, and one server can create many. What do you put next to the time so these never clash?

  3. "CLOCK GOES BACK"

    Keep the last millisecond you used. If the clock now shows an earlier one, which IDs could repeat? What is the safe move?

Show checked explanation

Pack three parts into a 64-bit integer: milliseconds since your own epoch in the top bits, then a machine id, then a counter for IDs made in the same millisecond. With time on top, IDs sort by time. If the counter runs out, wait for the next millisecond. If the clock goes back, stop until it catches up.

"SOLUTION · PYTHON"
MACHINE_BITS, SEQ_BITS = 10, 11  # the time gets the 42 bits above them


class IdMaker:
    def __init__(self, machine_id):
        self.machine, self.last_ms, self.seq = machine_id, -1, 0

    def make(self, now_ms):  # milliseconds since 2026-01-01 UTC
        if now_ms < self.last_ms:
            return None  # clock went back: refuse until it catches up
        self.seq = self.seq + 1 if now_ms == self.last_ms else 0
        if self.seq >= 1 << SEQ_BITS:
            return None  # 2,048 used in this ms: wait for the next one
        self.last_ms = now_ms
        return (now_ms << MACHINE_BITS | self.machine) << SEQ_BITS | self.seq


a, b = IdMaker(machine_id=7), IdMaker(machine_id=3)
for maker, now in [(a, 1000), (a, 1000), (b, 1000), (b, 1001), (a, 995)]:
    print(maker.machine, now, maker.make(now))
"OUTPUT"
7 1000 2097166336
7 1000 2097166337
3 1000 2097158144
3 1001 2099255296
7 995 None

Code run on CPython 3.12 against the examples above, a full millisecond of IDs and a clock that goes back. Checked by an editor other than the author.

Walkthrough

This is the idea behind Twitter's Snowflake IDs. One way to split the 64 bits, top bit first:

partbitsgives you
sign1always 0, so IDs stay positive
ms since 2026-01-01 UTC42about 139 years, to 2165
machine id101,024 machines
counter112,048 IDs per ms per machine

Build it with shifts: (ms << 21) | (machine << 11) | counter. Machine 7, one second after the epoch (ms 1000), counter 0, gets 2,097,166,336. Each machine can make up to 2,048,000 IDs a second.

Integers compare from the top bit down, so the time decides first. Any ID from ms 1001 is bigger than any ID from ms 1000. The machine id keeps two servers apart within one ms. The counter keeps one server's IDs apart.

The order is rough. Within one ms, machine 3's ID (2,097,158,144) sorts before machine 7's, whichever came first. Server clocks also differ. So don't use ID order to decide which of two orders came first.

Time sync can step a fast clock back. Say the server made IDs from ms 995 to 1000, and the clock now shows 995 again. New IDs could repeat old ones, so wait until the clock reaches 1000 again. Better, set time sync to slow the clock down instead of stepping it back.

Common mistakes

  • Putting the machine id in the top bits. IDs stay unique, but they sort by machine, not by time, so new orders land all over the index.
  • Hashing the host name into a 10-bit machine id. With 40 servers, the chance that two get the same id is 54%, and those two make the same IDs. Use a config table instead.
  • Wrapping the counter back to 0 within one millisecond. The 2,049th ID in that ms then repeats the first one.
  • Forgetting restarts. A new process doesn't know the last ms the old one used. If the clock went back meanwhile, it can repeat IDs. At startup, wait longer than the largest backward step you allow.

Follow-up questions

  • "Why not just use UUIDs?" A random UUID (version 4) has 122 random bits, so any server can make one alone. But it is 128 bits, too big for the column, and its random order scatters inserts across a B-tree index. UUID version 7 starts with a 48-bit millisecond time, so it sorts by time. Use it when 128 bits is fine.
  • "Why not one auto-increment column?" One database becomes a bottleneck and a single point of failure. Giving each of the 40 servers its own start and a step of 40 (server 1 makes 1, 41, 81) avoids that. But a busy server's IDs run ahead of a quiet one's, so they don't sort by time. A 41st server means changing every step.

What a strong answer covers

Puts time in the top bits and says why. Sizes each part with numbers. Handles a full counter and a clock that goes back. Compares the design with UUIDs and one auto-increment column.

Open in interactive practice deck →

Original OfferVibe question. Topic idea from System Design Interview: An Insider's Guide (Alex Xu, 2020); text, examples and code are our own.
© 2026 OfferVibe · Text CC BY-NC-SA 4.0 · code MITFree to share and adapt with credit, not for commercial use. Code samples are free for any use.