So we have a 4 servers (all ubuntu)... one for client, one for db, two for microservices. Currently we have 8 microservices running on the 2 servers (one server - 4 services and other server - 4 services). We have stored the session of the user in mongodb which runs in a separate server. We have implemented a dedicated authentication micro service. It is running along with another route in the same micro service. The mongodb is installed as cli in the database server and the connection is made to store the session. Each microservice has authentication middleware. So when a request comes in a microservice the auth middleware is run and we are checking if the session is present in the db or not.
Note that we are not using the express-session to store the sessions. We are manually storing the required user details and mongo automatically generates an id for the insertion that is used as session id by us.
So when the user logs in the session is created in the micro service which has the auth endpoint. But when the user makes a request to another microservice, a new mongo connection is made via mongoose and the auth middleware in that service does have the session generated by the first microservice since that was a different connection. After the request is made to the second microservice, when I log the list of collections via mongoose I am able to see two Session collections in my console.log()
Now that you have an understanding of my situation, how to solve this problem?
How can I share the mongodb connection across microservices running in separate servers and the mongodb running in a third server?