What is a good session store for a single-host Node.js production app?

后端 未结 8 1436
太阳男子
太阳男子 2020-11-29 15:38

I\'m using Node\'s Express w/ Connect middleware. Connect\'s memory session store isn\'t fit for production:

Warning: connection.session() Memo         


        
相关标签:
8条回答
  • 2020-11-29 16:05

    I would still use Redis even for local development. This is helpful because it stores the session even when you restart the Node application, keeping your browser session logged in. Redis by default saves the session in memory, same as connect's memory store is simple to configure (I just run it in screen along with my node apps) can support multiple applications if you just use a different database or session value in the configuration.

    0 讨论(0)
  • 2020-11-29 16:06

    Since the accepted answer is only connecting to remote hosts, it is obvious that it 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: https://github.com/mustafaakin/express-session-store-benchmark

    Concurrency: 1
    none       4484.86 [#/sec] 
    memory     2144.15 [#/sec] 
    redis      1891.96 [#/sec] 
    mongo      710.85 [#/sec] 
    Concurrency: 10
    none       5737.21 [#/sec] 
    memory     3336.45 [#/sec] 
    redis      3164.84 [#/sec] 
    mongo      1783.65 [#/sec] 
    Concurrency: 100
    none       5500.41 [#/sec] 
    memory     3274.33 [#/sec] 
    redis      3269.49 [#/sec] 
    mongo      2416.72 [#/sec] 
    Concurrency: 500
    none       5008.14 [#/sec] 
    memory     3137.93 [#/sec] 
    redis      3122.37 [#/sec] 
    mongo      2258.21 [#/sec] 
    

    The session used pages are very simple pages;

    app.get("/", function(req,res){
        if ( req.session && req.session.no){
            req.session.no = req.session.no + 1;
        } else {
            req.session.no = 1;
        }
        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'
    }));
    
    0 讨论(0)
提交回复
热议问题