LESSON 03 · Bit layout

Budget the 64 bits

A familiar layout divides time, worker identity, and a per-millisecond sequence.

SYSTEM VIEW

The ID path

Diagram view
Bits—
Lifespan—
Per worker—

Watch one Worker split 64 bits among time, identity and sequence, then shift four fields into one ID.

  1. One example layout splits 64 bits into five fields, always in this order: 1 sign bit, 41 bits of time from the Clock, 5 data-center and 5 machine bits naming the Worker, and 12 bits of Sequence. Together they fill exactly 64.

    Sign, time
    1 + 41 bits
    Data center, machine
    5 + 5 bits
    Sequence
    12 bits

    What changed

    • Sign: 1 bit
    • Time: 41 bits
    • Worker: 5 + 5 bits
    • Sequence: 12 bits
  2. The Clock counts milliseconds from a custom epoch, a start date chosen instead of 1970. 41 bits span about 69.7 years: from 1970 they run out in 2039, from the start of 2024 in 2093. Record the epoch permanently; decoding old IDs depends on it.

    From 1970
    runs out in 2039
    From 2024
    lasts until 2093

    What changed

    • Epoch: start of 2024
    • Lasts until 2093, not 2039
  3. 5 data-center bits give 32 values, and 5 machine bits give 32 within each: 32 × 32 workers, and this Worker is data center 3, machine 7. The Sequence’s 12 bits give 4,096 values per millisecond per worker.

    Workers
    32 × 32 = 1,024
    This worker
    dc 3 · machine 7
    Sequence
    4,096 per ms

    What changed

    • 32 × 32 workers
    • 4,096 values per ms
  4. Millisecond 1,000 shifts into the high bits (1000 << 22), then data center 3 and machine 7 (3 << 17, 7 << 12), then sequence 5. OR them together: 4,194,725,893. The sign bit stays 0, so the Orders DB’s signed 64-bit key is positive.

    Fields
    1,000 · 3 · 7 · 5
    ID
    4,194,725,893
    Sign bit
    0: positive

    What changed

    • Orders DB
    • ID 4,194,725,893
    • Sign bit kept 0
  5. 4,096 values per millisecond let one Worker issue up to 4,096,000 IDs a second, far more than a 25,000-per-second system needs. Real infrastructure may not deliver that figure anyway.

    One worker
    up to 4,096,000 IDs/s
    System needs
    25,000 IDs/s

    What changed

    • Up to 4,096,000 IDs/s
    • Needed: 25,000 IDs/s
  6. Widths are a budget: 41 · 12 gives 4,096 per millisecond for about 69.7 years. Try 43 · 10: 2 bits move from sequence to time, leaving 1,024 per millisecond for about 279 years. Never change a layout silently if two schemes share a namespace.

    Twitter’s Snowflake
    41 · 10 · 12 bits
    Discord
    42-bit time from 2015

    What changed

    • A choice: 41 · 12 or 43 · 10
    • 43 · 10: 1,024/ms, about 279 years
    Time · sequence

Space plays · ← → step · Home resets

THE IDEA

How it works

One example uses one reserved sign bit, 41 bits of milliseconds since a custom epoch, 5 data-center bits, 5 machine bits, and 12 sequence bits. A custom epoch is a start date you choose in place of 1970. That layout provides 32 data-center values, 32 machine values within each, and 4,096 sequence values per millisecond per worker. The 41-bit timestamp spans about 69.7 years: counted from 1970 it would run out in 2039, while counted from the start of 2024 it lasts until 2093. Record the epoch permanently because decoding old IDs depends on it.

Compose the value by shifting the timestamp into the high bits, then the worker fields, then the sequence. For timestamp 1,000, data center 3, machine 7, and sequence 5, the ID is (1000 << 22) | (3 << 17) | (7 << 12) | 5 = 4,194,725,893. Keep the sign bit zero so a signed 64-bit database field stores a positive number.

Treat field widths as a budget you can spend differently. This layout allows 4,096,000 IDs per second per worker, far more than a 25,000-per-second system needs, and real infrastructure may not deliver that figure anyway. Moving 2 bits from sequence to timestamp leaves 1,024 values per millisecond and stretches the span to about 279 years. Twitter’s original Snowflake used 41 time bits, 10 machine bits, and 12 sequence bits; Discord documents a 42-bit timestamp from a 2015 epoch. Never change a layout silently if IDs from two schemes share a namespace.

DECISION POINT

A design doubles sequence bits but keeps the ID at 64 bits. What must give way?

Write a sentence or two to unlock the guidance. Committing first is what makes it stick.

Keep in mind

  • Field widths are a capacity budget that must fit the contract.
  • The epoch and layout become durable compatibility rules.
CHECK YOUR UNDERSTANDING

Put it into practice.

1 OF 3
RecallQUESTION 01

What are the fields in the example Snowflake-style layout?

Choose an answer. You’ll see the explanation once you get it right.

Next lesson

0 of 3 checkpoints answered correctly to complete this lesson.