Skip to content

Feature: Allows overriding a getServer method for using a hashring #159

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 14, 2022

Conversation

sparkida
Copy link
Contributor

Continuing the conversation from #158.

This is a very simple addition to the codebase that will make it easy to hook in a consistent hashing ring.

I'd also like to suggest using node-hashring for performance instead of the 3rd-eden HashRing. Most notably, 3rd-Eden's implementation uses a 128-bit MD5 and then reduces it to a 32-bit uint and node-hashring uses the 32-bit MurmurHash algorithm which offers tremendous performance gains.

Usage

const memjs = require('memjs');
const HashRing = require('node-hashring');
const servers = ['localhost:11211', 'localhost:11212'];
// build a map of server addresses to their index in the server list
const serverMap = {};
servers.forEach((server, index) => serverMap[server] = index);
const client = memjs.Client.create(servers.join(','));
// build the hashring
const hashRing = new HashRing(servers);
// override the getServer method
client.getServer = (key) => serverMap[hashRing.findNode(key)];

client.set('hello', 'world', {expires: 600}, function(err) {
  console.error(err);
});

client.get('hello', function(err, val) {
  if (err) {
    console.error(err);
  }
  console.log(val.toString());
});

Copy link
Member

@saschat saschat left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good to me, just one small comment.

@@ -82,12 +82,18 @@ Client.create = function(serversStr, options) {
return new Client(servers, options);
};

// An overridable method you can use for determing
// server selection. Should return the server index
// in the list of servers on Client#servers.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add the usage example to the comment?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just added the example, let me know if it needs changing, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants