A utility library for creating a service that generates unique IDs at scale. Think of it as containing the common logic of Twitter's Snowflake and Boundary's Flake.
Want to see pretty graphs? Log in now!
npm install emiflake
|5||downloads in the last week|
|19||downloads in the last month|
|Last Published By|
|Version||1.1.0 last updated a month ago|
|Keywords||id, generation, distributed, flake|
emiflake is a node.js utility library for creating a service that generates unique IDs at scale. Think of it as containing the common logic of Twitter Snowflake and Boundary Flake, sans network and machine ID generation stuff.
emiflake is useful if you want something like Snowflake, but don't want to use Zookeeper to coordinate the machine ID allocation. Another situation where the library is useful is when you want something like Boundary Flake, but don't want to add a dependency to Erlang.
flake.cc contain the actual logic, and are usable by themselves, without node.js.
The current implementation uses the C standard function
gettimeofday to get the time.
The length of
emiflake ids vary depending on the parameters: They are
machineNumberLength bits long.
The structure of the ids are: The most significant bits contain the timestamp. After that is the sequence number, and last is the machine number.
When is it appropriate to use
To replicate the beavior of Boundary Flake, use parameters like this:
machineNumberLength: 48, and use a MAC address as the machine number.
To do something similar to Twitter Snowflake, use parameters like this:
machineNumberLength: 10, and use some coordination mechanism to choose the machine number.
timestampOffset: Something like (2012-1970)365246060*1000
npm install emiflake
Generate an id:
var emiFlake = require('emiflake'); var f = new emiFlake(new Buffer("mid"), /*machineNumberLength:*/24, /*sequenceNumberLength:*/16, /*timestampLength:*/24, /*timestampOffset:*/20000); f.generate(console.log);
new emiFlake(machineNumber, machineNumberLength, sequenceNumberLength, timestampLength[, timestampOffset])
Create a new flake object.
machineNumbermust be a Buffer object
machineNumberLengthshould be an integer specifying how many bits of the
machineNumberthat should be used.
sequenceNumberLengthshould be an integer specifying how many bits the sequence number should use. Must be less than or equal to 64.
timestampLengthshould be an integer specifying how many bits the timestamp should use. Must be less than or equal to 64.
timestampOffsetspecifies the epoch, as a positive number of milliseconds since 00:00 Jan 1, 1970. Optional. The default value is 0. When using a small timestamp length, this parameter is useful to maximize the range. For instance, one could set the 0 timestamp to be at the start of 2012.
Generates a unique ID.
cb should be a callback that takes the resulting ID as a buffer. In case of sequence number overflow or minor backward jumps in the wall time, it will wait and try again. If the wall time has jumped backwards a lot,
cb will be invoked with
generate never invokes
cb before it returns.
Unspecified return value.
Returns the size of ID buffers in bytes. This is equal to ceil([the bit length]/8).
Returns the size of the IDs in bits.