Security News
GitHub Removes Malicious Pull Requests Targeting Open Source Repositories
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
The hashring npm package is a consistent hashing library that allows for the distribution of keys across a set of nodes. It is useful for load balancing, distributed caching, and sharding data across multiple servers.
Creating a Hash Ring
This feature allows you to create a hash ring with a set of nodes. The nodes can be any identifier, such as server names or IP addresses.
const HashRing = require('hashring');
const ring = new HashRing(['node1', 'node2', 'node3']);
Adding and Removing Nodes
This feature allows you to dynamically add or remove nodes from the hash ring. This is useful for scaling up or down the number of nodes in your system.
ring.add('node4');
ring.remove('node2');
Getting the Node for a Key
This feature allows you to get the node responsible for a given key. This is useful for determining which node should handle a particular piece of data.
const node = ring.get('myKey');
Weighting Nodes
This feature allows you to assign weights to nodes, so some nodes can handle more keys than others. This is useful for load balancing when some nodes have more capacity than others.
const weightedRing = new HashRing({'node1': 1, 'node2': 2, 'node3': 1});
The consistent-hashing package provides similar functionality for consistent hashing. It allows for the distribution of keys across a set of nodes and supports adding and removing nodes dynamically. Compared to hashring, it is simpler and may have fewer features, but it is still effective for basic consistent hashing needs.
Hash ring provides consistent hashing based on the libketema
library.
You can either install it using the Node Package Manager (NPM)
npm install hashring
Or fork this repository to your machine
git clone git://github.com/3rd-Eden/node-hashring.git hashring
The constructor is designed to handle multiple arguments types as the hash ring
can be used for different use cases. You have the ability to use a String
to
add a single server, a Array
to provide multiple servers or an Object
to
provide servers with a custom weight. The weight can be used to give a server a
bigger distribution in the hash ring. For example you have 3 machines, 2 of
those machines have 8 gig memory and one has 32 gig of memory because the last
server has more memory you might it to handle more keys than the other server.
So you can give it a weight of 2 and the other servers a weight of 1.
Creating a hash ring with only one server
var hashring = require('hashring');
var ring = new hashring('192.168.0.102:11212');
Creating a hash ring with multiple servers
var hashring = require('hashring');
var ring = new hashring([ '192.168.0.102:11212', '192.168.0.103:11212', '192.168.0.104:11212']);
Creating a hash ring with multiple servers and weights
var hashring = require('hashring');
var ring = new hashring({
'192.168.0.102:11212': 1
, '192.168.0.103:11212': 2
, '192.168.0.104:11212': 1
});
Creating a hash ring with multiple servers an vnodes selected per nodes
var hashring = require('hashring');
var ring = new hashring({
'192.168.0.102:11212': {"vnodes": 5}
, '192.168.0.103:11212': {"vnodes": 10}
, '192.168.0.104:11212': {"vnodes": 7}
});
Optionaly you could add the weigth property to the object.
By default the hash ring uses a JavaScript crc32 implementation hashing algorithm. But this can be overwritten by adding a second argument to the constructor. This can be anything that is supported as hashing algorithm by the crypto module.
var hashring = require('hashring');
var ring = new hashring('192.168.0.102:11212', 'md5');
I have chosen crc32 as default algorithm because a creates a nice dense ring distribution. Another good alternative and common used hashing algorithm is md5. The JavaScript crc32 algorithm is faster than md5. So If you are doing allot of operations per seconds these small differences can really matter.
In these examples I assume that you already setup a hashring
instance, with
the variable name ring
like I did the in the examples illustrated above.
a.k.a key -> node look up, this is where all the magic is happening.
ring.get('foo'); // => '192.168.0.104:11212'
ring.get('pewpew'); // => '192.168.0.103:11212'
If you are experiencing downtime with one of your servers, you might want to
hot swap
with a new server.
ring.replace('192.168.0.104:11212','192.168.0.112:11212');
ring.get('foo'); // => '192.168.0.112:11212'
Adds a new server to the hash ring, but please note that this could cause a shift in current key -> server distribution.
ring.add('192.168.0.102:11212');
Remove a server from the generated hash ring.
ring.remove('192.168.0.102:11212');
Iterates over the nodes for a give key, can be used to create redundancy support.
ring.range('key', 3);
Clean up the internal hash ring, kill the cache, kill nodes, nuke the planet.
range.end();
For a more extensive documentation: Read the source, it's not rocket sience.
FAQs
A consistent hashring compatible with ketama hashing and python's hash_ring
We found that hashring demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.