I'm using Node's Express w/ Connect middleware. Connect's memory session store isn't fit for production:
Warning: connection.session() MemoryStore is not designed for a production environment, as it will leak memory, and obviously only work within a single process.
For larger deployments, mongo or redis makes sense.
But what is a good solution for a single-host app in production?
On the other hand, the session data is stored on the server-side, i.e., a database or a session store. Hence, it can accommodate larger amounts of data. To access data from the server-side, a session is authenticated with a secret key or a session id that we get from the cookie on every request.
Here, since sess is global, the session won't work for multiple users as the server will create the same session for all the users. This can be solved by using what is called a session store. We have to store every session in the store so that each one will belong to only a single user.
Most frameworks use their own session management middleware. For example, express , the most popular server framework for Node. js, has the accompanying express-session for session management.
Session handling is a globally used concept, without which any website or app, regardless of whether it is written in Node.js, PHP or any other backend language, will be in a precarious condition. It enables the user information to be persistent in a ‘stateful’ manner across all the pages of that website or app.
This is a Node.js module available through the npm registry. Installation is done using the npm install command: Create a session middleware with the given options. Note Session data is not saved in the cookie itself, just the session ID. Session data is stored server-side.
sessionstore A session store that works with various databases. tch-nedb-session A file system session store based on NeDB. A simple example using express-session to store page views for a user.
Upon logging out, the session is destroyed and the user will be redirected to the home page, where he or she can log in again. Create a new folder and open a terminal in that directory. Note: Here, we are using the Windows command line tool. This creates the package.json file for building the Node project.
Spent the day looking into this. Here are the options I've discovered. Requests/second are performed via ab -n 100000 -c 1 http://127.0.0.1:9778/
on my local machine.
maxAge
)ttl
)clear_interval
to be set to cleanup sessionsHere is the coffeescript I used for cookieSession:
server.use express.cookieSession({ secret: appConfig.site.salt cookie: maxAge: 1000*60*60 })
Here is the coffeescript I use for redis:
RedisSessionStore ?= require('connect-redis')(express) redisSessionStore ?= new RedisSessionStore( host: appConfig.databaseRedis.host port: appConfig.databaseRedis.port db: appConfig.databaseRedis.username pass: appConfig.databaseRedis.password no_ready_check: true ttl: 60*60 # hour ) server.use express.session({ secret: appConfig.site.salt cookie: maxAge: 1000*60*60 store: redisSessionStore })
Here is my coffeescript for mongo:
server.use express.session({ secret: appConfig.site.salt cookie: maxAge: 100*60*60 store: new MongoSessionStore({ db: appConfig.database.name host: appConfig.database.host port: appConfig.database.port username: appConfig.database.username password: appConfig.database.password auto_reconnect: appConfig.database.serverOptions.auto_reconnect clear_interval: 60*60 # hour }) })
Now of course, the remote redis and mongo databases will be slower than their local equivalents. I just couldn't get the local equivalents working, especially considering the installation and maintenance time for me was far more than what I was willing to invest when compared with hosted remote alternatives, something I feel is true for others too hence why these hosted remote database services exist in the first place!
For local database benhmarks, see @Mustafa's answer.
Happy for someone to edit this answer to add their local database benchmarks to the mix.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With