Home

Awesome

Memcached Build Status

memcached is a fully featured Memcached client for Node.js. memcached is built with scaling, high availability and exceptional performance in mind. We use consistent hashing to store the data across different nodes. Consistent hashing is a scheme that provides a hash table functionality in a way that adding or removing a server node does not significantly change the mapping of the keys to server nodes. The algorithm that is used for consistent hashing is the same as libketama.

There are different ways to handle errors for example, when a server becomes unavailable you can configure the client to see all requests to that server as cache misses until it goes up again. It's also possible to automatically remove the affected server from the consistent hashing algorithm or provide memcached with a failover server that can take the place of the unresponsive server.

When these issues occur the memcached client will emit different events where you can subscribe to containing detailed information about the issues.

The client is configurable on different levels. There's a global configuration that you update so all your Memcached clusters will use the same failure configuration for example, but it's also possible to overwrite these changes per memcached instance.

protocol

As in other databases and message queues, this module uses the ASCII protocol to communicate with the server, which means that you can see what is send over the wire. For debugging this is easier for both the users and the developers however this also means that SASL auth is not supported because it demands the binary protocol.

Installation

npm install memcached

Setting up the client

The constructor of the memcached client take 2 different arguments server locations and options. Syntax:

var Memcached = require('memcached');
var memcached = new Memcached(Server locations, options);

Server locations

The server locations is designed to work with different formats. These formats are all internally parsed to the correct format so our consistent hashing scheme can work with it. You can either use:

  1. String, this only works if you are running a single server instance of Memcached. It's as easy a suppling a string in the following format: hostname:port. For example 192.168.0.102:11211 This would tell the client to connect to host 192.168.0.102 on port number 11211.

  2. Array, if you are running a single server you would only have to supply one item in the array. The array format is particularly useful if you are running a cluster of Memcached servers. This will allow you to spread the keys and load between the different servers. Giving you higher availability when one of your Memcached servers goes down.

  3. Object, when running a cluster of Memcached servers, some servers may allocate different amounts of memory, e.g. 128, 512, and 128mb. While by default all servers are equally important and dispatch consistently the keys between the servers (33/33/33%), it is possible to send more keys in servers having more memory. To do so, define an object whose key represents the server location and whose value represents a server weight, the default weight for a server being 1; so, for instance { '192.168.0.102:11211': 1, '192.168.0.103:11211': 2, '192.168.0.104:11211': 1 } distributes 50% of the keys on server 103, but only 25% on 104 and 25% on 102.

To implement one of the above formats, your constructor would look like this:

var memcached = new Memcached({ '192.168.0.102:11211': 1, '192.168.0.103:11211': 2, '192.168.0.104:11211': 1 });
var memcached = new Memcached([ '192.168.0.102:11211', '192.168.0.103:11211', '192.168.0.104:11211' ]);
var memcached = new Memcached('192.168.0.102:11211');

Options

Memcached accepts two option schemes. The first one inherits of all Memcached server instances while the second one is client specific and overwrites the globals. To define these options, Memcached server uses the same properties:

Example usage:

var memcached = new Memcached('localhost:11211', {retries:10,retry:10000,remove:true,failOverServers:['192.168.0.103:11211']});

If you wish to configure the options globally:

var Memcached = require('memcached');
// all global configurations should be applied to the .config object of the Client.
Memcached.config.poolSize = 25;

API

Public methods

memcached.touch Touches the given key.

memcached.touch('key', 10, function (err) { /* stuff */ });

memcached.get Get the value for the given key.

memcached.get('foo', function (err, data) {
  console.log(data);
});

memcached.gets Get the value and the CAS id.

memcached.gets('foo', function (err, data) {
  console.log(data.foo);
  console.log(data.cas);

  // Please note that the data is stored under the name of the given key.
});

memcached.getMulti Retrieves a bunch of values from multiple keys.

memcached.getMulti(['foo', 'bar'], function (err, data) {
  console.log(data.foo);
  console.log(data.bar);
});

memcached.set Stores a new value in Memcached.

memcached.set('foo', 'bar', 10, function (err) { /* stuff */ });

memcached.replace Replaces the value in memcached.

memcached.replace('foo', 'bar', 10, function (err) { /* stuff */ });

memcached.add Add the value, only if it's not in memcached already.

memcached.add('foo', 'bar', 10, function (err) { /* stuff */ });

memcached.cas Add the value, only if it matches the given CAS value.

memcached.gets('foo', function (err, data) {
  memcached.cas('foo', 'bar', data.cas, 10, function (err) { /* stuff */ });
});

memcached.append Add the given value string to the value of an existing item.

memcached.append('foo', 'bar', function (err) { /* stuff */ });

memcached.prepend Add the given value string to the value of an existing item.

memcached.prepend('foo', 'bar', function (err) { /* stuff */ });

memcached.incr Increment a given key.

memcached.incr('foo', 10, function (err) { /* stuff */ });

memcached.decr Decrement a given key.

memcached.decr('foo', 10, function (err) { /* stuff */ });

memcached.del Remove the key from memcached.

memcached.del('foo', function (err) { /* stuff */ });

memcached.version Retrieves the version number of your server.

memcached.flush Flushes the memcached server.

memcached.stats Retrieves stats from your memcached server.

memcached.settings Retrieves your stats settings.

memcached.slabs Retrieves stats slabs information.

memcached.items Retrieves stats items information.

memcached.cachedump Inspect cache, see examples for a detailed explanation.

memcached.end Closes all active memcached connections.

Private methods

The following methods are intended for private usage

.connect Fetches or generates a connection for the given server. The supplied callback function will receive a reference to the connection as argument. If there are issues with the server connection, we are going to respond with cache-miss pattern.

memcached.connect( '192.168.0.103:11211', function( err, conn ){
  if( err ) throw new Error( err );
  console.log( conn.server );
});

.multi A small wrapper function that makes it easier to query multiple Memcached servers. It will return the location for each key or the complete list of servers.

  1. server: String, The server location.
  2. key: String, The key associated with the server, if you didn't specify keys, this variable will be undefined.
  3. index: Number, The current index of the loop
  4. total: Number, The total amount server retrieved.
memcached.multi( false, function( server, key, index, totals ){
  if( err ) throw new Error( err );

  this.connect( server, function( err, conn ){
    console.log( "connection ready" )
  })
});

.command This is the core functionality of the memcached client. All public API's are routed through this function. It takes care of the argument validations Server retrieval ( If the server argument isn't specified ). After all data ready a connection is asked for the private connect method and the command is written to the Memcached server.

memcached.command({
  key: 'key', callback: function(){ console.dir( arguments ); },

  // validate the arguments
  validate: [[ 'key', String ], [ 'callback', Function ]],

  // used for the query
  type: 'delete',
  command: 'delete key'
});

.connectionIssue A internal function for logging issues with connections. As there can be various of ways that an error occurs we need solid issue manager to handle all these cases. For example server could crash or the Memcached server could respond with SERVER ERROR <broken>.

memcached.connectionIssue( "Server down", connectionReference );

Callbacks

Each method requires a callback function. Once this function get executed there will be 2 variables applied:

When we have a successful response, the context of the callback function will shift to a metaData object. The metaData object contains all information that we used to generate the request for the Memcached server. The metaData object contains the following properties:

And all the arguments you have send to the method, this depends on the method you have called.

Events

When connection issues occur we send out different notifications using the EventEmitter protocol. This can be useful for logging, notification and debugging purposes. Each event will receive details Object containing detailed information about the issues that occurred.

Details Object

The details Object contains the various of error messages that caused, the following 3 will always be present in all error events:

The following properties depend on the type of event that is send. If we are still in our retry phase the details will also contain:

If the server is dead these details will be added:

Events

There are 5 different events that the memcached client emits when connection issues occur.

Example implementations:

var memcached = new Memcached([ '192.168.0.102:11211', '192.168.0.103:11211' ]);
memcached.on('failure', function( details ){ sys.error( "Server " + details.server + "went down due to: " + details.messages.join( '' ) ) });
memcached.on('reconnecting', function( details ){ sys.debug( "Total downtime caused by server " + details.server + " :" + details.totalDownTime + "ms")});

Compatibility

For compatibility with other libmemcached clients they need to have the behavior ketama_weighted set to true and the hash set to the same as node-memcached's algorithm.

Due to client dependent type flags it is unlikely that any types other than string will work.

Test

You may encounter several problems when run the test. Be sure you already made these preparations:

  1. Start the memcached service. (If in Mac env, you can install it via homebrew, and brew services start memcached)
  2. Start 3 memcached servers at port 11211, 11212, 11213. (You first service will start at default port 11211, so you need to start 2 other servers manually. memcached -p 11212 -d, memcached -p 11213 -d)
  3. Run export MEMCACHED__HOST=localhost in your terminal. (This will make sure that the test case use localhost as your memcached server IP address other than it's default IP)

Contributors

This project wouldn't be possible without the hard work of our amazing contributors. See the contributors tab in Github for an up to date list of contributors.

Thanks for all your hard work on this project!

License

The driver is released under the MIT license. See the LICENSE for more information.