eChat

Categories => Technology & Gadgets => Topic started by: Ayaan on May 05, 2026, 07:06 PM

Title: Twitter Snowflake ID Generator Overview
Post by: Ayaan on May 05, 2026, 07:06 PM
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
Title: Re: Twitter Snowflake ID Generator Overview
Post by: Akash on May 05, 2026, 07:06 PM
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.
Title: Re: Twitter Snowflake ID Generator Overview
Post by: Mohan on May 05, 2026, 07:06 PM
I've heard about it before but never dug into the details.
Title: Re: Twitter Snowflake ID Generator Overview
Post by: Lakshmi on May 05, 2026, 07:06 PM
In real‑world scenarios, a collision is extremely unlikely. Theoretically UUIDs can collide, but most databases see it only in very rare cases.
Title: Re: Twitter Snowflake ID Generator Overview
Post by: Pillai on May 05, 2026, 07:06 PM
Also check out UUIDv7.
Title: Re: Twitter Snowflake ID Generator Overview
Post by: Advik on May 05, 2026, 07:06 PM
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.
Title: Re: Twitter Snowflake ID Generator Overview
Post by: Bhavin on May 05, 2026, 07:06 PM
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.
Title: Re: Twitter Snowflake ID Generator Overview
Post by: Rahul on May 05, 2026, 07:06 PM
Give ULIDs a look sometime.

As mentioned earlier, the new UUID v7 also produces time‑sortable IDs.
Title: Re: Twitter Snowflake ID Generator Overview
Post by: Kalpana on May 05, 2026, 07:06 PM
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.