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: 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.

• • •

Missing some Tweet in this thread? You can try to force a refresh
 

Keep Current with Saurabh

Saurabh Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Too expensive? Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal Become our Patreon

Thank you for your support!

Follow Us on Twitter!