What this integer is
A Snowflake id is a 64-bit integer that a fleet can mint without asking a single database for the next number. Twitter published the layout this page uses: 1 sign bit that stays 0, 41 bits of milliseconds since a chosen epoch, 5 bits of datacenter, 5 bits of worker, and 12 bits of sequence. 1 + 41 + 5 + 5 + 12 = 64. Because the sign bit is 0, the value fits in a signed 64-bit integer as well as an unsigned one.
The 41-bit field counts milliseconds, so the id increases with time and a database can use it as a clustered key. The field holds 2^41 - 1 milliseconds, about 69.7 years, measured from the epoch you pick. It does not hold a calendar date by itself.
Bit layout, and a value you can check
This page builds the integer as (delta << 22) | (datacenter << 17) | (worker << 12) | sequence. delta is the Unix millisecond time minus the epoch, masked to 41 bits. Datacenter and worker are each masked to 5 bits (0 through 31). Sequence is masked to 12 bits (0 through 4095).
Take timestamp 2020-01-01T00:00:00.000Z, which is 1577836800000. Subtract the Twitter epoch 1288834974657 (2010-11-04T01:42:54.657Z). The delta is 289001825343. With datacenter 1, worker 2, and sequence 3 the integer is 1212161512043585539. Decoding shifts the other way: the 41 bits above position 22, plus the same epoch, return 1577836800000. Bits 17-21 are datacenter 1, bits 12-16 are worker 2, and the low 12 bits are sequence 3.
1212161512043585539
bit 63 sign 0
bits 22-62 timestamp 00100001101001001110110011110010000111111
bits 17-21 datacenter 00001 = 1
bits 12-16 worker 00010 = 2
bits 0-11 sequence 000000000011 = 3The epoch is part of the id
The integer does not store which epoch was used. This page offers three presets. Twitter is 1288834974657. Discord is 1420070400000, which is 2015-01-01T00:00:00.000Z. Unix is 0. Decoding 1212161512043585539 with the Discord epoch does not return 2020-01-01. You get a time 1420070400000 milliseconds earlier than the truth, because you added the wrong constant.
The 41-bit field also sets an end date. Twitter's epoch plus 2^41 - 1 milliseconds is 2080-07-10T17:30:30.208Z. Discord's epoch reaches 2084-09-06T15:47:35.551Z. Past that point a generator that keeps the same width will wrap the timestamp unless the operators move the epoch or widen the field. Wrapping reuses old ids.
When to use it
- You want a numeric primary key that sorts by creation time and stays inside a 64-bit integer column.
- You can give every process a unique pair of datacenter and worker, and you can keep a sequence for that pair.
- You are reading an id from a log and need the time back. You must already know the producer's epoch and that it used this 5/5/12 split.
Pitfalls
- Two processes that share a datacenter and a worker will issue the same integer if they also share the millisecond and the sequence. Uniqueness is an assignment problem, not a random one.
- 4096 is not a rate you can exceed inside one millisecond on one worker. A correct generator waits for the next millisecond or returns an error. A generator that wraps the sequence to 0 will collide.
- Discord, Mastodon, and other fleets use the same widths with different epochs, and some swap the meaning of the two 5-bit fields. Parse with the producer's map. This page treats bits 17-21 as datacenter and bits 12-16 as worker.
- JavaScript cannot hold this integer in a Number. The safe integer limit is 2^53 - 1, and a Snowflake is larger. Keep it as a string or a BigInt until it reaches a 64-bit column.
- The custom time control on this page stamps whatever clock you type. It does not check that the time is after the epoch. A timestamp before the epoch underflows the delta.
Snowflake next to ULID and UUID v4
| Snowflake (this page) | ULID | UUID v4 | |
|---|---|---|---|
| Stored as | 64-bit integer | 26-character string, 128 bits | 36-character hex string, 128 bits |
| Time order | Yes, from the 41-bit field | Yes, from the 48-bit prefix | No |
| Needs a worker id | Yes, or the ids collide | No | No |
| Per-millisecond budget | 4096 per worker | 2^80 random suffixes | Not bucketed by time |
| Readable clock | Only if you know the epoch | Yes, the prefix is absolute Unix time | No clock |
Questions
Which bits are the timestamp?
Bits 22 through 62, 41 bits in all. Shift the integer right by 22 and mask with 0x1ffffffffff. Add the epoch in milliseconds to get the Unix time. Bit 63 is the sign and stays 0.
Why does the same integer decode to two different dates?
The epoch is not stored in the integer. Twitter's epoch and Discord's epoch differ by 1420070400000 - 1288834974657 milliseconds. Use the epoch the producer used, or the calendar time is wrong by that constant.
What happens in the year 2080?
For the Twitter epoch, the 41-bit millisecond counter is exhausted at 2080-07-10T17:30:30.208Z. After that the field cannot represent a later delta. Operators have to change the epoch or the width before then. The Discord epoch lasts until 2084-09-06T15:47:35.551Z.
Is a Snowflake random?
No. Given the time, the datacenter, the worker, and the sequence, the integer is determined. Collision resistance comes from never reusing that tuple, not from a birthday bound.
Can I paste a Discord snowflake here?
Yes, if you select the Discord epoch first. The bit widths match the layout this page uses. If the producer assigned the 5-bit fields in the other order, the datacenter and worker numbers will be swapped, but the timestamp will still be right.