Twitter Snowflake ID Generator Overview

Started by Ayaan, May 05, 2026, 07:06 PM

Previous topic - Next topic

Ayaan

It generates a unique 64‑bit ID using several parts:

* 1 bit – always 0
* 41 bits – timestamp in milliseconds
* 5 bits – datacenter ID
* 5 bits – machine ID
* 12 bits – sequence number

The 41‑bit timestamp gives about 2^41 milliseconds, roughly 70 years, so the system will eventually run out of timestamp range.

With 5 bits each for datacenter and machine IDs:

* 2^5 = 32 datacenters
* Each datacenter can host 32 machines

The 12‑bit sequence number lets a single machine create multiple unique IDs within the same millisecond.

Example: if two IDs are generated on the same machine at the exact same millisecond, the sequence number increments to keep them unique.

Some limitations I noticed:

* Clock‑sync issues between servers can cause problems. If a machine's clock moves backwards, duplicate IDs may appear.
* Mis‑configured machines using the same datacenter and machine IDs can collide.
* Sequence overflow can occur if too many IDs are generated in one millisecond on a single machine.

It's a really interesting system‑design concept because it gives distributed unique IDs without a central database.

If you know more about how companies handle these issues at scale, please share in the comments. I'd love to learn more

Akash

I used it during my last internship. We faced a UUID collision in MySQL, so I suggested and implemented Snowflake. Thanks to YouTube and X for the great design ideas.

Mohan

I've heard about it before but never dug into the details.

Lakshmi

In real‑world scenarios, a collision is extremely unlikely. Theoretically UUIDs can collide, but most databases see it only in very rare cases.

Pillai


Advik

We used the internal IP and port together with a local MySQL sequence generator, since several services could run on the same machine.

Another optimisation was to reserve 5,000 sequence numbers in one go, hit the DB once and then serve them from RAM.

Bhavin

There is sonyflake too – https://github.com/sony/sonyflake

* 39 bits for time in 10 ms units
* 8 bits for a sequence number
* 16 bits for a machine id

It gives a 174‑year lifespan and can handle 2^16 machines (compared to Snowflake's 2^10). I have used both; they are a much better alternative to UUIDs, which bloat storage in SQL databases, whereas a simple uint64 (Snowflake or Sonyflake) is compact.

Rahul

Give ULIDs a look sometime.

As mentioned earlier, the new UUID v7 also produces time‑sortable IDs.

Kalpana

I came across a patent that solves the same problem: https://patents.google.com/patent/US9237074B1/en

Before cloud services became common, most systems relied on Oracle sequence numbers for IDs. About fifteen years ago, when many moved away from Oracle, this became a hot issue. Almost every large‑scale company ended up building its own ID‑generation solution.