Getting information from node.js session vs getting it from database Getting information from node.js session vs getting it from database mongodb mongodb

Getting information from node.js session vs getting it from database


Retrieving the session data requires a database query. If you would store the favorite color in the session, getting that preference would require that one query.

If you don't store the color in the session, you would need two database queries: one for the session, one for the user data that holds the color preference.

So if you know that during a session you will be needing the color information a lot, it would be better to copy that information into the session (although don't expect massive performance improvements, MongoDB and your OS will try and keep often-used database records in memory as much as possible).

A possible downside would be that in case of changes to preferences you need to update multiple documents (if the user changes their favorite color, you need to update both the session document and the user document in the database).