I have been using the nestjs CacheModule along with cache-manager-redis-store as a cache store in an API. Recently I started clustering the app using node's cluster module. But now it seems like each cluster has it's own separate cache. Is it possible for the cache to be shared between all clusters? If not, what are other options for caching alongside clustering?
The workaround I used was ditching the nestjs CacheModule and connecting to the redis server directly using a redis client (node-redis).
I think the nestjs CacheModule is tied to the node process which makes each cluster have a different cache. But since the redis server exists outside node, it does not have the same constraints.
Related
Working on Redis Cache in a Node JS application and trying to have individual clients for read and write operations in Redis.
When using replicas, the main node is handling both Write and read and the other replica nodes are responsible for only reading by default. when using the primary endpoint, we could see the traffic is not equally shared among the nodes so would like to configure both the read and write node clients in the application for making the nodes work better.
Now I am having two nodes in Redis where one is responsible for read and the other is for write operations. I was trying to get if there is any configuration in createClient method where we can pass the read and write endpoint and When searching for the configurations I am not able to get any properties to pass in the createClient method.
Could anyone share, Is there any such configuration like that to specify to the Redis client to use read and write with different endpoint configurations when creating or we can achieve the same with any approaches?
Using Redis (node-redis) package from npm.
As of now trying to get any such configurations but not getting any proper way of handling the configurations. Most of them suggesting to go with manual checks for the commands and choosing the endpoint.
I am writing real time connection web server with WebSocket(ws) library in Node js(Express).so when I am running server on pm2 cluster mode than websocket not working properly.Is there any way to make share data between pm2 clusters with websocket.
From what I understand, that's kind of the point of cluster mode. It wouldn't be able to keep the connection alive across multiple "nodes" running processes. Straight from the PM2 docs:
Be sure your application is stateless meaning that no local data is stored in the process, for example sessions/websocket connections, session-memory and related. Use Redis, Mongo or other databases to share states between processes.
If you could pull out your websocket into its own instance and avoid side effects from a pm2 cluster--that's probably the most performant, although maybe not feasible in your set up. Best of luck!
You can switch the library to socket.io they already solve this issue by using redis adapter for socket.io Socket.IO Redis
By running socket.io with the socket.io-redis adapter you can run multiple socket.io instances in different processes or servers that can all broadcast and emit events to and from each other.
I'm trying to make server that helps http clients and net clients to connect between them, but I'm not sure that whether some module such as redis can help the server to share sessions or make connections from http to net.
Because I wanna use cluster (because of load balancing) to optimize speed in multi-core processors, I think the sessions must be shared.
Here is the server what I make
Using Redis is a good approach to save and share session details when using clusters. Read more on this approach here Nodejs Clustering and expressjs sessions.
Another approach would be to use Express Session module if your web application is developed using ExpressJS. This link https://medium.com/#karaxuna/how-to-share-session-across-mutiple-nodejs-http-server-instances-7d466389d123#.wrg84qel6 provides on how to configure Express Session module.
Can I use a redis cluster transparently (without writing any client side code for the redis cluster) with socket-io-redis instead of a single redis server as the default examples state? Unfortunately, there seems to be no official documentation on this.
Also, without a redis cluster, how many users can socket.io servers support with a single redis server as socket.io-redis backend?
Update:
Socket.io-redis allows specification of redis clients to use. May be some node_redis compatible redis client which also has support for redis clusters can be used? ioredis (the only other full featured/recommended client) has support for redis clusters but I am not sure if it's compatible with the node_redis client. Does anyone have some info on it or other possible solutions?
Note: I know Redis cloud provides scaling solution with a single endpoint paradigm but I need a cluster solution.
The module socket.io-ioredis allows use of ioredis with socket.io. Ioredis can connect to a redis cluster. However, I have, so far, only used socket.io-ioredis with socket.io with a single node server and single redis instance and it's working okay in this configuration.
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/