Free preview

Approach 5: Twitter Snowflake

Why this matters: Snowflake is the design people actually deploy, and its bit layout is the most commonly asked detail in this entire topic. Being able to allocate the 64 bits and justify each field is the deliverable.

Key takeaway

To use time efficiently, allocate specific bits of the 64-bit ID for the timestamp and use the remaining bits for other information. That is Twitter Snowflake — and it converts the previous lesson's failures into tunable parameters.

The bit layout

 1 bit    41 bits              10 bits      12 bits
[sign]   [timestamp ms]       [worker]     [sequence]
   0     0010100100...           1010      000000000001

 1 + 41 + 10 + 12 = 64 bits
FieldBitsPurpose
Sign bit1Always zero, ensuring the ID is interpreted as a positive integer
Timestamp41Milliseconds since a chosen epoch — provides the time ordering
Worker number10Supports 2^10 = 1,024 worker IDs — distinguishes machines
Sequence number122^12 = 4,096 IDs per millisecond per worker; resets to zero every millisecond

Each field answers one of the previous lesson's failures. The sequence fixes same-millisecond collisions on one server. The worker fixes cross-server collisions. The timestamp provides ordering. The sign bit just keeps the number positive so it sorts correctly as a signed integer.

The epoch

Snowflake uses a default epoch of 1288834974657 (Nov 4, 2010), but systems can define a custom epoch — for instance Jan 1, 2022.

Time to range depletion = 2^41 identifiers
                        / (365 * 24 * 60 * 60 * 1000 identifiers/sec)
                        ~= 69 years

This 41-bit range allows for 69 years of IDs.

Decoding a real Snowflake ID

Worth walking end to end, because it makes the layout concrete:

Step 1 — read the 41 timestamp bits as a decimal:
         352,721,356,343

Step 2 — add the Twitter epoch:
         352,721,356,343 + 1,288,834,974,657 = 1,641,556,331,000

Step 3 — interpret as milliseconds since the UNIX epoch:
         1,641,556,331,000 ms  ->  Jan 07 2022 11:52:11 UTC

The timestamp field is a delta from the epoch, not an absolute UNIX time — which is exactly why the epoch choice matters and why two systems with different epochs produce incomparable IDs.

Pros and cons

Detail
ProsIDs are time-sortable and the generator is highly available
ConsThe 41-bit timestamp limits the system's lifespan (approx. 69 years). This range is consumed by the passage of time, regardless of how many IDs are actually generated

That second point is subtle and worth stating: the timestamp budget drains on a wall clock, not on usage. A system generating ten IDs a day exhausts its 41 bits at exactly the same moment as one generating billions.

Scorecard

UniqueScalableAvailable64-bit numeric IDCausality maintained
Using a range handler
Using UNIX timestampsweakweak
Using Twitter Snowflakeweak

Four solid columns and a weak on causality — because the ordering it provides is only as good as the clocks producing it.

Key takeaway

Snowflake is a bit-budget allocation problem solved well: time for ordering, worker for machine identity, sequence for intra-millisecond concurrency. Its remaining flaw is inherited from physical time, which is what logical clocks address next.

Interview signal by level

LevelWhat a strong answer sounds like
L4"Use Snowflake IDs — they're time-sortable."
L5Gives the layout: "1 sign bit, 41 timestamp, 10 worker, 12 sequence — 4,096 IDs per millisecond per worker across 1,024 workers."
Staff+Justifies each field and names the flaw: "the sequence fixes same-millisecond collisions, the worker fixes cross-server ones. I'd set a custom epoch so we don't spend years of the 41-bit budget before launch. And the ordering is only as trustworthy as the clocks — an NTP correction backwards can reissue a millisecond, which is why this is weak on causality rather than solved."

Next: ordering without trusting clocks at all.

Enjoying the preview?

Create a free account to unlock the rest of this course, the in-browser judge, and live AI mock interviews.

Sign up free to continue