Make Your App Faster
Note: This content originally appeared on https://kadira.io/academy/meteor-performance-101 .
This guide will help you to speed up your methods and publications by following a few simple techniques. First let’s try to speed up methods; later on I’ll show you how to apply these techniques for publications as well.
First you need to sort your methods by throughput and optimize methods from top to bottom. This way you add more focus to the methods utilized most by users, which will save your resources since you’ll be optimizing what your users care about.
Click on a method and you’ll see the Response Time graph. If that’s above 500ms, you might need to worry about that method. Click on the Response Time graph and analyze some traces to see what’s really happening inside your app.
Then you can use the following guides to optimize if some parts of your traces are slower. Add necessary indexes
Normally, your database queries should take no more than 300 ms. If they take more than that, you’ll likely need an index for your query. You should add an index (or allocate an index) for all the queries you use, especially for methods with high throughput. Otherwise you’ll have a hard time when your database is larger and you have plenty of users using your app.
MongoDB stores your data on the hard disk, without any order and in some random places. Now, let’s say you have 1,000 documents and you need to find a single document. MongoDB needs to start from the first document and traverse until it finds the document you are looking for. This is inefficient, especially if you have a large number of documents or many requests.
An index is a better way to find documents. It is a sorted list that maintains a pointer to the specific document in the database. Index is also stored on the hard disk, but it will be loaded into the memory when it will be used. Since the index is sorted, MongoDB can find your document quickly. This comes at a cost—you need to create indexes for all queries to find them faster. However, there are some ways to use a single index for multiple queries.
Learning how to use an index is a big topic and needs to be taught correctly. Refer to the following MongoDB documentation, which is really useful:
You can also watch the following videos extracted from MongoDB for DBA course.
All methods interacting with third party services take a considerable amount of time to complete. Using these calls in a method will cause two main issues:
- Other methods from the same client will have to wait for the completion of the current method.
- They will slow down the method itself.
You can simply use
this.unblock() to ask Meteor not to wait on this method. Sometimes it is not wise to use
this.unblock(). Refer our article on Managing WaitTime to learn more about
For emails, you also can use
Meteor.defer, as shown below:
With this, your method does not include the time used to send the email. This is completely okay, since adding a new post does not need to wait for sending emails, as it is a background operation. That’s what
Sometimes you’ll receive a lot of data for the method and do some calculations inside it. This will increase the Response Time of your app as well as the CPU usage. MongoDB aggregations is the best and most efficient option. Let’s say you need to count the number of posts by each category. This is how you can do it with aggregations:
Install the following package.
meteor add meteorhacks:aggregate
Use this code.
var result = Posts.aggregate([
With traces, you can find out the wait time and the methods and subscriptions for the current method.
Find those methods and subscriptions and reduce their Response Time by applying the above techniques.
You can follow the same process above for publications as well. However, instead of sorting with throughput, you need to sort with SubRate. You also can’t use
this.unblock inside publications by default.
But we’ve a solution for that.