I'm developing an Express website that uses MongoDB as a backend and the connect-mongo middleware for sessions. Monitoring has shown that about 25% of the database time of my server is spent sending (mostly useless) updates to the MongoDB session store.
According to its README, it seems connect-mongo systematically updates the store (in my case MongoDB) on every request, but lets you optionally disable this behavior using the touchAfter
option :
If you are using express-session >= 1.10.0 and don't want to resave all the session on database every single time that the user refresh the page, you can lazy update the session, by limiting a period of time.
app.use(express.session({
secret: 'keyboard cat',
saveUninitialized: false, // don't create session until something stored
resave: false, //don't save session if unmodified
store: new mongoStore({
url: 'mongodb://localhost/test-app',
touchAfter: 24 * 3600 // time period in seconds
})
}));
by doing this, setting touchAfter: 24 * 3600 you are saying to the session be updated only one time in a period of 24 hours, does not matter how many request's are made (with the exception of those that change something on the session data)
What I do not understand is why one would want another behavior than this. Why update a session which data has not changed?