Skip to content

Commit 18b124e

Browse files
committed
Added more details on ID format
1 parent 812a8db commit 18b124e

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,24 @@ This assumes you already have a working Go environment, if not please see
2727
go get github.com/bwmarrin/snowflake
2828
```
2929

30+
### ID Format
31+
By default, the ID format follows the original Twitter snowflake format.
32+
* The ID as a whole is a 63 bit integer stored in an int64
33+
* 41 bits are used to store a timestamp with millisecond precision, using a custom epoch.
34+
* 10 bits are used to store a node id - a range from 0 through 1023.
35+
* 12 bits are used to store a sequence number - a range from 0 through 4095.
36+
37+
### Custom Format
38+
Coming soon.. ish. I plan to find the best way to modify the existing package to allow altering the number of bits used for Node ID's and Sequence Numbers. The goal is to implement this without breaking the API (1/31/18)
39+
40+
### How it Works.
41+
Each time you generate an ID, it works, like this.
42+
* A timestamp with millisecond precision is stored in the first 41 bits of the ID.
43+
* Then the NodeID is added in subsequent bits.
44+
* Then the Sequence Number is added, starting at 0 and incrementing for each ID generated in the same millisecond. If you generate enough IDs in the same millisecond that the sequence would roll over or overfill then the generate function will pause until the next millisecond.
45+
46+
Using the default settings, this allows for 4096 unique IDs to be generated every millisecond, per Node ID.
47+
3048
### Usage
3149

3250
Import the package into your project then construct a new snowflake Node using a

0 commit comments

Comments
 (0)