How To Make Your Node.js API 5x Faster With Platformatic

How To Make Your Node.js API 5x Faster With Platformatic

As a backend developer, building robust, secure and fast APIs is critical to success.

Fast APIs lead to better user experiences and efficient applications. But how can you achieve that elusive speed boost, especially when dealing with potential database bottlenecks?

In this article, we will explore how to use Platformatic to make your Node.js API up to 5x faster. We'll also explore practical steps to optimize your API's performance and improve your user experience.

Prerequisites

To get the most out of this article, you will need an understanding of the following concepts:

  • Node.js setup and installation

  • Building APIs with Platformatic DB

  • Testing APIs with Autocannon

Introducing Platformatic DB

Platformatic DB is a powerful tool that eliminates the need to manually write code when creating CRUD APIs.

With Platformatic DB, you can effortlessly generate both OpenAPI and GraphQL schemas directly from your database, streamlining API development and reducing development time. Flexible at its core, Platformatic DB allows for customization through Node.js and Fastify plugins, leveraging the seamless integration with Platformatic Service.

Whether your project relies on SQLite, MySQL, MariaDB, or PostgreSQL, Platformatic DB accommodates your database preferences, allowing you to choose the best database solution to match your specific project needs.

Optimizing Node.js API Performance with Caching

Caching, the practice of storing frequently accessed data to reduce retrieval time, is a crucial aspect of optimizing your APIs. The process of caching is similar to a situation in which you would keep your favorite book on your desk instead of a distant library shelf, ensuring quicker access to it when you wish to read it.

async-cache-dedupe is a cache for asynchronous fetching of resources with full deduplication. It plays a vital role in reducing response times and improving your API's efficiency with minimal effort.

Async Cache Dedupe stores frequently requested data in memory, which is a temporary storage location. This means that the data your API needs can be accessed quickly without needing to repeatedly fetch this data from slower sources, like databases, every time.

💡Platformatic DB can handle much of this heavy lifting, with caching and API optimization built into it.

The Advantages of Caching

Caching offers several notable advantages, including:

  • Speed: Caching makes data instantly accessible, drastically reducing API response times, and offering a smoother user experience.

  • Lesser Latency: By reducing the need to constantly communicate with the database, caching ensures your APIs are swifter and more responsive.

  • Saving Bandwidth: Using cached responses reduces the frequency of data fetching, which is particularly beneficial for applications where data transfer costs matter.

Performance Testing with Autocannon

To ensure the effectiveness of your optimizations, you need to conduct performance testing. Autocannon is a versatile HTTP/1.1 benchmarking tool that allows you to measure the capabilities of your API accurately. Let's look at how to set up Autocannon from the command line and leverage it for efficient performance testing.

Installing Autocannon

Before you can begin performance testing with Autocannon, you'll need to install it from the command line. Here's how to do it:

1. Ensure you have Node.js installed on your system. If not, download and install it from the official Node.js website.

2. Open your terminal or command prompt and run the following command to install Autocannon globally:

npm install -g autocannon

This command will install Autocannon as a global package, allowing you to use it from anywhere in your system.

3. To verify that Autocannon is correctly installed, run the following command:

autocannon —v

Running Performance Tests

Now that you have Autocannon installed, it's time to run performance tests on your Platformatic API. These tests will provide you with valuable data regarding your API's performance and help you assess the impact of caching.

To conduct these tests, we'll use a simple bash script:

autocannon -c 100 -d 5 127.0.0.1:3042/movies

Here's what each part of the script does:

  • -c 100: This flag sets the number of connections to 100, meaning that the test will simulate 100 concurrent users.

  • -d 5: This flag sets the duration of the test to 5 seconds.

  • 127.0.0.1:3042/movies: This is your API endpoint that Autocannon will target.

This command will run performance tests against the specified API endpoint. Autocannon will provide you with a wealth of data regarding your API's performance, including response times, requests per second, latency, and more.

The result of this initial test may indicates a latency of about 37 milliseconds at the 99th percentile and around 5203 requests per second at the 97.5th percentile.

Now, let's enable caching to see the improvements. In your “platformatic.db.json” file, add the following line to the “db” property to enable caching:

"cache": true

Make sure that the "$schema" in your platformatic.db.json file is set to:

"$schema": "https://platformatic.dev/schemas/v1.2.0/db"

After enabling caching and making the schema update, running the same test shows significantly improved results. The latency decreases to around 15 milliseconds at the 99th percentile, and the requests per second increase to approximately 25,567 at the 97.5th percentile.

How it works

Platformatic makes it easy to implement caching in Node.js applications by providing a built-in caching layer. This layer automatically generates SQL queries and computes hashes of those queries.

When a request is made for data that is not already in the cache, Platformatic will de-duplicate the request and only execute one query to the database. This can save a significant amount of time, especially if the database is under heavy load.

How Platformatic DB Utilizes Caching

Platformatic DB incorporates caching through the efficient async-cache-dedupe library. Below is a detailed breakdown of how this caching mechanism is integrated into the Platformatic DB source code:

  • Cache Setup: When setting up the cache, it validates the provided options, and if none are given, it defaults to a "ttl" (time-to-live) of 0. In this context, a "ttl" of 0 implies deduplication only. Further features related to full caching are mentioned as a TODO for future implementation.

  • Caching Implementation: The actual caching is facilitated by the createCache function, which is part of the async-cache-dedupe library. It is configured with the provided or default options (in this case, deduplication only).

  • Cache Definitions: Platformatic DB goes a step further to define caching for each entity's "find" function. A unique function name, derived from the entity's name, is created for this purpose (e.g., "entityNameFind"). This step specifies the serialization of queries and the asynchronous function that carries out the actual caching.

  • Hook Integration: Platformatic DB integrates these caching definitions with the entity's "find" function using hooks. When a "find" request is made, it checks whether a transaction (query.tx) is involved. If not, it routes the request through the previously defined cache function, effectively leveraging caching for faster data retrieval.

This caching approach helps avoid redundant database queries by deduplicating requests and storing frequently accessed data in memory. Platformatic DB automatically handles this caching, saving developers from manually implementing such optimizations.

Setting Up a Platformatic DB Project

To start using Platformatic DB, you need to create a new project. This is a simple process that will give you access to all the tools and resources you need to develop a fast and scalable Node.js API.

Create a New API Project

To create a new Platformatic project using the Platformatic CLI;

1. Open your terminal and run the following command:

npm create platformatic@latest

2. The CLI will initiate an interactive command-line tool to guide you through the project setup process. For this guide, select the following:

Once the wizard completes, you will have a Platformatic app project created in the "project" folder.

Start your API server

To navigate to the project directory using the VS Code terminal, you can use the following command:

cd project

This command will change your current directory to the quick-start directory, where your Platformatic API project is located. Run the following command to start your API server:

npm start

This command will automatically map your SQL database to REST and GraphQL API interfaces, and start the Platformatic API server on port 3042.

Using the REST API interface

To interact with the REST interface of your API, you can use cURL or any other HTTP client. Here's an example using cURL, where we will be creating a local database of our favorite movies:

Create a new movie

curl -X POST -H "Content-Type: application/json" \
  -d "{ \"title\": \"John Wick\" }" \
    http://localhost:3042/movies

You should receive a response from your API like this:

{"id":1,"title":"John Wick"}

Get all movies:

curl http://localhost:3042/movies

You should receive a response from your API like this, with an array containing all the movies in your database:

[{"id":1,"title":"John Wick"}]

You also have the option to access the API documentation interface at "http://127.0.0.1:3042/documentation/static/index.html" to conveniently make your POST and GET requests. This interface provides an interactive and user-friendly way to interact with your API and receive responses.

Wrapping Up

Caching is a powerful technique that can significantly improve the performance of Node.js APIs.

Platformatic DB makes it easy to implement caching in Node.js APIs by providing a built-in caching mechanism that leverages the async-cache-dedupe library.

In a nutshell, we explored the advantages of caching and how Platformatic DB utilizes caching to improve API performance. We also discussed how to set up a Platformatic DB project and perform performance testing with Autocannon.

As demonstrated in the performance testing section, enabling caching in Platformatic DB can increase the speed of your Node.js API by up to 5x. This can lead to a significant improvement in the user experience of your application.

For any questions you encounter while following this guide, or to simply join our community, find us on Discord.