Five techniques for improving Node.js application performance

Source: Internet
Author: User
Tags hash node server

If your node server has no nginx in front of you, 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. Because of the ability to provide both Web servers and application servers, Node.js is considered a key tool for development and deployment on a micro-service basis.

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

Node.js is single-threaded non-blocking I/O, enabling it to support thousands of concurrent operations. This is the same way as NGINX solves c10k problems. Node.js is known for its efficient performance and development efficiency.

  So, where did it go wrong?

Some of the flaws in Node.js make node.js systems face potential performance problems and even crashes, especially when system traffic is growing rapidly. Although Node.js is a great tool for dealing with Web application logic, it is not good at working with static files, such as pictures and JavaScript files, and is also not good at load balancing among multiple servers.

In order to better use node.js, you need to cache static files, agents, load balancing, client connection management functions to NGINX to do.

Here are some suggestions for improving Node.js performance:

Implementing a Reverse proxy server

Caching static files

Multi-server load Balancing

Agent WebSocket Connection

 Implement SSL/TLS and HTTP/2

Note: The quickest way to improve node.js application performance is to modify your Node.js file to take advantage of multi-core processors and view this article to learn how to take full advantage of multi-core CPUs on the server.

1. Implement a reverse proxy server

Compared to most application servers, Node.js can handle a lot of network traffic very easily, but this is not the original intention of node.js design.

If you have a high flow site, the first step in improving performance is to put a reverse proxy server in front of your node.js. This protects your Node.js server from direct exposure to the network and allows you to flexibly use multiple application servers for load balancing and static file caching.

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

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

Simplifies permission handling and port assignment

More efficient processing of static resources

Better deal with Node.js crashes

Mitigating 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 to do a reverse proxy server before node.js.

2. Caching static files

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

1. Use more Node.js servers.

2. Load balancing across multiple servers

This is actually very simple, NGINX started as a reverse proxy server to implement, which makes it easy to do caching and load balancing.

Modulus's website has a useful article that describes the performance improvement of using NGINX as a node.js reverse proxy server. When using Node.js only, the author's web site can handle 900 requests per second. After using NGINX as a reverse proxy server to process static files, the site can handle more than 1600 requests per second, nearly twice times the performance boost.

Here is the configuration code for the site to do the above performance improvement:

3. Achieve Node.js Load Balancing

The final target-node.js runs multiple application servers and balances the load among these servers.

Node.js It is more difficult to achieve load balancing because Node.js allows browser-side JavaScript and server-side node.js to do data interaction through JSON, which means that the same client can repeatedly access a particular application server, It is also difficult to share sessions among multiple application servers.

NGINX the way to achieve a stateless load balancing:

Round Robin. New requests go to the next server in the list

least connections. New requests to connect the least number of servers

IP Hash. Specify the server based on the hash value of the client IP

Only IP Hash, a reliable way to broker client requests to the same server, can benefit the Node.js application server.

4. Agent WebSocket Connection

All versions of HTTP are designed for the client to actively request the server, and WebSocket can implement the server's unsolicited message push to the client.

The WebSocket protocol makes it easier to stabilize the client and server side, while providing a smaller interaction latency. When you need a Full-duplex communication, that is, both the client and the server can initiate a message request when needed, then use the WebSocket.

The WebSocket protocol has a sound JavaScript interface and is therefore native to use as an application server for Node.js. When the number of connections increases, it makes sense to use NGINX to cache static files and load balancing on the client and Node.js server side.

5. Implementation of 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 site, but if you decide to do it, then NGINX has two ways to support it:

1. You can use NGINX to do SSL/TLS reverse proxy, node.js the server to use the decrypted request and then return unencrypted content to NGINX.

2. Using HTTP/2 can offset the performance overhead of SSL/TLS, NGINX support HTTP/2, so you can use both HTTP/2 and SSL proxy requests, and your Node.js server does not need to make any changes.

In 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 profile. Adding HTTP/2 support means that browsers that support HTTP/2 can use new protocols to interact with your application, while older browsers continue to use http/1.x.

  Summarize

This blog describes some of the main ways in which node.js applications improve performance, mainly on how NGINX and Node.js are mixed. Using NGINX as a reverse proxy, you can cache static files, load Balancing, proxy WebSocket connections, configuration Ssl/tls, and HTTP/2 protocols.

NGINX and Node.js blends are recognized as a friendly way to create a micro-application server, as well as flexible extensions of existing SOA based projects, such as Java or Microsoft.NET projects. This article helps you optimize your node.js application, and if you use Node.js, it's best to use it with NGINX.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.