Thanks for sharing this post. You always share something interesting with us. I always visit your post to know some new facts. But right now, I am searching for the best essay writing website to help me in completing my assignment project. If anyone knows about it please let me know.
Here are a few best practices you can follow to optimise your MongoDB performance :
Use Indexes :
Queries on MongoDB work faster if there are indexed fields. You can use indexes to make data retrieval faster by reducing the number of disk I/Os.
By default the
_idfield is indexed. But you can index other fields as well. For example, if you sort your query results in the ascending/descending order ofdateAddedyou can create an index on this field. In Mongoose you will do something like this to create an index :Make sure you turn off
autoIndexin production mode :A separate data structure is maintained by MongoDB when you create an index. This consumes more memory. So, you need to be careful with indexes and avoid using them excessively.
Check out the docs to learn more about indexes.
Update Vs Retrieve & Update
Many times to do an update we retrieve a document, make the updates and then save the document. This can be slow in some cases. You can do faster updates by issuing updates to the modified fields only.
Read from Primary
If you have deployed a replica set, read from primary. Although the secondary instances update themselves quickly, the reads on secondary may not be consistent with primary instance due to network latency.
Storing Data for a record
Store all the data for a particular record in a single document. This speeds up retrieval because all the required data is retrieved in a single query. For example, in your post schema you can have embedded comments so that when you retrieve a post all the comments are automatically fetched.
But there are cases when this is not possible. For instance, you might have a
postscollection and each post is owned by an author. So, you need to link each post with an author fromauthorscollection. Here, you can't have an embedded author. So, you need to store therefin post document and make another query to populate it while retrieving the post. This can be done easily withpopulate()in Mongoose.Allocate sufficient RAM
MongoDB stores most frequently accessed documents in RAM in order to have faster retrieval. So, while deploying a MongoDB server make sure you have enough RAM. If possible deploy Redis and MongoDB servers separately in different machines with sufficient RAM.
Use shorter field names
As field names are repeated across documents, they consume memory. By keeping field names shorter you can save some memory and more documents will fit into the RAM.
These are some of the basic things you need to take care of to make MongoDB faster. Apart from this you can use
explainfeature to see how MongoDB executes your query, whether indexes are used or not etc. Also in my opinion if you have an option to choose between SSD and non-SSD cloud go for the SSD. This is because most of the disk I/Os are random in MongoDB and you will definitely have a performance boost by using SSDs.