1

My first Stackoverflow question: I have a webshop where users can search for products. I am using Angular, NodeJS (express) and MongoDB. Infrastructure is this: Front-end on some norwegian hosting service. Backend Heroku (server.js) MongoDB on MongoDB.com

The webpage only does read operations towards the database, there is no write, delete, modify operations, and the user does not have to sign in. What I have noticed is that when the user opens the page for the first time, the first query take some time, around 4sec (connection is being established, I checked in Postman, times goes to "Transfer Start"). After that everything runs smood (response time less than 1 sec.). I also see that there is a new connection for each different user. Since the backend is running on heroku, connecting to mongodb with a specific user (in connection string), my assumtion is that it should be possible to re-use this connection for all users of the webpage, by keeping the connection alive. Is my assumption right? The reason for the current infrastructure / setup is that I wanted to learn some new stuff while creating the page.

Summary: Webpage -> Api call -> Heroku (server.js) -> query (Mongo.com)

FauxVice
  • 11
  • 1
  • Hi, your assumption on being able to reuse an existing connection is correct. If you would like help troubleshooting why every request creates a new db connection, please provide a [Minimal Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) of your code. – zishone Jun 07 '20 at 13:48
  • Yes, your assumption is right. There are several ways you can go about this. Here is a good post on the very subject. https://stackoverflow.com/questions/10656574/how-do-i-manage-mongodb-connections-in-a-node-js-web-application/62169792#62169792 – Hoppo Jun 07 '20 at 14:00
  • Are you using free heroku dynos? – D. SM Jun 07 '20 at 16:16
  • Yes, i am using free Heroku. Thanks for your feedback guys, I’ll post my server js code tomorrow:) – FauxVice Jun 07 '20 at 19:10
  • @D.SM You are right. The problem was that I was using free Heroku dyno. That is the main reason for the delay. The app goes to sleep if not active. That causes a delay 4-10 seconds when waking up. – FauxVice Jun 09 '20 at 12:19

0 Answers0