Yesterday one of my colleagues asked me about how to boost MongoDB performance.
I explained to him, and now it's worth sharing:

Performance tuning for #MongoDB :

1. Create custom indexes for your queries.
see: https://docs.mongodb.com/manual/indexes/ 

Thread 👇

#javascript #Nodejs
2. If you use mongoose always use "lean", by default mongoose queries return an instance of Mongoose Document class, and these are much heavier than vanilla javascript objects.

#javascript #Database #MongoDB #NoSQL
3. Reuse connections (Connect once, use multiple times):

Don't connect and disconnect DB every time you query something. You should connect once,
at the beginning of your app.

Setting up a new TCP Connection is expensive time-wise, memory, and network wise.
4. Run database operations in parallel. Use async/await or you can use promise.all() method if necessary, it all depend on your requirements.
5. When you have too many operations, use bulkwrite to reduce the number of req, and res between server and the DB.
6. Always Run Db in a separate instance with full access to the CPU, instead of running both Server and mongo in the same instance,

or, use managed services like Mongo Atlas, although it's cheap for small project or research purposes, but have to pay $$ to put in the production.
7. Always use projection operator to project in only the data you need, it saves some CPU usage and reduces bandwidth.

Anything else..? Please add.
You can follow @nuLL_Programmer.
Tip: mention @twtextapp on a Twitter thread with the keyword “unroll” to get a link to it.

Latest Threads Unrolled:

By continuing to use the site, you are consenting to the use of cookies as explained in our Cookie Policy to improve your experience.