0
votes

I am currently developing a Node.js app. It has a mySql database server which I use to store all of the data in the app. However, I find myself storing a lot of data that pertains to the User in session storage. How I have been doing this is by using express-session to store the contents of my User class however these User classes can be quite large. I was thinking about writing a middleware that will save the User class as JSON to either redis or mongodb and store the key to the storage within the session cookie. When I retrieve the JSON from redis or mongodb, I will then parse it and use it to reconstruct my User class.

My question is which method would be the fastest performing and also scalable: storing JSON strings in Redis, or storing a mongo document representation of my User class in MongoDB? Thanks!

EDIT: I am planning to include mongoDB in another part of the app, solving a different issue. Also will the JSON parsing from redis be more time-consuming and memory intensive than parsing from mongo? At what reoccuring user count would server memory sessions become a problem?

2
@robertklep Correct i am using express-session. I want to know what is the most efficient performance wise. Obviously using express-sessions in memory storage will not be scalable though its working fine right now.Tyler Oliver
I removed my comment, will write an answer instead.robertklep

2 Answers

3
votes

express-session has various session store options to save the session data to.

AFAIK, these all work through the same principle: they serialize the session object to a JSON string, and store that string in the store (using the session id as the key).

In other words, your idea of storing the user data as a JSON string in either Redis or MongoDB using a second key is basically exactly the same as what express-session does when using the Redis or MongoDB stores. So I wouldn't expect any performance benefits from that.

Another option would be to store the user data as a proper MongoDB document (not serialized to a JSON string). Under the hood this would still require (de)serialization, although from and to BSON, not JSON. I have never benchmarked which of those two is faster, but I'm gonna guess and say that JSON might be a tad quicker.

There's also a difference between Redis and MongoDB, in that Redis is primarily in-memory and more lightweight. However, MongoDB is more of a "real" database that allows for more elaborate queries and has more options in terms of scalability.

Since it seems to me that you're only storing transient data in your sessions (as the actual data is stored on MySQL), I would suggest the following:

  • use the Redis session store if the total amount of data you're storing in the sessions will fit in memory;
  • use the MongoDB session store if not.
0
votes

tj/connect-redis in conjunction with express-session does the job well! Redis is incredibly fast with JSON and is superb for handling sessions.