Scrivio Interview library
CodingFoundations6 min read · updated 13 November 2026

Snowflake-style ids are 64-bit numbers generated without coordination on each node. They combine a timestamp, a worker id and a per-millisecond sequence, so they are unique and roughly sorted by time.

Describe the usual bit layout and its limits, and outline in Java the nextId() method of such a generator.

A Snowflake-style id packs 1 unused sign bit, 41 bits of milliseconds since a custom epoch (about 69 years), 10 bits of worker id (1,024 workers) and 12 bits of sequence (4,096 ids per millisecond per worker) into one 64-bit number. nextId() runs under a lock: it reads the clock, increments a masked sequence within the same millisecond, spins to the next millisecond when the sequence wraps, resets it when time moves on, refuses to issue ids when the clock goes backwards, and composes ((ts - EPOCH) << 22) | (workerId << 12) | sequence. Ids are only roughly time-ordered across workers and reveal creation time and volume.

Why this question is asked

Unique ids are the first thing every distributed system needs and one of the first things it gets wrong. A database sequence is simple but becomes a central bottleneck; random UUIDs are coordination-free but large and unordered. Snowflake-style ids sit in between, and interviewers like them because the implementation is short enough to write on a whiteboard and full of small details that matter. In this answer, we will recap the bit layout and its limits, write nextId() step by step, check it with a worked example, and look at what these ids reveal and where their ordering stops.

1A Quick Recap of the Bit Layout

The idea is to build an id from three parts that, together, can never repeat. The timestamp makes ids from different milliseconds different. The worker id, unique per running generator, makes ids from different machines different within the same millisecond. The sequence makes ids from the same machine in the same millisecond different. All three fit into a Java long, so the id can be stored in a BIGINT column and compared quickly.

Snowflake-style 64-bit layout
FieldBitsRangeLimit
Sign1always 0keeps ids positive in a signed long
Timestamp41ms since custom epochabout 69 years
Worker id100 to 1,0231,024 generators at a time
Sequence120 to 4,0954,096 ids per ms per worker

Each field's width sets a limit. 41 bits of milliseconds cover 2^41 ms, about 69.7 years from our chosen epoch. A custom epoch, such as the start of 2025 instead of 1970, gives the full 69 years to the system's own lifetime. 10 bits of worker id allow 1,024 generators at once. 12 bits of sequence allow 4,096 ids per millisecond per worker, about four million per second per worker, which is rarely the bottleneck. The sign bit stays 0 so ids are positive.

2Writing nextId() Step by Step

The generator keeps two pieces of state: the last timestamp it used and the sequence within that millisecond. Because several threads call nextId() at once, the whole method runs under a lock; synchronized is simple and fast enough here, and a lock-free version would pack both fields into one AtomicLong and use compare-and-set.

Java
public final class SnowflakeIdGenerator {
private static final long EPOCH = 1735689600000L; // 2025-01-01T00:00:00Z
private static final int WORKER_BITS = 10, SEQUENCE_BITS = 12;
private static final long MAX_WORKER = (1L << WORKER_BITS) - 1; // 1023
private static final long SEQUENCE_MASK = (1L << SEQUENCE_BITS) - 1; // 4095
private static final long MAX_BACKWARD_MS = 5;
private final long workerId;
private long lastTimestamp = -1L;
private long sequence = 0L;
public SnowflakeIdGenerator(long workerId) {
if (workerId < 0 || workerId > MAX_WORKER)
throw new IllegalArgumentException("workerId out of range: " + workerId);
this.workerId = workerId;
}
public synchronized long nextId() {
long now = System.currentTimeMillis();
if (now < lastTimestamp) { // clock went backwards
long behind = lastTimestamp - now;
if (behind > MAX_BACKWARD_MS)
throw new IllegalStateException("clock moved back " + behind + " ms");
now = waitUntil(lastTimestamp); // small gap: wait it out
}
if (now == lastTimestamp) {
sequence = (sequence + 1) & SEQUENCE_MASK; // 4095 + 1 -> 0
if (sequence == 0) now = waitUntil(lastTimestamp + 1); // 4,096 used: next ms
} else {
sequence = 0; // new millisecond
}
lastTimestamp = now;
return ((now - EPOCH) << (WORKER_BITS + SEQUENCE_BITS)) // << 22
| (workerId << SEQUENCE_BITS) // << 12
| sequence;
}
private static long waitUntil(long target) {
long now = System.currentTimeMillis();
while (now < target) { Thread.onSpinWait(); now = System.currentTimeMillis(); }
return now;
}
}

Notice the order of the checks. First we protect against a clock that moved backwards, because reusing an old timestamp with the same worker id and a restarted sequence would produce ids we have already issued. A gap of a few milliseconds is waited out; a larger one fails loudly, since silently waiting seconds would stall every request and hide a real problem. Then, within the same millisecond, we increment the sequence and mask it to 12 bits; when it wraps to 0 we have used all 4,096 values, so we spin until the clock moves on. In a new millisecond the sequence starts again at 0. Finally the three fields are shifted into place and combined with bitwise OR.

3Checking the Composition With Real Numbers

It helps to compute one id by hand. Suppose 1,000 ms have passed since the epoch, the worker id is 5 and the sequence is 3.

Java
long ts = 1_000, worker = 5, seq = 3;
long id = (ts << 22) | (worker << 12) | seq;
// ts << 22 = 4194304000
// worker << 12 = 20480
// id = 4194324483
long tsBack = id >>> 22; // 1000
long workerBack = (id >>> 12) & 1023; // 5
long seqBack = id & 4095; // 3

Because each field sits in its own range of bits, OR never mixes them, and shifting and masking recover each part. That is also why the timestamp must be in the highest bits: comparing two ids numerically compares their timestamps first, which makes ids roughly sortable by time.

4Limits: Rough Ordering and What Ids Reveal

Ids from one worker increase strictly. Across workers they are only roughly ordered by time: two servers whose clocks differ by 20 ms produce ids that can be out of order by up to 20 ms, and within one millisecond, a higher worker id sorts after a lower one regardless of which request came first. So ids are good for sorting feeds approximately and for keeping B-tree inserts mostly at the end of the index, but not for strict ordering of events.

Ids also reveal information. Anyone can shift an id right by 22 bits and read its creation time, and by watching ids over time they can estimate how many objects we create per second. In public URLs, that can leak business volumes or let users guess neighbouring ids. And JavaScript numbers lose precision above 2^53, so we send these ids to browsers as strings in JSON.

5Follow-up questions

How would you change the bit split for 10,000 workers?

10,000 workers need 14 bits, because 2^13 is 8,192 and 2^14 is 16,384. The four extra bits must come from somewhere. Taking them from the sequence leaves 8 bits, 256 ids per millisecond per worker, which is still 256,000 per second per worker and usually plenty. Taking them from the timestamp instead would cut the lifetime from about 69 years to about 17 years. So the usual split becomes 1 sign bit, 41 timestamp bits, 14 worker bits and 8 sequence bits.

How are worker ids assigned?

They must be unique among running generators at all times. Small systems configure them per host; in containers, where pods come and go, a lease from a coordination store or database is safer than deriving them from host names, which can collide.

Why not use UUIDs instead?

Random UUIDs need no coordination at all but are 128 bits and unordered, which bloats indexes and scatters inserts. UUIDv7 is time-ordered and also needs no worker ids, so it is a good choice when 128 bits are acceptable; Snowflake ids are for when we need ids to fit in 64 bits.

6Conclusion

In this answer, we saw the usual Snowflake layout of 1 sign bit, 41 timestamp bits, 10 worker bits and 12 sequence bits, giving about 69 years, 1,024 workers and 4,096 ids per millisecond per worker. We wrote nextId() under a lock, incrementing a masked sequence within a millisecond, spinning when it wraps, resetting it when time moves, and refusing to issue ids when the clock goes backwards, then composed the id with shifts. The ids are only roughly time-ordered across workers and reveal creation time and volume.

What to say in the interviewAbout 80 seconds aloud

Open with

A Snowflake-style id is a 64-bit long made of a sign bit that is always zero, 41 bits of milliseconds since a custom epoch, which lasts about 69 years, 10 bits of worker id, so 1,024 generators, and 12 bits of sequence, so 4,096 ids per millisecond per worker.

Then, if they want more

In nextId(), which I would make synchronized, I read the current time. If it is lower than the last timestamp, the clock went backwards, and I must not issue ids, because I would repeat ones already used: if the gap is a few milliseconds I wait, otherwise I throw.

If the time equals the last timestamp, I increment the sequence and mask it with 4095; if it wraps to zero, I spin until the next millisecond. If time moved on, I reset the sequence to zero.

Then I store the timestamp and compose the id as timestamp minus epoch shifted left by 22, OR worker id shifted left by 12, OR sequence. The limits are that ids are only roughly time-ordered across workers, because clocks differ, and they leak creation time and volume, which matters if they appear in public URLs.

I would also send them to JavaScript clients as strings, since they exceed 2 to the 53.

About this answer

Last updated
13 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Describe the usual bit layout and its limits, and outline in Java the nextId() method of...": https://scrivio-interview-library.netlify.app/architecture/storage-and-pipelines/26552c4410/