flake / 2.0.0 last updated 9 months ago created on Dec 10th 2012
Install
npm install --save flake
Clone
git clone git@github.com:chilts/flake.git
dependencies
No dependencies listed.
maintainers
versions
12 total
2.0.0 | 9 months ago | chilts |
1.0.0 | 2 years ago | chilts |
0.5.0 | 3 years ago | chilts |
0.4.4 | 3 years ago | chilts |
0.4.3 | 3 years ago | chilts |
0.4.2 | 3 years ago | chilts |
0.4.1 | 3 years ago | chilts |
0.4.0 | 3 years ago | chilts |
0.3.1 | 4 years ago | chilts |
0.3.0 | 4 years ago | chilts |
0.2.0 | 4 years ago | chilts |
0.1.0 | 4 years ago | chilts |
readme
Flake
Generate practically unique approximately sortable IDs in a distributed environment.
Flake Numbers
There are 3.403e+38 (or 340,282,366,920,938,463,463,374,607,431,768,211,456) possible flake numbers. Use in distributed environments where you don't have a central service which can be a single point of failure - perfect when you want everything to be redundant and independent of each other.
The make-up of a Flake ID
Example Flake IDs from two different machines:
As you can see, the Flake Id is made up of: timestamp-counter-pid-macaddress
and can be seen in the
following table:
As you can see, each Flake ID is 128 bits long, consisting of a 48 bit timestamp, 16 bit counter, 16 bit PID and a 48 bit mac address.
All of these FlakeIDs are approximately sortable
and practically unique
.
Usage
Firstly, find a network interface you can use so you can obtain a mac address:
Look for something like 'eth0', 'en0', 'wlan0', 'venet0' or 'net0' (Note: you may have a number other than 0.) Don't choose something like 'lo' or 'tun0'. Experiment to make sure it works. :)
In the following example, I'm using eth0
, but this is not guaranteed to work on your machine. You need to change
it to something which exists on your machine.
Would give something like:
Running it again might give:
As you can see, the time has increased by a few seconds and the PID is different. The mac address is the same and the sequence always resets.
Note: these unique IDs are not the same as UUIDs or can be used in place of one.
Format of each Unique ID
Each unique ID has 4 sections which are:
Unlike Twitter's Snowflake, we also use the PID since we want to be able to generate unique IDs on the same machine at the same time. This is because we're generating them within the program which needs them and not in a client/server architecture.
Approximately Sortable
If you were to generate IDs in multiple processes on the same machine and on multiple machines, then whilst we can't guarantee that the IDs will be exactly sortable, they will be approximately sortable. In fact, to the millisecond (assuming your clocks aren't skew from each other).
e.g. some IDs generated on the same machine in two processes, using 'eth0' and 'wlan0' as the MAC address could be, and printing out alternately:
As you can see, the IDs are firstly sorted by timestamp, then sequence, the PID then mac address.
Non-Monotomic Clocks
Currently there is a possibility to generate the same ID in the same process on the same machine. This is described by System Clock Depedency.
This situation hasn't yet been solved but is known about. Therefore, you should run NTP in a mode which doesn't move the clock backwards.
Author
Written by Andrew Chilton - Blog - .
License
Copyright (c) 2012 Andrew Chilton.
MIT License : http://chilts.mit-license.org/2012/
(Ends)