Since the accepted answer is only connecting to remote hosts, it is obvious thatit will be always slower than localhost. Even if it is the next computer in your home, it would take milliseconds to read from that computer, but local memory takes only nanoseconds. You should compare them by using locally installed servers.
Here are my results from my local pc: You see, redis is almost as fast as in-memory in under high load. You can clone my the repo that these test codes are available:
node.js session store 有可选的四种方法的性能测试如下:
Concurrency: 1none 4484.86 [#/sec] memory 2144.15 [#/sec] redis 1891.96 [#/sec] mongo 710.85 [#/sec] Concurrency: 10none 5737.21 [#/sec] memory 3336.45 [#/sec] redis 3164.84 [#/sec] mongo 1783.65 [#/sec] Concurrency: 100none 5500.41 [#/sec] memory 3274.33 [#/sec] redis 3269.49 [#/sec] mongo 2416.72 [#/sec] Concurrency: 500none 5008.14 [#/sec] memory 3137.93 [#/sec] redis 3122.37 [#/sec] mongo 2258.21 [#/sec]
对比得出redis来存储在并发性越多的情况下性能优越。
The session used pages are very simple pages;
app.get("/", function(req,res){ if ( req.session && req.session.user_id){ req.session.no = req.session.user_id; } else { throw Error('error'); } res.send("No: " + req.session.no);});
Redis store config:
app.use(express.session({ store: new RedisStore({ host: 'localhost', port: 6379, db: 2, }), secret: 'hello'}));
Mongo store config:
app.use(express.cookieParser());app.use(express.session({ store: new MongoStore({ url: 'mongodb://localhost/test-session' }), secret: 'hello'}));
在使用mongodb存储时别忘记在加载一个模块:connect-mongo
npm install connect-mongo