Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NodeJS, Express, & Mongoose: Calls are intermittantly slow

I have a really simple nodeJS app. For some reason, the response times of the server vary greatly.

Here's my app.js:

var express = require('express');
var http = require('http');
var path = require('path');
var Models = require('./schema/schema.js');

var app = express();

// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.json());
app.use(express.urlencoded());
app.use(express.static(path.join(__dirname, 'public')));

app.use(function(req, res, next) {
    var start = Date.now();

    res.on('header', function() {
        var duration = Date.now() - start;
        var time = Date.now() - req.start;

        fs.appendFile("times.txt", time+"\n", function (err) {});

    });
    next();
});
app.use(app.router);



app.get("/tags", function(req, res) {
    var query = Models.Tag.find({}).sort({'popularity': -1}).limit(200);
    query.exec(function(err, tags) {
        res.status(200);
        res.send(tags);
    });
}

The data is always returned correctly, but here are my response times, as measured by the 'header' function:

19
11
13
6
10
10
8
9
2
62449
57862
24919
9975
11
17
21116
10
3
2
2
13

Most are less than 10 ms, but frequently there are some more than a minute. What is going on?

Schema:

var tagSchema = new mongoose.Schema({
    name : {
        type: String,
        trim: true
    },
    popularity :{
        type:Number, default:0
    },
    created_date: {
        type:Date, default:Date.now
    },
    last_update: {
        type:Date, default:Date.now
    }
});
like image 503
Jason Avatar asked Apr 02 '14 22:04

Jason


1 Answers

Have you tried using a hosted mongo server, like mongohq, just to see if it's a local IO latency issue? A database that small shouldn't have ANY problems, unless you're doing something crazy, which you're clearly not.

like image 192
sent1nel Avatar answered Oct 05 '22 11:10

sent1nel