Monthly Archives: February 2014

Starting out with Node.js

Here is an excellent guide for starting out with Node.js, the Express framework, and MongoDB: click me.

So what more is there for me to add? Well, here is some of my code and folder structure so you have a better idea of how to generalize this for web development.

Here are some differences that I introduce:

  • The article above uses an outdated method of connecting to MongoDB, so mine shows you a different way using the MongoClient class.
  • The example in the article above shows you a quick way to create REST services. You may also want to serve normal web pages, so that’s included below using the Express framework
  • I also include a way to exit the server gracefully (ie. a hook to release resources).

So here is a snapshot of my folder structure so you know what to aim for:

Screen Shot 2014-02-21 at 2.23.50 PM

Here’s the content of server.js (note the serving of normal web pages):

var port=3000;
var express = require('express');
var hand = require('./routes/hand');

var app = express();

/*********************************
 * Serve all pages under /html
 *********************************/
app.use(express.static(__dirname+'/html'));

/*************************
 * Route REST calls
 ************************/
app.get('/hand/:id', hand.getHandById);

/*************************
 * Start Server
 ************************/
app.listen(port);
console.log('Listening on port '+port);

Here’s the content of my hand.js

var mongo = require('mongodb');

var DB_NAME = 'myMongoDb';

var MongoClient = mongo.MongoClient;
var Server = mongo.Server;
var BSON = mongo.BSONPure;
var mongoClient = null;

/******************
 * Ensure proper shutdown when exiting
 ******************/
process.on('SIGINT', function() { process.exit(); });
process.on('exit', function shutdown() {
	console.log("shutting down...");
	if( mongoClient ) {
		console.log("close mongo");
		mongoClient.close();
	}
});

/*******************
 * Mongo Setup
 *******************/
mongoClient = new MongoClient(new Server('localhost', 27017, {auto_reconnect: true}));
mongoClient.open(function(err, mongoClient) {
	if( err ) {
		console.log(err);
		process.exit(1);
	}
});

var db = mongoClient.db(DB_NAME);
var handCollection = db.collection('hand');

/*******************
 * REST API
 *******************/
exports.getHandById = function(req, res) {
	var id = new BSON.ObjectID(req.params.id);
	handCollection.findOne({'_id':id}, function(err, item) {
		console.log('getHandById{'+id+'}');
		res.send(item);
	});
};

Advertisements
Tagged , ,

Your Mysql on AWS may be running 20x slower!

speeduppc

No, this is not a Spam Ad trying to get you to click on something to speed up your machine. You should take this seriously and look into it if you have a mysql instance on AWS.

Recently, I created a regular ubuntu12 EC2 m1.large instance on amazon. Then I used the standard apt-get tool to install mysql. This installed mysql 5.5 with all the defaults.

I ran a small import of about 5k records into an innodb table. This took about 10s on my macbook pro (with a 7200rpm HD, no solid state HD). I ran the same test on my EC2 instance and noticed it took 100s!!!

I tried adding and modifying a bunch of parameters in the my.cnf (mysql’s config file, probably located in /etc/mysql). Nothing really made much of a difference except this:

innodb_flush_log_at_trx_commit

The default value is 1, which guarantees ACID compliance. But if you can live with a possible 1 second lost of data in case of a catastrophic event, then setting it to 0 or 2 will speed it up quite a bit. I set mine to 2.
You can read more about it here

Now, when I import the 5k records, it takes about 5s. That’s a 20X increase in speed.

So to review, this will affect you if the following apply:

  1. You launched an Ubuntu EC2 instance from AWS
  2. You installed mysql using apt-get
  3. You are using InnoDB tables
  4. You want greater WRITE performance (the change above does not affect READs)

 

Tagged ,