I want to create a database which stores the user data for a temporary period of time (i.e) Until the user logout.
Once the user logged out, I want all the details they are provided to get deleted.
My question is, can we use mongoDB for temporary storage. Until the user logout!
Or is there any other solution for this?
My answer is yes, you can use mongoDB for temporary storage. You can even set a TTL for the document so it will expire after an amount of time.
Here is the link for the documentation: https://docs.mongodb.com/manual/tutorial/expire-data/
Another solution would be to store that kind of information in a database that persists only in memory like Redis. That way you could gain some performance even.
You can use any database to do that. Just delete the data when the user logs out.
Redis may be a good fit for that because it stores the data in RAM, see:
http://redis.io/
But really any database can do it. You will just have to remove the data on logout.
Since you tagged your question with "mean-stack" then I assume that you're using Express. There are some modules that can help you with what you're trying to do. See the Compatible Session Stores in the express-session documentation.
Some of the more relevant modules from that list:
connect-mongo (for Mongo, as you asked)
connect-mongodb-session (another one for Mongo)
connect-redis (for Redis, which is well suited for that use case)
connect-sqlite3 (for SQLite, an embedded RDBMS that you don't have to install)
session-file-store (for storing session data in files)
or you can use the default in-memory session storage for testing (not suitable for production)
Related
I'm using node js along with mongo db. I need help for something related to caching. I have certain data like trending posts which will be updated for every 15 mins. But for a given instance of time period all the users making the api call for the trending posts data will get same response. Hence is there any way to save the cache data from db and update it periodically for 15 mins.
Note: I doubt if storing the data in json format in a file in server will be helpful? Is it prone to attacks or malicious usages? This data contains some confidential information too. So I guess that is not a good idea to store it in files. So is there anyother methods???
As mentioned by Kedar, this is where you would use some type of distributed cache like Redis.
There are NPM modules like mongodb-redis-cache
(https://www.npmjs.com/package/mongodb-redis-cache)
They syntax is quite simple, just append it onto the select. (it does require a redis server though, if you are not familiar with Redis or setting one up, I would look at a more managed solution then like on Digital ocean etc )
The idea here is that each call to Mongo will first check if there is a 'cached' key/value in redis, if not it runs the query, otherwise it will just pull from Redis.
I was going through this question: socket.io determine if a user is online or offline.
In the answer I have seen that an object containing the online users is created. In a production app should you store this data in a database like redis? Or is it okay if it stays saved in memory in the server?
I would not store the users in the server's memory, imagine this case:
for some reason you need to restart the server, a crash, a new version update, a new release and the memory of the server gets reset and you loose the users object.
So for this redis looks like a great option to store users data.
I build an API, which will send data to another API when has been collect 10 hashes. The client sends 1 hash per hour.
For example:
The client POST hash to API
API need to store it somewhere until the hashes number becomes to 10
When the number of hashes becomes 10 need to send data to another API and start from 0 again
My question related to the 2nd point. I can store the hashes in the array, the problem is that the data will be lost when the server will be shut down suddenly.
This is the only data which I need to store in API, so I don't want to use DBS.
By the way, it's my first time of developing API, so will be glad to your help.
Thanks in advance.
Sorry but your only options of storing data are either memory or disk.
If you store data in variables, you're using memory. It is fast and instant but it's not durable as you already said.
If you store data in database, you're using disk storage. It is slower but it is durable.
If you need durability, then database is your only option. Or maybe if you don't want to store the data in your machine, you could use cloud database such as firebase database.
Maybe your problem will be solved with Redis.
I had one feature where I needed to use some user's pieces of information on the server side in runtime and it could not be persisted at the database.
So, I used this.
In simple words, the redis will save the information in your cache and you can retrieve when you need.
There's no disk use and are more stable than a hand made memory control.
I hope this helps you.
I'm writing an auth module that contains several functions so my server can authenticate with an oAuth2 system using client_credentials. In the module I want to cache / save the credentials since they don't expire for some time (I'll refresh as needed).
Whats the best way to store the credentials?
Should I just create a var at the top of my node module? Should I create a class and instantiate it (const auth = new MyClass()) where my makes subsequent API calls (with the Bearer token)?
Creating a var variable means to store data in RAM, which is not a comprehensive approach.
I suggest you look at Redis, which was developed mostly for your purposes.
Redis is used as a database and for cache, since it’s super fast because the data is stored “in-memory” contrary to other databases in which the data is usually stored “on-disk.”
Moreover, there some thoughts about the fact that Node.js is less efficient at storing data than Redis. This article will clear more about my point.
So, in general, I guess using Redis will bring you more advantages in storing credentials.
I'm thinking about creating session this way:
create a secure token with cryto.randomBytes then store it in cookie.
extract token from cookie when node receive a new connection, store it inside global variable GLOBAL.SESSION[token] = data
I'm stuck on step 2:
What happend if node is crashed? Do I need to store the SESSION in a file like PHP does?
If I do it my way, in order to call SESSION, I need to globalize the token too. However, it's name will be a little long. If I shorten session name via assigning GLOBAL.SESSION[token] = GLOBAL.SESSION, it will be overwritten when node receive another connection.
Should I follow this way? Or Any further ideas about this?
create a secure token with cryto.randomBytes then store it in cookie
Just once? You risk session fixation attacks.
extract token from cookie when node receive a new connection, store it inside global variable GLOBAL.SESSION[token] = data
It's not great practice to keep things global... but that's up to you and your application structure.
What happend if node is crashed?
When your application stops running, everything you put into memory is freed. You are responsible for managing your data, and if you want it persistent, you have to make it persistent by writing to disk, a database, etc.
Should I follow this way?
No. Don't re-invent the wheel. You will inevitably make a security mistake along the way, and you are just creating more work for yourself.
Yes, you will need to store the session data into a persistent database, which could be anything like a flat file, SQL database, or noSQL db like mongo, couchdb, etc.
If you use node.js and express, there is a really good library called connect-session:
https://github.com/expressjs/session
you can readily use instead of reinventing the wheel.