Making My Node.js API Super Fast

Rate this content
Bookmark

Node.js servers need to process a large number of requests concurrently as the scale grows.  A Node.js micro service which receives an API request needs to do multiple actions, like parsing JWTs, use caching, work with the databases and more. In this talk, Tamar will show strategies to improve your REST API performance - starting from new Node.js frameworks that can work faster, better parsing of request parts, efficient work with caching and DB , better parallelism and more strategies. The talk will include demos, benchmarks and profiling of code to see the improvements. At the end of this talk, developers will have practical knowledge on how to improve API performance in various Node.js platforms.

Tamar Twena-Stern
Tamar Twena-Stern
34 min
04 Apr, 2024

Comments

Sign in or register to post your comment.

Video Summary and Transcription

This talk focuses on improving performance in Node.js API development. It covers various areas such as optimizing database work, connection pool, JSON parsing, logging, and web framework selection. Key highlights include the use of Native Mongo Driver for better database performance, optimizing connection pool for improved throughput, replacing Express serializer for faster serialization and deserialization, and choosing Festify as an efficient web framework. Caching and authentication's impact on performance is discussed, along with recommendations for caching types. The talk also emphasizes considering environmental factors and human impact on performance. Fastify is highlighted as a recommended tool for Node.js performance optimization.

1. Introduction to Performance Optimization

Short description:

Hi everyone, welcome to my session about improving performance in your Node.js API. I'm passionate about JavaScript and Node.js. Today we'll discuss improving performance in your API layer, covering areas such as database, serialization, deserialization, logging, web frameworks, and caching. Let's start with database work. Optimize DB queries, use efficient indexes, denormalize data, and consider solutions like read-write replicas and sharding.

Hi everyone, happy to be here and welcome to my session about how to improve performance in your Node.js API and make it really fast. So before starting to talk about technical stuff, let me introduce myself. My name is Tamar. You can follow me on those links and contact me on those links. I'm really passionate about JavaScript and Node.js and my interest in those technologies started when I founded a startup of my own and I wrote the entire backend in Node.js and from there the rest is history.

And enough talking about myself, let's start talking about performance, which is one of my favorite things that I like to talk about. And today we are going to talk about how we're going to improve performance in your API layer. And we're going to revise, we're going to look at those areas, we're going to look at database. We're going to look at serialization and deserialization of your Jasons. We're going to talk about logging. It looks trivial to you, but that can impact your performance. The web framework that you choose and caching.

So let's start with database work. First of all, before we go into JavaScript related stuff, there are some things that you need to keep in mind for every technology that you work with, for every programming language. First of all, of course your DB queries have to be optimized. All of the indexes have to be very efficient and enable really fast fetching and efficient operations. And then you need to denormalize your data. What does it mean? If you have a table of 50 columns and you only need three, don't fetch the entire 50. Fetch only what you need, for example. And save the data in a way which is efficient. Don't save inefficient things. And then you need to think about solutions in the DB layer. That can be read-write replicas that can improve your performance. Working with a replica set, matching the DBs that support that, and sharding.

2. Improving Node.js Application Database Work

Short description:

Let's discuss improving Node.js application-related database work by comparing Mongoose and Native Mongo Driver. Key measurements for performance benchmarking include latency (looking at the 99th percentile), and throughput (average requests per second and total requests). In the benchmark, both Mongoose and Native Mongo Driver were used to post a person object to the database. The results showed that Native Mongo Driver performed significantly better than Mongoose, with an increase in average requests per second and a much smaller 99th percentile.

But we are in a Node.js conference, and after talking a little bit about generic tips, let's go into Node.js stuff. How can I improve my Node.js application-related database work? When we are starting to work with Node.js, we are starting to work with the first tutorial that we are all doing, Node.js, express and Mongoose. Let's start with that, and let's take a benchmark and see how that is going to perform. When we are going to do a benchmark, we have to keep in mind the following things. First of all, the measurements that are the most important is the latency. You need to look at the 99th percentile, not the average. Because when you are telling you have a contract with a third party, and you are telling them all of my requests are faster than this, you need to give them the 99th percentile in the latency. Not the median, not the average. That is really important. Second, measuring throughput. For measuring throughput, we have two important things that we need to look at. First is the average request per second, and then the total request. Those measurements will give you a good idea about how your throughput is improving.

Now we are going to compare Mongoose and Native Mongo Driver. I wrote a server that works with Mongoose, and that server exposed one post request, that is posting a person object, something really simple. The code is not something really interesting. We are building a server with Express and Mongoose, building a Mongoose schema of several parameters, and then that is the function that we are going to measure. Post, and then we are creating a new person object, and saving it as you can see here. The Native Mongo Driver code is going to do exactly the same. A post request, which also is posting a person object, and that is because we want to compare apples to apples. When we are doing performance benchmark, we want to compare things that are doing the same. Here is the code that is running with the Native Mongo Driver. As you can see, this is also doing insert1 into the database.

Cool. Those are the results that we have with Mongoose. You can see if we are going to look at average requests per second, we are around 5700 requests, and the total request is around 64,000, and the 99th percentile is 4 millisecond, but as we are using Mongo Driver, we can see that things improved hugely. We can see that the average requests per second went up to 9500, which is a lot. It's more than 30%, I think. The 99th percentile is much smaller, by 50%, and we were able to serve, in the time of the benchmark, around 30,000% more requests. As you can see, if we are going to see it in a graph, Native Mongo Driver performs much better than Mongoose.

QnA

Check out more articles and videos

We constantly think of articles and videos that might spark Git people interest / skill us up or help building a stellar career

It's a Jungle Out There: What's Really Going on Inside Your Node_Modules Folder
Node Congress 2022Node Congress 2022
26 min
It's a Jungle Out There: What's Really Going on Inside Your Node_Modules Folder
Top Content
Do you know what’s really going on in your node_modules folder? Software supply chain attacks have exploded over the past 12 months and they’re only accelerating in 2022 and beyond. We’ll dive into examples of recent supply chain attacks and what concrete steps you can take to protect your team from this emerging threat.
You can check the slides for Feross' talk here.
Towards a Standard Library for JavaScript Runtimes
Node Congress 2022Node Congress 2022
34 min
Towards a Standard Library for JavaScript Runtimes
Top Content
You can check the slides for James' talk here.
Get rid of your API schemas with tRPC
React Day Berlin 2022React Day Berlin 2022
29 min
Get rid of your API schemas with tRPC
Do you know we can replace API schemas with a lightweight and type-safe library? With tRPC you can easily replace GraphQL or REST with inferred shapes without schemas or code generation. In this talk we will understand the benefit of tRPC and how apply it in a NextJs application. If you want reduce your project complexity you can't miss this talk.
ESM Loaders: Enhancing Module Loading in Node.js
JSNation 2023JSNation 2023
22 min
ESM Loaders: Enhancing Module Loading in Node.js
Native ESM support for Node.js was a chance for the Node.js project to release official support for enhancing the module loading experience, to enable use cases such as on the fly transpilation, module stubbing, support for loading modules from HTTP, and monitoring.
While CommonJS has support for all this, it was never officially supported and was done by hacking into the Node.js runtime code. ESM has fixed all this. We will look at the architecture of ESM loading in Node.js, and discuss the loader API that supports enhancing it. We will also look into advanced features such as loader chaining and off thread execution.
Out of the Box Node.js Diagnostics
Node Congress 2022Node Congress 2022
34 min
Out of the Box Node.js Diagnostics
In the early years of Node.js, diagnostics and debugging were considerable pain points. Modern versions of Node have improved considerably in these areas. Features like async stack traces, heap snapshots, and CPU profiling no longer require third party modules or modifications to application source code. This talk explores the various diagnostic features that have recently been built into Node.
You can check the slides for Colin's talk here. 
Step aside resolvers: a new approach to GraphQL execution
GraphQL Galaxy 2022GraphQL Galaxy 2022
16 min
Step aside resolvers: a new approach to GraphQL execution
Though GraphQL is declarative, resolvers operate field-by-field, layer-by-layer, often resulting in unnecessary work for your business logic even when using techniques such as DataLoader. In this talk, Benjie will introduce his vision for a new general-purpose GraphQL execution strategy whose holistic approach could lead to significant efficiency and scalability gains for all GraphQL APIs.

Workshops on related topic

Node.js Masterclass
Node Congress 2023Node Congress 2023
109 min
Node.js Masterclass
Top Content
Workshop
Matteo Collina
Matteo Collina
Have you ever struggled with designing and structuring your Node.js applications? Building applications that are well organised, testable and extendable is not always easy. It can often turn out to be a lot more complicated than you expect it to be. In this live event Matteo will show you how he builds Node.js applications from scratch. You’ll learn how he approaches application design, and the philosophies that he applies to create modular, maintainable and effective applications.

Level: intermediate
Building GraphQL APIs on top of Ethereum with The Graph
GraphQL Galaxy 2021GraphQL Galaxy 2021
48 min
Building GraphQL APIs on top of Ethereum with The Graph
WorkshopFree
Nader Dabit
Nader Dabit
The Graph is an indexing protocol for querying networks like Ethereum, IPFS, and other blockchains. Anyone can build and publish open APIs, called subgraphs, making data easily accessible.

In this workshop you’ll learn how to build a subgraph that indexes NFT blockchain data from the Foundation smart contract. We’ll deploy the API, and learn how to perform queries to retrieve data using various types of data access patterns, implementing filters and sorting.

By the end of the workshop, you should understand how to build and deploy performant APIs to The Graph to index data from any smart contract deployed to Ethereum.
Build and Deploy a Backend With Fastify & Platformatic
JSNation 2023JSNation 2023
104 min
Build and Deploy a Backend With Fastify & Platformatic
WorkshopFree
Matteo Collina
Matteo Collina
Platformatic allows you to rapidly develop GraphQL and REST APIs with minimal effort. The best part is that it also allows you to unleash the full potential of Node.js and Fastify whenever you need to. You can fully customise a Platformatic application by writing your own additional features and plugins. In the workshop, we’ll cover both our Open Source modules and our Cloud offering:- Platformatic OSS (open-source software) — Tools and libraries for rapidly building robust applications with Node.js (https://oss.platformatic.dev/).- Platformatic Cloud (currently in beta) — Our hosting platform that includes features such as preview apps, built-in metrics and integration with your Git flow (https://platformatic.dev/). 
In this workshop you'll learn how to develop APIs with Fastify and deploy them to the Platformatic Cloud.
Hands-on with AG Grid's React Data Grid
React Summit 2022React Summit 2022
147 min
Hands-on with AG Grid's React Data Grid
WorkshopFree
Sean Landsman
Sean Landsman
Get started with AG Grid React Data Grid with a hands-on tutorial from the core team that will take you through the steps of creating your first grid, including how to configure the grid with simple properties and custom components. AG Grid community edition is completely free to use in commercial applications, so you'll learn a powerful tool that you can immediately add to your projects. You'll also discover how to load data into the grid and different ways to add custom rendering to the grid. By the end of the workshop, you will have created an AG Grid React Data Grid and customized with functional React components.- Getting started and installing AG Grid- Configuring sorting, filtering, pagination- Loading data into the grid- The grid API- Using hooks and functional components with AG Grid- Capabilities of the free community edition of AG Grid- Customizing the grid with React Components
0 to Auth in an Hour Using NodeJS SDK
Node Congress 2023Node Congress 2023
63 min
0 to Auth in an Hour Using NodeJS SDK
WorkshopFree
Asaf Shen
Asaf Shen
Passwordless authentication may seem complex, but it is simple to add it to any app using the right tool.
We will enhance a full-stack JS application (Node.JS backend + React frontend) to authenticate users with OAuth (social login) and One Time Passwords (email), including:- User authentication - Managing user interactions, returning session / refresh JWTs- Session management and validation - Storing the session for subsequent client requests, validating / refreshing sessions
At the end of the workshop, we will also touch on another approach to code authentication using frontend Descope Flows (drag-and-drop workflows), while keeping only session validation in the backend. With this, we will also show how easy it is to enable biometrics and other passwordless authentication methods.
Table of contents- A quick intro to core authentication concepts- Coding- Why passwordless matters
Prerequisites- IDE for your choice- Node 18 or higher
Building a Hyper Fast Web Server with Deno
JSNation Live 2021JSNation Live 2021
156 min
Building a Hyper Fast Web Server with Deno
WorkshopFree
Matt Landers
Will Johnston
2 authors
Deno 1.9 introduced a new web server API that takes advantage of Hyper, a fast and correct HTTP implementation for Rust. Using this API instead of the std/http implementation increases performance and provides support for HTTP2. In this workshop, learn how to create a web server utilizing Hyper under the hood and boost the performance for your web apps.