Node.js Memcached

Make Node.js Memcached super easy with Redis Labs

Redis Labs’ products and services enable running Memcached buckets in a highly available and auto-scalable manner, with predictable top performance.

The Redis Labs Enterprise Cluster (RLEC) lets you install an enterprise grade Memcached cluster in your environment of choice, whether an on-premises data-center or your preferred cloud platform. It gives you full control of your data and configuration – no clustering or sharding knowledge required!

Memcached Cloud is a fully managed cloud service for hosting and running Memcached datasets in a highly available and scalable manner, with predictable and stable top performance. It provides a storage engine for standard Memcached, as well as in-memory replication and instant auto-failover within the same data center or across data centers. The service completely frees developers from dealing with nodes, clusters, scaling, data persistence issues or failure recovery.

Using Memcached with Node.js

In order to use Memcached with Node.js you will need a Node.js Memcached client. In the following sections, we will demonstrate the use of MemJS, a Memcache client for Node.js using the binary protocol and SASL authentication.

Installing MemJS

MemJS’ installation instructions are given in the “Installation” section of its README file. To install, use npm as follows:

$ npm install memjs

Opening a Connection to Memcached Using MemJS

The following code creates a connection to Memcached using MemJS:

var memjs = require('memjs');

var mc = memjs.Client.create('hostname:port', {
  username: 'username',
  password: 'password'

To adapt this example to your code, make sure that you replace the following values with those of your bucket:

  • In line 3, the first argument should be your bucket’s endpoint
  • In line 4, username should be set with your bucket’s username
  • In line 5, password should be set with your bucket’s password

Reading and Writing Data with MemJS

Once connected to Memcached, you can start reading and writing data. The following code snippet writes the value bar to the Memcached key foo, reads it back, and prints it:

// open a connection to Memcached

mc.set('foo', 'bar');
mc.get('foo', function (err, value, key) {
    if (value != null) {

The output of the above code should be:

$ node example_memjs.js

For more information on using Redis Labs’ products and services with node-js please see the Howto page.