Memcache v/s redis for maintaining persistent sessions? - node.js

I want to make persistent sessions on server i am using node.js with express and for that first i read about connect-redis enter link description here and connect-mongo enter link description here i read that redis is faster then mongo that's why i decided to use it but now i also find a module named memcached enter link description here i dont know which will be better for my project, also in mamcache is data stored in memory or where because if it is memory then it must be fastest.

If you have already setup Redis then I would stick with it as it is very fast and easy to manage. MemCached and Redis are very similar when used for caching however the key difference is that Redis can be set to persist to disk in the background meaning that if the server goes down the data in memory can be reloaded.
Personally, I would not use MongoDb for session persistence for speed reasons however if I was using MemCached I'd possibly use it as a backup for the sessions. e.g. Write session data to MemCached and Mongo but only read from MemCached and use Mongo to restore is an error occurs.
Bottom line, I think your choice to use Redis is the best one for what you've described

Related

Can I use only redis and node?

I've already looked on the internet and it only gave me doubts what architecture to use.
I am thinking of using:
BackEnd:
Nodejs, Mongo, Redis.
FrontEnd:
You travel
Because it is a chat application with the possibility of scaling horizontally.
The big question is do I have to use Mongo?
If not, use Redis, it is safe to treat with user.
Or leave the Mongo for user, and Redis for msg.
I thank anyone who has worked with something like this or knows the subject more than I ... Grateful
In my opinion, you can just use mongodb or redis or mysql and so on.
On the whole:
MongoDB: a document database
MySQL: a relational database
Redis: a in-memory data structure store. Of course, you can use redis as a database, but more people use it as cache to speed up request processing
So, I think you can use mongo or mysql as base storage. If your data too big, you can you add redis when your want to speed up request

Is there any solution to prevent the express sessions from drop every time you perform server restart without using RedisStore?

I can perform other operations on dashbord even server restart without reload the webpage
I am guessing you are not using any kind of persistence storage for your sessions. In that case the sessions are stored in memory of the express app. Hence a restart would drop all the sessions. You either need a cache like Redis or even postgres to persist your sessions.

Set cookie on browser with node js

I'm quite new to node js and I've encountered a problem with cookies on my client side browser.
So to resume, I am trying to set cookie to a page (which is not yet opened) with node http server, my goal is that I set cookie to this page, then open this URL with node (opn module) and then this page access cookie with document.cookie.
Also, the URL I'm opening is hosted on the node server (localhost/mail/sendingMail/index.html) on port 32126.
I've tried a lot of module and request but couldn't find one that did the job, is this possible and if so, how?
If this isn't possible any way of sending data to this page would be nice!
i think you have to use redis for this i also use redis and it is veryfast because it store data on RAM.
in redis you can store objects or hashes or arrays and it is very easy to use with node by installing redis-npm
this is the best dock which i prefer and describe how to use redis with node but remember you have to install redis first .
about redis :-
Redis is an open source (BSD licensed), in-memory data structure store, used as a database, cache and message broker. It supports data structures such as strings, hashes, lists, sets, sorted sets with range queries, bitmaps, hyperloglogs and geospatial indexes with radius queries. Redis has built-in replication, Lua scripting, LRU eviction, transactions and different levels of on-disk persistence, and provides high availability via Redis Sentinel and automatic partitioning with Redis Cluster.

Nodejs express interchangeable session stores

Is there a way to switch session stores on the fly with express?
Currently depending on default config my express app is using either redis or mongo to store sessions.
So my question: is there a way to switch session from using redis to mongo on the fly? In case if redis goes down.
I tried calling app.use(express_session({....})) again but it doest work. I think there must me some hook that will allow me to delete current session store and create|add new one. Not sure where though.
I don't think there's a way to "un-use" a mounted middleware after the app has started, but what you could do is write your own session store (a pretty simple api to follow) that handles switching between the two (or more) stores.

Store sessions on disk

I can't setup redis server because i'm on windows.
How can I store the sessions on disk so they will persist through node restarts?
Also, do I have to restart node everytime I modify a JS file for the changes to go through?
Btw, I'm already using express for node. Express uses the memorystore which means that sessions reset everytime node restarts.
There are multiple solutions:
https://github.com/antirez/redis/issues/238 < actually Redis got a patch so it can be built on Windows, may not be perfect but works
Make an account on https://redistogo.com/ , they provide a free database of 5Mb (which is ok if you just want to test out some things)
You can use something like connect-cookie-session, so that you store the session into the cookie (this is ok if you are just developing stuff and need to have durable sessions, and then use Redis into production)
Also, do I have to restart node everytime I modify a JS file for the changes to go through?
There are dedicated modules for that, one of the most popular being node-supervisor. Read the docs on their official page, it's really easy to use.
How can I store the sessions on disk so they will persist through node
restarts?
To be honest I have only used redis as my session-store, but you could also try to use(also found using http://search.npmjs.org:
mongodb as your session store.
supermarket-cart which I believe uses supermarket under the covers.
connect-fs: https://www.npmjs.com/search?q=connect-fs
connect-mysql-session: A MySQL session store for node.js connect.
connect-cookie-session: Connect middleware to allow you to store your sessions directly in the client's cookie.
Also, do I have to restart node everytime I modify a JS file for the
changes to go through?
Will answer this later!
You can use a cloud-based redis like http://redis4you.com/

Resources