express: Why express session is very slow?

Environment vm - centos7(4cpu, 8Gb memory, gigabit NAT)

I tested in the Express version 3 and 4. set as follows…

package.json

{
  "name": "apiauth",
  "version": "1.0.0",
  "description": "", 
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },  
  "author": "", 
  "license": "ISC",
  "dependencies": {
    "express": "4.13.3",
    "body-parser": "~1.13.3",
    "connect-redis": "~2.4.1",
    "cookie-parser": "~1.3.5",
    "cookie-session": "~1.2.0",
    "express-session": "~1.11.3",
    "redis-connection-pool": "1.1.0"
  }
}

A. no session api.js

var express = require('express');
var http = require('http');
var app = express();
var server = http.createServer(app);
var session = require('express-session');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
app.use(cookieParser());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

app.get('/test.json', function (req, res) {
    data = {a:[1,2,3,4,5,67,7,8,9,0,10]};
    res.send(data);
});

server.listen(8182, function() {
    console.log('Express server listening on port ' + server.address().port);
});

Image

B. session api.js

var express = require('express');
var http = require('http');
var app = express();
var server = http.createServer(app);
var session = require('express-session');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
app.use(cookieParser());
app.use(session({
    key: 'sid',
    secret: "test.kr",
    resave: false,
    saveUninitialized: true,
    cookie: {secure: false, maxAge: 60000, domain: '.test.kr'}
}));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

app.get('/test.json', function (req, res) {
    data = {a:[1,2,3,4,5,67,7,8,9,0,10]};
    res.send(data);
});

server.listen(8182, function() {
    console.log('Express server listening on port ' + server.address().port);
});

Image of Yaktocat Content download speed is extremely slow. Despite of the local environment.

How can I speed up?

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Comments: 27 (15 by maintainers)

Most upvoted comments

P.S. to disable Nagle in Node.js, add the following as your first middleware:

app.use(function(req,res,next){
  req.connection.setNoDelay(true);
  next();
});

To quote nodejs document, “noDelay defaults to true.”

Doesn’t that mean nagle is off by default?

That’s what I thought at first, but no, their documentation is just very confusing. What that is actually saying is that .noDelay() is the same as .noDelay(true) because the first argument to the .noDelay method is true by default if you don’t provide it; it has nothing to do with the initial state of the socket.

Please feel free to continue this discussion over on Node.js’s issue tracker, as I can only state the facts: Nagle is on by default and you can turn it off using socket.noDelay().

Most API calls should be stateless, and changing the order of your session initialization will speed up your stateless API calls:

// ...other app stuff...

// swap order of:
app.get('/test.json', function (req, res) {
    data = {a:[1,2,3,4,5,67,7,8,9,0,10]};
    res.send(data);
});

// having this below your api will mean that your API requests will be handled before this is added to the request...
app.use(session({
    key: 'sid',
    secret: "test.kr",
    resave: false,
    saveUninitialized: true,
    cookie: {secure: false, maxAge: 60000, domain: '.test.kr'}
}));

server.listen(8182, function() {
    console.log('Express server listening on port ' + server.address().port);
});