Using sequelize with multiple instances of the same db - node.js

I would like sequelize to pool connections for multiple db hosts (postgres) so that if one crashes, my application stays up. In other words, I have multiple instances of the same db installed on different hosts (guaranteed to be in sync). I would like sequelize to pool connections to both dbs. Is this possible with sequelize directly? If not, what is the easiest way of doing this?

You need to set up a Postgres cluster and then connect to that from Sequelize. The configuration is done on the server side and will be transparent to the client.
https://wiki.postgresql.org/wiki/Replication,_Clustering,_and_Connection_Pooling
The connection pooling for Sequelize is defined in the connection, look for options.pool in the docs.
http://docs.sequelizejs.com/class/lib/sequelize.js~Sequelize.html

Related

Connection Pooling in typeorm with postgressql

I've gone through enough articles and typeorm official documentation on setting up connection pooling with typeorm and postgressql but couldn't find a solution.
All the articles, I've seen so far explains about adding the max/Poolsize attribute in orm configuration or connection pooling but this is not setting up a pool of idle connections in the database.
When I verify pg_stat_activity table after the application bootstraps, I could not see any idle connections in the DB but when a request is sent to the application I could see an active connection to the DB
The max/poolSize attribute defined under the extras in the orm configuration merely acts as the max number of connections that can be opened from the application to the db concurrently.
What I'm expecting is that during the bootstrap, the application opens a predefined number of connections with the database and keep it in idle state. When a request comes into the application one of the idle connection is picked up and the request is served.
Can anyone provide your insights on how to have this configuration defined with typeorm and postgresql?
TypeORM uses node-postgres which has built in pg-pool and doesn't have that kind of option, as far as I can tell. It supports a max, and as your app needs more connections it will create them, so if you want to pre-warm it, or maybe load/stress test it, and see those additional connections you'll need to write some code that kicks off a bunch of async queries/inserts.
I think I understand what you're looking for as I used to do enterprise Java, and connection pools in things like glassfish and jboss have more options where you can keep hot unused connections in the pool. There are no such options in TypeORM/node-postgres though.

How to configure Mongoose with an already existing Mongo connection

I have an app that is already working with the native Node Mongo driver (v3.0).
I'm now trying to slowly implement Mongoose in order to make the app easier to maintain. I would like to do this in a gradual way so I rewrote all the user related operations with Mongoose and the rest like it was before. I noticed that my app now creates two connections to my Mongo db. This is clearly because Mongoose knows nothing about my existing connection.
I would like to handle connecting and disconnecting to Mongo myself and give Mongoose a reference to the already existing connection but I can't find anything like this in the docs.
Is this even possible or will I need two different connections until my app is fully rewritten to use Mongoose exclusively?
EDIT: My app is being run as an AWS Lambda function which has to connect and disconnect to mongo on every request so having two concurrent connections per request is effectively halving my mongo db available connections. That’s why I’m concerned about having an extra connection.
Turns out the answer to this is to do it the other way around. Just connect to Mongoose and then grab the connection.
let mongoConnection = mongoose.connection.client

Micro Services Architecture if using Sequelize as an ORM

I'm developing a chat application that consists of several micro-services or nodes. And to handle database I use Sequelize ORM.
However, I've two separate nodes one for handling socket messages and another one is a general API server. Both of them have to use Sequelize.
How can I use Sequelize in both? I don't want the same code to copy paste in two different services.
What we have done in our project, is to connect socket server and API server. Thus API server is a special socket client for the socket server. Thus if we need to do some db query we pass data to-fro between them.

is it safe switching db on mongoose?

in my application I have a default database and other database I have to connect to in function of client's requests , since with mongoose in node as far as I understood: there is a pool of connections application wide, if I change database, it is changed for all the subsequent requests, I think it could cause some problems, what is the best way to switch Database with mongoose?
Mongoose 3.7.1 (unstable) supports switching databases.
Otherwise you'll need to create separate connection instances for each database.

Sharing a DB connection on the entire app or connect to DB on each request?

I'm doing an API with Nodejs using Restify.
For the DB I'm using Mongodb (with mongoose).
I was wondering, what the best solution between sharing a db connection to my entire app or connecting to the db on each request ?
For now, I'm using the second option of this answer : sharing db connection
But I've seen a different pattern here : Node.js Web Application with Storage on MongoDB
I can't figure out, what is the best architecture ?
A list of pros and cons could be a great help.
Of course keeping one connection ( or pool of connections if mongoose supports it ) and reusing it is better, simply because creating connection on each request eats resources.

Resources