search
HomeWeb Front-endJS Tutorial5 tips to improve Node application performance

5 tips to improve Node application performance

Related recommendations: "node js tutorial"

"If there is no nginx in front of your node server, then you may have done something wrong. ” — Bryan Hughes

Node.js is the leading tool for building server-side applications using the most popular language—JavaScript. Node.js is considered a key tool for microservices-based development and deployment due to its ability to provide both web server and application server functionality.

In back-end development, Node.js can replace or extend Java and .NET.

Node.js is single-threaded non-blocking I/O, allowing it to support thousands of concurrent operations. This is exactly how NGINX solves the C10K problem. Node.js is known for its efficient performance and development efficiency.

So, what did you do wrong?

Some flaws in Node.js make Node.js-based systems face potential performance problems or even crashes, which is especially obvious when system traffic increases rapidly. Although Node.js is a good tool for handling web application logic, it is not good at handling static files, such as images and JavaScript files, and it is also not good at load balancing among multiple servers.

In order to better use Node.js, you need to leave functions such as caching static files, proxying, load balancing, and client connection management to NGINX.

Here are some suggestions to improve the performance of Node.js:

Implement a reverse proxy server
Cache static files
Multi-server load balancing
Proxy WebSocket connection
Implement SSL/TLS and HTTP/2
Note: The fastest way to improve the performance of Node.js applications is to modify your Node.js files to take advantage of multi-core processors, check out this article to learn How to take full advantage of multi-core CPUs on your server.

1. Implement a reverse proxy server

Compared to most application servers, Node.js can easily handle a large number of networks Traffic, but that's not what Node.js was designed for.

If you have a high-traffic site, the first step to improve performance is to put a reverse proxy server in front of your Node.js. This protects your Node.js server from being directly exposed to the network, and allows you to flexibly use multiple application servers for load balancing and static file caching.

5 tips to improve Node application performance

Use NGINX as a reverse proxy in front of an existing server. As a core application of NGINX, it has been used in thousands of sites around the world.

The following are the advantages of using NGINX as a reverse proxy server:

  • Simplified permission handling and port allocation

  • More Efficiently handle static resources

  • Better handle Node.js crashes

  • Mitigate the impact of DoS attacks

Note: This article explains how to use NGINX as a reverse proxy server in Ubuntu 14.04 or CentOS environment, and it is effective to use NGINX as a reverse proxy server in front of Node.js.

2. Caching static files

As traffic increases, Node-based servers begin to show pressure. At this time, you may want to do two things:

1. Use more Node.js servers.

2. Load balancing among multiple servers

This is actually very simple. NGINX was implemented as a reverse proxy server from the beginning, which makes it easy to do caching and load balancing. wait.

The Modulus website has a useful article that introduces the performance improvement of using NGINX as a Node.js reverse proxy server. Using only Node.js, the author's website can handle 900 requests per second. After using NGINX as a reverse proxy server to handle static files, the website can handle more than 1,600 requests per second, a nearly twice the performance increase.

The following is the configuration code for the website to improve the performance mentioned above:

nginx

server {
  listen 80;
  server_name static-test-47242.onmodulus.net;
  root /mnt/app;
  index index.html index.htm;
  location /static/ {
       try_files $uri $uri/ =404;
  }
  location /api/ {
       proxy_pass http://node-test-45750.onmodulus.net;
  }
}

3. Implement Node.js load balancing

Ultimate goal — Node.js runs multiple application servers and balances load across those servers.

It is difficult to implement load balancing in Node.js because Node.js allows browser-side JavaScript and server-side Node.js to interact with data through json, which means that the same client can access it repeatedly A specific application server, and it is difficult to share sessions between multiple application servers.

NGINX implements stateless load balancing:

Round Robin. New requests go to the next server in the list
Least Connections. New requests go to the server with the least number of connections
IP Hash. Specify the server based on the hash value of the client IP
Only IP Hash, a method that can reliably proxy client requests to the same server, can benefit the Node.js application server.

4. Proxy WebSocket connection

All versions of HTTP are designed for clients to actively request the server, while WebSocket can enable the server to actively request the client Terminal message push.

The WebSocket protocol makes stable interaction between the client and the server simpler, while also providing smaller interaction latency. When you need a full-duplex communication, that is, both the client and the server can actively initiate message requests when needed, then use WebSocket.

The WebSocket protocol has a sound JavaScript interface, so it is also natively suitable for using Node.js as an application server. As the number of connections increases, it makes sense to use NGINX as a proxy on the client and Node.js server for caching static files and load balancing.

5. Implement SSL/TLS and HTTP/2

More and more websites use SSL/TLS to ensure the security of information interaction You can also consider whether to add it to your website, but if you decide to do it, NGINX has two ways to support it:

  1. You can use NGINX As an SSL/TLS reverse proxy, the Node.js server uses the decrypted request and returns the unencrypted content to NGINX.

  2. Using HTTP/2 can offset the performance overhead caused by SSL/TLS. NGINX supports HTTP/2, so you can use HTTP/2 and SSL proxy requests at the same time, and your Node No changes are required for the .js server.

During the implementation phase you need to update the URL in the Node.js configuration file and use SPDY or HTTP/2 to optimize the connection in your NGINX configuration file. Adding HTTP/2 support means that browsers that support HTTP/2 can use the new protocol to interact with your application, while older browsers continue to use HTTP/1.x.

5 tips to improve Node application performance

Summary

This blog describes some of the main ways to improve the performance of Node.js applications, mainly about the mixed use of NGINX and Node.js Way. With NGINX acting as a reverse proxy, you can cache static files, load balance, proxy WebSocket connections, and configure SSL/TLS and HTTP/2 protocols.

The hybrid of NGINX and Node.js is recognized as a friendly way to create micro application servers, and can also flexibly extend existing SOA-based projects, such as Java or Microsoft.NET projects. This article helps you optimize your Node.js application. If you use Node.js, it is best to use it with NGINX.

Original author: Floyd Smith

Original link: https://www.nginx.com/blog/5-performance-tips-for-node-js-applications/

Translation link: https://blog.maxleap.cn/zh/archives/512

For more programming-related knowledge, please visit: Programming Learning Course ! !

The above is the detailed content of 5 tips to improve Node application performance. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:segmentfault. If there is any infringement, please contact admin@php.cn delete
JavaScript in Action: Real-World Examples and ProjectsJavaScript in Action: Real-World Examples and ProjectsApr 19, 2025 am 12:13 AM

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.

JavaScript and the Web: Core Functionality and Use CasesJavaScript and the Web: Core Functionality and Use CasesApr 18, 2025 am 12:19 AM

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

Understanding the JavaScript Engine: Implementation DetailsUnderstanding the JavaScript Engine: Implementation DetailsApr 17, 2025 am 12:05 AM

Understanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.

Python vs. JavaScript: The Learning Curve and Ease of UsePython vs. JavaScript: The Learning Curve and Ease of UseApr 16, 2025 am 12:12 AM

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Python vs. JavaScript: Community, Libraries, and ResourcesPython vs. JavaScript: Community, Libraries, and ResourcesApr 15, 2025 am 12:16 AM

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.

From C/C   to JavaScript: How It All WorksFrom C/C to JavaScript: How It All WorksApr 14, 2025 am 12:05 AM

The shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.

JavaScript Engines: Comparing ImplementationsJavaScript Engines: Comparing ImplementationsApr 13, 2025 am 12:05 AM

Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

Beyond the Browser: JavaScript in the Real WorldBeyond the Browser: JavaScript in the Real WorldApr 12, 2025 am 12:06 AM

JavaScript's applications in the real world include server-side programming, mobile application development and Internet of Things control: 1. Server-side programming is realized through Node.js, suitable for high concurrent request processing. 2. Mobile application development is carried out through ReactNative and supports cross-platform deployment. 3. Used for IoT device control through Johnny-Five library, suitable for hardware interaction.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use