Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Express/NodeJS + Mongoose App server response slow

Issue

I have an Express (Node.JS) + MongoDB app with a server response load time of 4 - 7 seconds on average (slow).

I understand that the average server response time is under 200ms as per google pagespeed tools.

This app is fetching data from a mongoDB asynchronously but the roundtrip times to the database is extremely slow with each call averaging about 500ms - 1s. These calls are simple findAll calls to retrieve data of less than < 100 records.

Context

  • Mongoose version: 4.13.14
  • DB server's MongoDB version is 3.4.16
  • DB server is hosted on MongoDB Atlas M10 in AWS / Oregon (us-west-1)
  • Web server is hosted with now.sh in SFO1 (us-west-1)
  • Have performed recommended indexes as advised by MongoDB Atlas's performance advisor
  • Data fetching perfectly fine in local environment (local server + local db) as data is queried in a matter of few ms
  • Mongoose logs for the affected page can be found in this gist

Mongo Server configuration

  • Mongo Atlas M10
  • 2GB Ram
  • 10 GB Storage
  • 100 IOPS
  • Encrypted
  • Auto-expand storage

Attempted solutions:

I have checked my DB metrics, they looked fine. There are also no slow queries. These are simple findAll queries. Performance advisor on mongo atlas reports nothing unusual.

The production application and database are both hosted in the same region.

I have already tried optimising the application layer of the query (mongoose) by running .lean()

Question:

Where else should i look to improve the database latency? How can a simple query take so long? Otherwise, why is my server response time taking up to 4s when the expected is about 200ms?

like image 749
clodal Avatar asked Aug 11 '18 17:08

clodal


People also ask

What is the difference between Mongoose and express?

Mongoose, by the way, is simply a layer of abstraction on top of MongoDB, the same way Express is a layer of abstraction on top of Node. We'll require Mongoose at the top of our JS file that starts the server and then invoke Mongoose's connect() method.

Why is NodeJS slow?

Node. js programs can be slow due to a CPU/IO-bound operation, such as a database query or slow API call. For most Node. js applications, data fetching is done via an API request and a response is returned.

What is difference between Mongoclient and Mongoose?

In terms of Node. js, mongodb is the native driver for interacting with a mongodb instance and mongoose is an Object modeling tool for MongoDB. mongoose is built on top of the mongodb driver to provide programmers with a way to model their data.


1 Answers

Hey you can try hosting your server and database in the same region. I think the network is creating a overhead in this case. If the server and the database are in the same region, They are on the same network which will reduce the latency significantly. there is a diagram on aws for this enter image description here

like image 171
Aditya_Anand Avatar answered Sep 24 '22 00:09

Aditya_Anand