NodeJS and Memcached: Installation

So you have a worker process that should update a web process so it can notify all your connected clients of something?

We there are a couple strategies for doing this.

  1. Shared memory between processes.
  2. Pub/Sub and messaging service
  3. TCP socket between processes.

#1 is not so scalable. It works really quickly on your machine, but as soon as you put your application into a situation where you need to communicate between two servers, you can’t share memory.

#2 is great, something like redis or mongoDB works fine for this. You can easily store a message in this queue and pull down the message as you need it.

#3 is what we will work on today. It involves installing something called “memcached” which is a fancy term for a TCP connection between two process. As messages are published on the socket, they are pulled in and read by the process on the other end.

First off, my local environment is a mac, my production environment is an ubunty EC2 instance. To get it installed on the mac, make sure you have xcode installed with the developer tools, then run the script below:

# memcached requires libevent
cd /usr/local/src
curl -L -O http://cloud.github.com/downloads/libevent/libevent/libevent-2.0.17-stable.tar.gz
tar -xvzf libevent-2.0.17-stable.tar.gz
cd libevent-2.0.17-stable*
./configure
make
sudo make install

# Compile memcached utility
cd /usr/local/src
curl -L -O http://memcached.googlecode.com/files/memcached-1.4.13.tar.gz
tar -xvzf memcached-1.4.13.tar.gz
cd memcached-1.4.13*
./configure
make
sudo make install

# Create .bash_profile alias to start memcached as needed
alias m="memcached -d -m 24 -p 11211" 

# Install autoconfig
cd /usr/local/src
curl -L -O http://gnu.mirrors.hoobly.com/gnu/autoconf/autoconf-2.69.tar.gz
tar xzf autoconf-2.69.tar.gz
cd autoconf-2.69
./configure --prefix=/usr/local
make
sudo make install

# Compile and copy memcached.so module
cd /usr/local/src
curl -O http://pecl.php.net/get/memcache-2.2.7.tgz
tar -xvzf memcache-2.2.7.tgz
cd memcache-2.2.7
phpize
./configure --enable-memcache
make
sudo make install

# If using MAMP
# cp modules/memcache.so /Applications/MAMP/bin/php/php5.3.6/lib/php/extensions/no-debug-non-zts-20090626/
# emacs /Applications/MAMP/bin/php/php5.3.6/conf/php.ini # add line: extension=memcache.so

# If using Apache2
# sudo emacs /etc/php.ini # add line: extension=memcache.so

All the credit goes to this guy: https://gist.github.com/ryanjbonnell/3880048

Next you want to write a small script to test it. Set up two node processes and take this for example:

var Memcached = require('memcached');
var memcached = new Memcached('localhost:11211');
var lifetime = 86400; //24hrs
memcached.set('hello', 'world', lifetime, function( err, result ){
  if( err ) console.error( err );
  console.dir( result );
});
memcached.get('hello', function( err, result ){
  if( err ) console.error( err );
  console.dir( result );
});

All the credit goes to this guy: http://badsyntax.co/post/getting-started-with-the-nodejs-memcached-module

Finally, when you upload to the EC2 instance, you’ll need to install memcached there as well.

sudo apt-get install memcached
sudo vi /etc/memcached.conf
sudo /etc/init.d/memcached restart

And you should be good to go!

Leave a Reply

Your email address will not be published. Required fields are marked *