jonnifyelixir
F4 / F4.07 / snowflake

F4.07 · part 2 of 3

The Snowflake bigint

A Snowflake is one 64-bit integer with three fields packed into it: the high 42 bits are a millisecond timestamp counted from a custom epoch, the next 10 bits name the worker that minted it, and the low 12 bits are a sequence that disambiguates ids made in the same millisecond by the same worker. Reading a field is one shift and one mask — no parsing, no lookup.

One real Snowflake, 319545566822428714. Select a field and watch it get extracted with the actual shift and mask, computed on this page.

Three fields in 64 bits

The layout is fixed: timestamp << 22 | worker << 12 | sequence. To read a field back you shift it down to the low bits and mask off the rest. Select a field to see its shift, its mask, and the value pulled out of this exact integer.

Field · select one

snowflake = 319545566822428714 (64 bits) timestamp · 42 bits worker · 10 seq · 12 bits 63..22 21..12 11..0 timestamp = (snowflake >>> 22) + epoch = 2026-05-31 18:40:00 UTC the high 42 bits are a millisecond timestamp

      

role: the high 42 bits

result: 2026-05-31 18:40:00 UTC

Three fields, one integer, one machine word. Every read is a shift and a mask, so extracting the time, the worker, or the sequence costs the same as comparing two numbers.

Advanced: epoch, headroom, and limits

The timestamp is milliseconds since a custom epoch — here 2024-01-01 00:00:00 UTC, the integer 1_704_067_200_000. Counting from a recent epoch rather than 1970 keeps the value small, and 42 bits of milliseconds is about 2⁴² / 31_557_600_000 ≈ 139 years of headroom before the field overflows. The 10 worker bits allow 1024 workers; the 12 sequence bits allow 4096 ids per worker per millisecond — roughly four million ids per second per worker before a worker must wait for the next tick.

Because the timestamp is in the high bits, the integer’s natural order is time order: a plain < on two Snowflakes compares their timestamps first, then worker, then sequence. That is what gave the previous dive its sortability, and it is what lets a database paginate by id and a trie key on a value that is already a 64-bit integer.

import Bitwise
# EchoData.Snowflake.extract/1, in three lines
timestamp = (snowflake >>> 22) + 1_704_067_200_000
worker    = (snowflake >>> 12) &&& 0x3FF   # 10 bits → 0..1023
sequence  =  snowflake &&& 0xFFF          # 12 bits → 0..4095

F4.07.2 · a 64-bit integer

Time, worker, and sequence packed tight, read with shifts and masks.

F4.07.3 · the branded string

That integer, base62-encoded and namespaced, becomes the id the portal hands out.

Next: branded ids — how the 64-bit integer becomes a compact, url-safe string with a namespace prefix.

References

Primary sources for this lesson, and where it connects in the course.

Sources

Related in this course