gossip based application sharding
npm install swim-hashringApplication-level sharding for node.js, similar to
ringpop. You can use it to
maintain in-memory state between a cluster of nodes, and it allows you
to route your requests accordingly.
swim-hashring is a library that implements a distributed sharding system using a gossip membership protocol (swim) and a consistent hash ring based on farmhash.
This library does not assume any particular application protocol.
```
npm i swim-hashring -g
* hashring()
* instance.lookup()
* instance.next()
* instance.allocatedToMe()
* instance.peers()
* instance.whoami()
* instance.mymeta()
* instance.hash()
* instance.close()
Create a new hashring.
Options:
* name: the name of the ring, it defaults to 'hashring'. Needed ifmeta
you want to run mulitple hashrings into the same swim network.
* : all the metadata for the current node, it will be disseminatedhashFunc
across the gossip network.
* : the hashing function you want to use, default tofarmhash.hash32
.replicaPoints
* : the number of replica points each node would have.host
Every node needs to have the same configuration for this value.
* : the ip address the current node will use to advertise itselfport
on the swim network. Defaults to what is returned by
network-address.
* : the port the current node will use to advertise itselfbase
on the swim network. Randomly picked if not specified.
* : an array of nodes that will be used to boostrap the swimwhoami()
network. The value is what is returned by .client
* : if you are writing an hashring client rather than a normalfalse
peer. Defaults to .
Events:
* 'up': when the node is up and running'peerUp'
* : when a peer that is part of the hashring gets online'peerDown'
* : when a peer that is part of the hashring gets offline'move'
* : when a part of the hashring is moved from the current peerstart
to another peer, relevant keys , end, to.'steal'
* : when a part of the hashring is stolen by the current peerstart
from another peer, relevant keys , end, from.'error'
* : when an error occurs in the swim instance.
Lookup the peer handling a given key, which it can be a String, aBuffer or an integer. The integer needs to be the result ofinstance.hash(key).
It returns:
`js`
{
id: '192.168.0.1',
meta: {
// all metadata specified in
},
points: [
// n integers, where n is the number of replica points
]
}
Lookup the next peer in the hashring for the given key. It is possibleskip
to specify a list of ids of peers. Because of the skip list, it
is possible to implement a circuit breaker
to avoid messages that keeps flowing in infinite loops.
It returns:
`js`
{
id: '192.168.0.1',
meta: {
// all metadata specified in
},
points: [
// n integers, where n is the number of replica points
]
}
The id of the current peer. It will throw if the node has not emitted
'up' yet.
It returns the info of the current node in the same format of
lookup().
Similar to lookup(key), but returns true or false
depending if the given key has been allocated to this node or not.
Hashes the given key using the same hash function used to calculate
replica points. It returns an integer.
All the other peers, in the format of lookup.
if myself is set to true`, the current instance is returned as
well.
Close the instance, detaching it from the gossip network.
MIT