- Notifications
You must be signed in to change notification settings - Fork18
Flake ID generator yields k-ordered, conflict-free ids in a distributed environment in Node.js
License
vipermind/flake-idgen
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
Flake ID generator yields k-ordered, conflict-free ids in a distributed environment.
$ npm install --save flake-idgen ⏎
The Flake ID is made up of:timestamp,datacenter,worker andcounter. Examples in the following table:
+-------------+------------+--------+---------+--------------------+| Timestamp | Datacenter | Worker | Counter | Flake ID |+-------------+------------+--------+---------+--------------------+| 0x8c20543b0 | 00000b | 00000b | 0x000 | 0x02308150ec000000 |+-------------+------------+--------+---------+--------------------+| 0x8c20543b1 | 00000b | 00000b | 0x000 | 0x02308150ec400000 |+-------------+------------+--------+---------+--------------------+| 0x8c20543b1 | 00000b | 00000b | 0x001 | 0x02308150ec400001 |+-------------+------------+--------+---------+--------------------+| 0x8c20543b1 | 00000b | 00000b | 0x002 | 0x02308150ec400002 |+-------------+------------+--------+---------+--------------------+| 0x8c20543b1 | 00000b | 00000b | 0x003 | 0x02308150ec400003 |+-------------+------------+--------+---------+--------------------+| 0x8c20c0335 | 00011b | 00001b | 0x000 | 0x02308300cd461000 |+-------------+------------+--------+---------+--------------------+| 0x8c20c0335 | 00011b | 00001b | 0x001 | 0x02308300cd461001 |+-------------+------------+--------+---------+--------------------+As you can see, each Flake ID is 64 bits long, consisting of:
timestamp, a 42 bit long number of milliseconds elapsed since 1 January 1970 00:00:00 UTCdatacenter, a 5 bit long datacenter identifier. It can take up to 32 unique values (including 0)worker, a 5 bit long worker identifier. It can take up to 32 unique values (including 0)counter, a 12 bit long counter of ids in the same millisecond. It can take up to 4096 unique values.
Breakdown of bits for an id e.g.5828128208445124608 (counter is0, datacenter is7 and worker3) is as follows:
010100001110000110101011101110100001000111 00111 00011 000000000000 |------------| 12 bit counter |-----| 5 bit worker |-----| 5 bit datacenter |----- -----| 10 bit generator identifier|------------------------------------------| 42 bit timestampNote that composition ofdatacenter id andworker id makes 1024 unique generator identifiers. By modifying datacenter and worker id we can get up to 1024 id generators on a single machine (e.g. each running in a separate process) or have 1024 machines with a single id generator on each. It is also possible to provide a single 10 bit long identifier (up to 1024 values). That id is internally split intodatacenter (the most significant 5 bits) andworker (the least significant 5 bits).
Flake ID Generator returns 8 byte long nodeBuffer objects with its bytes representing 64 bit long id. Note that the number is stored in Big Endian format i.e. the most significant byte of the number is stored in the smallest address given and the least significant byte is stored in the largest.
Flake id generator instance has one methodnext(cb) returning generated id (if a callback function is not provided) or calling provided callback function with two arguments:error andgenerated id.
The following example usesnext with no callback function:
varFlakeId=require('flake-idgen');varflakeIdGen=newFlakeId();console.log(flakeIdGen.next());console.log(flakeIdGen.next());console.log(flakeIdGen.next());
It would give something like:
<Buffer 50 dd d5 99 01 c0 00 00><Buffer 50 dd d5 99 02 80 00 00><Buffer 50 dd d5 99 02 80 00 01>The following example usesnext with callback function:
varFlakeId=require('flake-idgen');varflakeIdGen=newFlakeId();flakeIdGen.next(function(err,id){console.info(id);})flakeIdGen.next(function(err,id){console.info(id);})
It would give something like:
<Buffer 50 dd d6 49 ef c0 00 00><Buffer 50 dd d6 49 f0 00 00 00>Flake ID Generator can generate up to 4096 unique identifiers within a millisecond. When generator tries to generate more than 4096 identifiers within a millisecond, the following things will happen:
- When using
next()without a callback function, an error is thrown. - When using
next(cb)with a callback function, the callback function is called in the following millisecond without any error.
Flake Id generator constructor takes optional parameter (generator configuration options) with the following properties:
datacenter(5 bit) - datacenter identifier. It can have values from 0 to 31.worker(5 bit) - worker identifier. It can have values from 0 to 31.id(10 bit) - generator identifier. It can have values from 0 to 1023. It can be provided instead ofdatacenterandworkeridentifiers.epoch- number used to reduce value of a generated timestamp. Note that this number should not exceed number of milliseconds elapsed since 1 January 1970 00:00:00 UTC. It can be used to generatesmaller ids.
Example of usingdatacenter andworker identifiers:
varFlakeId=require('flake-idgen')varflakeIdGen1=newFlakeId();varflakeIdGen2=newFlakeId({datacenter:9,worker:7});console.info(flakeIdGen1.next());console.info(flakeIdGen2.next());
It would give something like:
<Buffer 50 dd da 8f 43 40 00 00><Buffer 50 dd da 8f 43 d2 70 00>Example of usingepoch parameter:
varFlakeId=require('flake-idgen')varflakeIdGen1=newFlakeId();varflakeIdGen2=newFlakeId({epoch:1300000000000});console.info(flakeIdGen1.next());console.info(flakeIdGen2.next());
It would give something like:
<Buffer 50 dd db 00 d1 c0 00 00><Buffer 05 32 58 8e d2 40 00 00>Flake Id generator has some properties that can be read from a generator instance:
datacenter- returns datacenter number used for generator creation; otherwise it returnsundefinedvalue.worker- returns worker number used for generator creation; otherwise it returnsundefinedvalue.id- returns worker identifier number used for generator creation or combines its value from datacenter and worker numbers. Identifier is always available and it is defaulted to zero.
Flake Id generator instantiated without any parameter getsdatacenter,worker andid values defaulted to zeros.
varFlakeId=require('flake-idgen')varflakeIdGen1=newFlakeId({id:100});varflakeIdGen2=newFlakeId({datacenter:9,worker:7});varflakeIdGen3=newFlakeId();console.info(flakeIdGen1.id);// 100console.info(flakeIdGen1.datacenter);// undefinedconsole.info(flakeIdGen1.worker);// undefinedconsole.info(flakeIdGen2.datacenter);// 9console.info(flakeIdGen2.worker);// 7console.info(flakeIdGen2.id);// 259console.info(flakeIdGen3.datacenter);// 0console.info(flakeIdGen3.worker);// 0console.info(flakeIdGen3.id);// 0
It would give something like:
100undefinedundefined97295000
From time to time Node.js clock may move backward. In most cases it is only a few millisecond. However, as the generator relies on current timestamp, it won't be able to generate conflict-free identifiers (i.e. without duplicates) until the clock catches up with the last timestamp value. In case of clock move backward the following things will happen:
- When using
next()without a callback function, an error is thrown. - When using
next(cb)with a callback function, the callback function is called with a new identifier generated once the clock catches up with the last timestamp.
Flake Id generator returns node Buffer representing 64-bit number for the sake of future extensions or returned buffer modifications. Node Buffer can also be very easily converted to string format. There is a NPMbiguint-format module which provides Buffer to string conversion functionality e.g.
varintformat=require('biguint-format'),FlakeId=require('flake-idgen')varflakeIdGen1=newFlakeId();varflakeIdGen2=newFlakeId({epoch:1300000000000});console.info(intformat(flakeIdGen1.next(),'dec'));console.info(intformat(flakeIdGen1.next(),'hex',{prefix:'0x'}));console.info(intformat(flakeIdGen2.next(),'dec'));console.info(intformat(flakeIdGen2.next(),'hex',{prefix:'0x'}));
It would give something like:
5827056208820830208// flakeIdGen1 decimal format0x50dddcbfb5c00001// flakeIdGen1 hex format374461008833413120// flakeIdGen2 decimal format0x5325a4db6000002// flakeIdGen2 hex format
Generated id could also be converted to binary string format, split into 4 digit groups of 0's and 1's e.g.
varintformat=require('biguint-format'),idGen=new(require('flake-idgen'))for(vari=0;i<5;i++){console.info(intformat(idGen.next(),'bin',{groupsize:4}));};
It would give something like:
0101000011011111101101100001010111000001010000000000000000000000// 0x50 df b6 15 c1 40 00 000101000011011111101101100001010111000101000000000000000000000000// 0x50 df b6 15 c5 00 00 000101000011011111101101100001010111000101000000000000000000000001// 0x50 df b6 15 c5 00 00 010101000011011111101101100001010111000101010000000000000000000000// 0x50 df b6 15 c5 40 00 000101000011011111101101100001010111000101010000000000000000000001// 0x50 df b6 15 c5 40 00 01
Written by Tom Pawlak -Blog
Copyright (c) 2014 Tom Pawlak
MIT License :https://blog.abelotech.com/mit-license/
About
Flake ID generator yields k-ordered, conflict-free ids in a distributed environment in Node.js
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors4
Uh oh!
There was an error while loading.Please reload this page.