Table of Contents
Node features: high concurrency" >Node features: high concurrency
Single-threaded
Asynchronous I/O
Transaction Driver
What the traffic access layer does is All accepted traffic is processed and the following services are provided:
Database layer
Home Web Front-end JS Tutorial A brief discussion on high concurrency and distributed clustering in node.js

A brief discussion on high concurrency and distributed clustering in node.js

Aug 01, 2018 pm 03:54 PM
javascript node.js Distributed deployment

This article introduces to you a brief discussion of high concurrency and distributed clusters in node.js. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Node features: high concurrency

Before explaining why node can achieve high concurrency, you might as well understand several other features of node:

Single-threaded

Let’s first clarify a concept, that is: node is single-threaded, which is the same as the characteristics of JavaScript in the browser, and the JavaScript main thread in node is the same as Other threads (such as I/O threads) cannot share state.

The advantage of single thread is:

  • There is no need to pay attention to the state synchronization between threads like multi-threading

  • There is no overhead caused by thread switching

  • There is no deadlock

Of course, single thread also has many disadvantages:

  • Unable to fully utilize multi-core CPU

  • A large number of calculations occupying the CPU will cause application blocking (that is, not suitable for CPU-intensive applications)

  • Errors will cause the entire application to exit

However, it seems today that these disadvantages are no longer problems or have been appropriately resolved:

(1) Create Process or subdivision example

Regarding the first problem, the most straightforward solution is to use the child_process core module or cluster: child_process and net combined application. We can make full use of each core by creating multiple processes on a multi-core server (usually using a fork operation), but we need to deal with inter-process communication issues.

Another solution is that we can divide the physical machine into multiple single-core virtual machines, and use tools such as pm2 to manage multiple virtual machines to form a cluster architecture to efficiently run the required services. As for each I will not discuss the communication (status synchronization) between machines here, and will explain it in detail in the Node distributed architecture below.

(2) Time slice rotation

Regarding the second point, after discussing with my friends, I believe that we can use time slice rotation to simulate multi-threading on a single thread and appropriately reduce the risk of application blocking. Feeling (although this method will not really save time like multi-threading)

(3) Load balancing, dead pixel monitoring/isolation

As for the third point, my friends and I We have also discussed that the main pain point is that node is different from JAVA, and the logic it implements is mainly asynchronous.

This results in node being unable to use try/catch to catch and bypass errors as conveniently as JAVA, because it is impossible to determine when the asynchronous task will return the exception. In a single-threaded environment, failure to bypass errors means that the application will exit, and the gap between restarts and recovery will cause service interruptions, which we do not want to see.

Of course, now that the server resources are abundant, we can use tools such as pm2 or nginx to dynamically determine the service status. Isolate the bad pixel server when a service error occurs, forward the request to the normal server, and restart the bad pixel server to continue providing services. This is also part of Node's distributed architecture.

Asynchronous I/O

You may ask, since node is single-threaded and all events are processed on one thread, shouldn't it be inefficient and contrary to high concurrency?

On the contrary, the performance of node is very high. One of the reasons is that node has the asynchronous I/O feature. Whenever an I/O request occurs, node will provide an I/O thread for the request. Then node will not care about the I/O operation process, but will continue to execute the event on the main thread. It only needs to be processed when the request returns the callback. That is to say, node saves a lot of time waiting for requests.

This is also one of the important reasons why node supports high concurrency

In fact, not only I/O operations, most of the operations of node are asynchronous. carried out in a manner. It is like an organizer. It does not need to do everything personally. It only needs to tell members how to operate correctly, accept feedback, and handle key steps, so that the entire team can run efficiently.

Transaction Driver

You may want to ask again, how does node know that the request has returned a callback, and when should it handle these callbacks?

The answer is another feature of node: Transaction driver, that is, the main thread runs the program through the event loop event loop trigger

This is node support Another important reason for high concurrency

Illustration of Event loop in node environment:

   ┌───────────────────────┐
┌─>│        timers         │<p><strong>poll stage:</strong></p><p>When entering poll phase, and when no timers are called, the following situation will occur: </p><p> (1) If the poll queue is not empty: </p>
Copy after login
  • Event Loop will be executed synchronously Poll the callback (new I/O event) in the queue until the queue is empty or the executed callback comes online.

(2) If the poll queue is empty:

  • If the script calls setImmediate(), the Event Loop will end the poll phase and enter Execute the callback of setImmediate() in the check phase.

  • If the script is not called by setImmediate(), the Event Loop will wait for callbacks (new I/O events) to be added to the queue, and then execute them immediately.

When entering the poll phase and calling timers, the following situation will occur:

  • Once the poll queue is empty, the Event Loop will check Whether timers, if one or more timers have arrived, the Event Loop will return to the timer phase and execute the callbacks of those timers (ie, enter the next tick).

##Priority:

Next Tick Queue > MicroTask Queue

setTimeout, setInterval > setImmediate

Since timer needs to take out the timer from the red-black tree to determine whether the time has arrived, the time complexity is O(lg(n)), so if you want to execute an event asynchronously immediately, it is best not to use setTimeout(func, 0) . Instead use process.nextTick() to do it.

Distributed Node Architecture

The Node cluster architecture I learned is mainly divided into the following modules:

Nginx (load balancing, scheduling) -> Node cluster-> Redis (synchronization status)

I compiled a picture according to my understanding:

A brief discussion on high concurrency and distributed clustering in node.js## Of course, this should be an ideal architecture. Because although Redis's read/write is quite fast, this is because it stores data in the memory pool and performs related operations on the memory.

This is quite high for the memory load of the server, so usually we still add Mysql to the architecture, as shown below:

A brief discussion on high concurrency and distributed clustering in node.jsExplain this picture first:

When user data arrives, the data is first written to Mysql. When Node needs the data, it will go to Redis to read it. If it is not found, it will go to Mysql to query the desired data and write it. Enter Redis, and you can directly query in Redis next time you use it.


The advantages of joining Mysql compared to only reading/writing in Redis are:

(1) Avoid writing useless data to Redis in the short term, occupying memory, and reducing the burden on Redis

(2) When specific queries and analysis of data are needed in the later stage (such as analyzing the increase in users of operational activities), SQL relational query can provide great help

Of course, when dealing with large traffic in a short period of time When writing, we can also write data directly to Redis to quickly store data and increase the server's ability to cope with traffic. When the traffic subsides, we can write the data to Mysql separately.

After briefly introducing the general architecture, let’s take a closer look at the details of each part:

Traffic access layer

What the traffic access layer does is All accepted traffic is processed and the following services are provided:

    Traffic buffering
  • Diversion and forwarding

A brief discussion on high concurrency and distributed clustering in node.js

##Timeout detection
  • Timeout in establishing a connection with the user
    • Read user body timeout
    • Connect backend timeout
    • Read backend response header timeout
    • Write response timeout
    • Long connection timeout with user
    • Cluster health check/isolation Bad pixel server
  • Isolate the bad pixel server and try to repair/restart until the server returns to normal
    • Failure reset Trial mechanism
  • #After the request is forwarded to a certain machine in a certain cluster and a failure is returned, the request is forwarded to other machines in the cluster, or to machines across clusters. Retry
    • Connection pool/session persistence mechanism
  • Use the connection pool mechanism for delay-sensitive users to reduce connection establishment Time
    • Security Protection
  • Data Analysis
  • When forwarded to each product After going online, it’s time for the load layer to work: forward the request to various computer rooms according to the situation

A brief discussion on high concurrency and distributed clustering in node.jsOf course, this platform does not only forward this function , you can understand it as a large private cloud system that provides the following services:

File upload/service online deployment
  • line Modify the configuration
  • Set scheduled tasks
  • Online system monitoring/log printing service
  • Online instance management
  • Mirror center
  • etc...
  • Node cluster layer
The main work of this layer is:

(1) Write reliable Node code and provide back-end services for needs

(2) Write high-performance query statements, interact with Redis and Mysql, and improve query efficiency

(3) Synchronize the status of each Node service in the cluster through Redis

(4) Pass Hardware management platform, manages/monitors the status of physical machines, manages IP addresses, etc. (In fact, it feels inappropriate to put this part of the work on this layer, but I don’t know which layer it should be placed on...)

(Of course, I only briefly list the entries in this part, it still takes time to accumulate and understand deeply)

Database layer

The main work of this layer is:

(1) Create Mysql and design related pages and tables; establish necessary indexes and foreign keys to improve query convenience

(2) Deploy redis and provide corresponding interfaces to the Node layer

Related recommendations :

How vue uses axios to request back-end data

Analysis of form input binding and component foundation in Vue

The above is the detailed content of A brief discussion on high concurrency and distributed clustering in node.js. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

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 Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Two Point Museum: All Exhibits And Where To Find Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to build 4 virtual disks to build a distributed MinIO cluster in Linux? How to build 4 virtual disks to build a distributed MinIO cluster in Linux? Feb 10, 2024 pm 04:48 PM

Since I have recently started to be responsible for the construction and stability operation and maintenance of object storage-related systems, as a novice in "object storage", I need to strengthen my learning in this area. Since the company currently uses MinIO to build the company's object storage system, I will gradually share my learning experience about MinIO in the future. Everyone is welcome to continue to pay attention. This article mainly introduces how to set up MinIO in a test environment, which is also the most basic step in building a MinIO learning environment. 1. Prepare the experimental environment using OracleVMVirtualBox virtual machine, install a minimum version of Linux, and then add 4 virtual disks to serve as MinIO virtual disks. The experimental environment is as follows: Next, let me briefly introduce it to you

How to implement an online speech recognition system using WebSocket and JavaScript How to implement an online speech recognition system using WebSocket and JavaScript Dec 17, 2023 pm 02:54 PM

How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

WebSocket and JavaScript: key technologies for implementing real-time monitoring systems WebSocket and JavaScript: key technologies for implementing real-time monitoring systems Dec 17, 2023 pm 05:30 PM

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

How to implement an online reservation system using WebSocket and JavaScript How to implement an online reservation system using WebSocket and JavaScript Dec 17, 2023 am 09:39 AM

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

How to use JavaScript and WebSocket to implement a real-time online ordering system How to use JavaScript and WebSocket to implement a real-time online ordering system Dec 17, 2023 pm 12:09 PM

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

JavaScript and WebSocket: Building an efficient real-time weather forecasting system JavaScript and WebSocket: Building an efficient real-time weather forecasting system Dec 17, 2023 pm 05:13 PM

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

Simple JavaScript Tutorial: How to Get HTTP Status Code Simple JavaScript Tutorial: How to Get HTTP Status Code Jan 05, 2024 pm 06:08 PM

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

How to use insertBefore in javascript How to use insertBefore in javascript Nov 24, 2023 am 11:56 AM

Usage: In JavaScript, the insertBefore() method is used to insert a new node in the DOM tree. This method requires two parameters: the new node to be inserted and the reference node (that is, the node where the new node will be inserted).

See all articles