Morrisda
Morrisda

Reputation: 1420

Storing session on running up or in database in Node

After some days of molesting people on stackoverflow i've succeded in creating sessions and manage them. But i am wondering.. where is recommended to store them? in a database (i'm using mongoDb), or on a running memory of the app (i store them in a var)?

Upvotes: 1

Views: 72

Answers (2)

Xinzz
Xinzz

Reputation: 2242

I would highly suggest using a session management module such as connect-mongo to store your sessions inside mongodb. This way if your server ever goes down, sessions will still be stored inside the database and will not be disrupted, whereas the sessions will be lost if you store them inside memory. It is also a more scalable solution if you have to maintain the same sessions across different servers.

If you are using Express, connect-mongo + Express.session() is the simplest way to represent sessions for your app.

Upvotes: 2

qubyte
qubyte

Reputation: 17748

Don't store them as variables in Node. At some point you'll need to scale your app with the cluster module, which gives each worker process its own memory. Whilst it is possible to communicate between workers, it's not a good idea both in terms of memory and code complexity.

Store your sessions in mongo if you're already using it. In the future, your worker processes can all then use this as a shared pool of memory for sessions.

Upvotes: 1

Related Questions