Performance is one of the most crucial aspects to take into account while developing web applications. If your application is too slow, users will get impatient and might even stop using it. I will outline the measures I took to 10x the speed of an API endpoint for an e-commerce website in this article.
This specific API endpoint was in charge of creating a report by gathering information from several sources, integrating it, and running computations on it. To acquire the essential information, communication with numerous modules or services was required.
Determine the system bottlenecks:
I started by assessing the present performance of my API endpoint, comparing it to industry norms, determining the areas that needed improvement, and setting precise performance objectives. I also tried to improve the API endpoint by locating the areas of my code that are the slowest to execute. The execution timings of various method calls can be manually recorded, or performance profiling tools like the Node.js debug module or performance API can be used. Some of the bottlenecks in my situation were:
Utilizing a loop to access the database.
Many instances of a complex calculation being run with the same inputs.
Making repeated calls to a third-party service/API
Reduce database access frequency:
Database searches are usually a performance constraint in API endpoints. I reviewed my database queries and made them more effective. I achieved this in my case by using database indexes, conducting fewer searches, eliminating needless joins or subqueries, and optimizing database schema design. Here are some of the strategies I found useful and I think you might find useful too:
If you can aggregate the data into one call rather than making many calls to the same service or database, do so.
If feasible, use a query that causes the database to produce numerous results sets.
Always call the database outside of a loop when working with one.
Here's an illustration of how to use Node.js and the mysql2
package to fetch several results sets from a database:
const mysql = require("mysql2");
const connection = mysql.createConnection({
host: "localhost",
user: "root",
password: "password",
database: "mydb"
});
connection.query(
"SELECT * FROM products; SELECT * FROM orders",
(err, results) => {
if (err) throw err;
console.log(results[0]); // products
console.log(results[1]); // orders
}
);
connection.end();
Implement caching Techniques:
The performance of APIs can be increased through caching. I implemented caching techniques to cache frequently requested data and lessen the need for repeated calculations or database queries, such as in-memory caching, content delivery network (CDN) caching, or database caching. However, it's crucial to take into account how recent the data is and how to clear the cache when the underlying data changes. For this I used node-cache
library in nodejs
const NodeCache = require("node-cache");
const myCache = new NodeCache();
myCache.set("key", "value", 10 /*ttl in seconds*/);
const value = myCache.get("key");
Implement asynchronous processing:
By enabling many operations to be carried out simultaneously, asynchronous processing can dramatically enhance API performance. For time-consuming tasks, such as I/O operations or requests to external APIs, I delegated to different threads or processes using asynchronous programming techniques like async/await or callbacks.
Monitor and evaluate performance continuously:
Last but not least, it's critical to regularly test and track the performance of your API endpoint to spot any new bottlenecks and make any necessary adjustments. Monitoring technologies like New Relic, Prometheus, Grafana, or other tools of a similar nature can be used for this.
In conclusion, optimizing an API endpoint is a continuous process that requires a systematic approach and a thorough understanding of the different techniques and best practices.
You may greatly enhance the performance of your API endpoint and offer a better user experience by following some of the methods I used, Remember that performance optimization is a continuous process that needs to be monitored and updated frequently to account for evolving needs and technologies.