Life as I understand it

From the “rebellion” in Triple Doors to the maturity of Life as I Understand it, Han’s understanding of life and life has deepened with the increase of his experience. I think the same is probably true of technology, as it is used, explored, thought about and gained more and more understanding. At each stage you can have a different perspective, a different thinking, a different understanding.

My understanding of Node is similar, so when I decided to write this article, it was naturally titled Node.js as I Understand it.

From Life For Life

During this period, I came into contact with Professor Zhou Yizheng’s paper Computational Thinking, which inspired me and led to some new technical understanding and Thinking.

For example, technology and life are actually related:

  • You put things in your backpack that you often need, and that’s presets and caching.
  • When you lose something and you follow the path you’ve taken, it’s called pushing back.
  • The performance model of a multi-server system is which queue you should queue up to pay at the supermarket.

Physics experiments at school often need to be carried out in carefully prepared laboratory environments. My friend studies “unconventional physics experiments”, which encourage physics experiments and teaching in ordinary life scenarios (presenting physics knowledge in the simplest and most visible way). Inspired by this, can computers also learn in life-like scenarios, more simply?

Many students do not get in touch with computers until they are in college. Computers often require certain mathematical thinking and abstract thinking, and we are often not easy to understand some computer concepts. It is relatively easy for us to understand some scenes of life that we are familiar with. Sometimes, technology also comes from life and serves life. Appropriate integration of some scenes in life to describe some obscure technical concepts can help us better understand technology.

What Is Node.js

You’ll come across many node-related concepts, such as single threads, asynchronous I/O, event loops, and so on. The Node debate is full of arguments:

  • Some people think of Node as a language, while others think of it as a JS runtime environment
  • Some people think of Node as single-threaded, others think of Node as multi-threaded
  • Ruan Yifeng and Park Ling on the cycle of events

Node is 10 years old, and I’ve been using Node for 5 years. Whenever I encounter issues such as event loops, asynchronous I/O, ETC., I find that I can’t explain these concepts clearly and feel vaguely understood. So I started trying to understand these concepts, looking at books and materials, and looking at the Source code for Node. When a friend wrote the Node.js Debug Guide and asked me to look at the first draft, I told him what I thought was a better way to make the difficult concepts of event looping easier to understand.

After trying many ways, unfortunately, I have not found the answer I want.

Why Node.js

Until I happened to see RY’s early speech about Node again, I suddenly had a whim and read all of RY’s speech, interview, articles and other materials, and followed the lead to discover the new continent.

Since you don’t know what a Node is, you might as well think about why.

In Germany, The father of Node was introduced to Rack, a framework for simplifying and abstracting Web interfaces on which many Ruby servers are implemented. He was reminded of Nginx’s excellent asynchronous I/O performance and wondered if he could combine the two.

The advent of V8 made him realize that JS runs in browsers with single-threaded and asynchronous I/O. He thought it would be cool to use JS and asynchronous I/O in the server and started experimenting with the idea.

At that time, Ruby, Java and other languages already have a mature server ecosystem, why RY to design a more simple and practical high-performance server. Or: what problem is Node trying to solve?

C10X

C10X: C10K, C10M

Node

Before Node, hardware, network, server, and other problems had changed a lot, but there was no good solution (actually Go has appeared, maybe RY didn’t notice).

Problems with other solutions:

  • C, C++ programming complex, low development efficiency
  • Java multithreaded blocking model, too cumbersome (thread resource consumption, context switch consumption, etc.)

The Node that RY wants to implement corresponds to the software layer:

  • Productivity: Make server-side programming simple and efficient. Node provides a concise development interface that hides the underlying complex implementation logic.
  • Performance: Better I/O performance on asynchronous networks

How To Use

Now that you know what a Node is and why you need it, learn more about how to use it. Understand Node’s strengths and weaknesses so you can use Node in the right context.

advantage

High-performance network I/O

Node is often said to be I/O intensive, and I/O refers to network I/O. The Internet is mostly I/O intensive, such as The Web and IOT.

A unified language

  • Unified front-end and back-end language: no language switching costs, easy to achieve full stack development.
  • Back-end code reuse: For example, the validation of a form can be extracted as a common code base. A set of code can be reused at the back-end to ensure that the validation logic is consistent and only one copy of the code needs to be maintained.
  • Multiterminal code reuse: front end (React), mobile end (RN), PC end (Electron).

insufficient

Lack of industrial standards

The Node community lacks industrial-level standards, such as enterprise standards such as J2EE.

Lack of killer apps

Node has an active community, lots of open source projects on Github, and lots of packages on NPM, but there are few heavyweight frameworks and killer applications, let alone a strong ecosystem like Java Spring.

Performance of interpreted languages

JS interpreted language performance is inherently inadequate, although V8 has done a lot of optimization, but there is still a gap with compiled language performance.

Not suitable for CPU intensive

The Node main thread uses the Event Loop. Running cpu-intensive tasks directly will affect other tasks.

Libuv thread pool can be used, but it is not recommended because of high development and maintenance costs. Under the idea of microservices, Node can use Java, Go and other languages to make up for what cannot be done, but this also brings multi-language costs.

application

Microservice

Microservices are an architectural style in which a large complex software application consists of multiple microservices. Each microservice in the system can be deployed independently. Each microservice is loosely coupled and uses lightweight communication protocols to communicate. Each microservice is only focused on completing one task and doing it well. In all cases, each task represents a small business capability.

Each language has its own advantages and application scenarios. Modern systems often encounter complex and diverse scenarios. Under the micro-service architecture, different services can choose languages according to the corresponding situation and use the advantages of multi-language combination. We actually use multiple languages all the time, like MySQL for C and Hadoop for Java. Network I/O intensive scenario, Node advantage, CPU intensive can be Java, GO, etc.

Monolith to Microservice

Architecture is an evolution, not a design. Architecture often evolves iteratively as the business changes to meet the needs of the business. Our systems tend to start out as monolithic applications and evolve into microservices.

The life cycle of a product can be divided into three stages: 1. 2. Product growth period; 3. Product stability period.

  1. The beginning of the product

In the early stage of a product, the requirements may be unclear, and it is necessary to quickly respond to business changes while balancing software efficiency and quality. At this time, the technology is required to be flexible and quickly respond to changes in requirements. The front-end (React) and back-end (Node) unified JS, can realize code reuse, Node efficient development efficiency, rapid development online and continuous iteration.

  1. Product growth period

With the rapid or even explosive growth of users, the system may have some performance problems or need to solve the previously unreasonable design problems due to rapid online. At this time, the system needs to be optimized or reconstructed to maintain the stability of services and continuous business iteration. Split performance-critical services into microservices and use Java, Go, etc., to handle scenarios (such as CPU-intensive services) that Node is not good at.

To meet the needs of users in multi-device scenarios, mobile and PC versions need to be supported. Browser (React), mobile terminal (RN), PC terminal (Electron) multi-terminal unified JS, code further reuse, reduce the cost of development.

  1. Product stability period

After the main business of the product tends to be stable, the number of users and the amount of data generally reach a relatively large magnitude, and the stability lasts for a long time. Development can have time for optimization and refactoring. The stable business continues to split microservices, Node into API Gateway.

Serverless

The Serverless (SLS) architecture is an Internet-based system in which application development does not use regular server processes. They rely only on a combination of third-party services (such as Lambda), client-side logic, and service-managed remote procedure calls. SLS applies to event requests (such as IOT) and traffic bursts.

Node is lightweight, low resource cost and easy to deploy. The SLS service of the mainstream cloud service platform is friendly to Node, and the tool is relatively perfect.

We have seen better results using SLS for notification services (event requests) and marketing campaigns (traffic surges), improving development efficiency and reducing costs (compared to using Java).

We will carry out several market activities of different scales for each Q to attract new users or enhance the community atmosphere, such as mini-games, voting, answering questions and so on. Events can range from tens of thousands to millions of people. The early activities were developed by the front end and the Java back end in collaboration. Later, when the Java back end was short of manpower, the front end students used Node to independently complete the front and back end development. Due to the lack of back-end knowledge and experience of the front-end students, the efficiency and quality of the back-end part are slightly inadequate. In addition, traditional server capacity expansion does not cope well with sudden surges in activity. The above factors lead to poor user experience, and the effect of activities is affected to a certain extent. We decided to introduce SLS because of the flexibility of activities, uncomplicated business logic, surging traffic, and current developer staffing. We have encapsulated an SLS shelf, so front-end students only need to pay attention to the business part and realize some basic back-end system functions, without considering tedious non-business problems such as deployment and expansion.

Node is not an ideal or perfect SLS choice: we don’t seem to be using high I/O concurrency, which is one of the main advantages of Node. AWS Lambda performance looks something like this: Go > Python3 > node.js. For more technical details, check out our online review.

Event Loop

Event Loop

Node.js ?

Node is like durian. Those who like it can’t stop, while those who dislike it may not be able to accept it.

Node is less used in the back-end field in China and more abroad, and it is still relatively small in general. Node may not be as successful and widely used as it should be. Node is not the best programming model, but it makes sense to explore Node.

Node makes it easy and efficient to develop high-performance services and fun to program. Node encapsulates a lot of the underlying stuff, helping us solve a lot of complicated problems. But it also leads to a lot of developers having little access to the bottom layer and a lack of understanding of it.

Node shines in the front end field and is almost standard, greatly improving the efficiency and quality of front-end engineering. Serverless has taken off in recent years, and Node has grown in popularity. Especially for front-end students, Serverless is a good choice, whether applied to the front end or extended to the back end.

RY

Node.js

There have been many attempts to run JS on the server, and only Node has succeeded.

RY has been in touch with computers since he was a child. He majored in mathematics for his master’s degree and doctoral degree. He likes the fun of creation. Node goes from an idea to exploration, solving one problem after another. It reflects RY’s ability to find, analyze and solve problems, or has a good calculation thinking.

Hello Go

When Node was developing well, RY suddenly gave up Node and switched to Go.

Go provides a better programming model, introducing Goroutine that takes full advantage of the CPU while reducing the number of threads and thread context switching issues. CPU intensive and I/O intensive scenarios at the same time, have your cake and eat it. In addition to excellent performance, Go also has good development efficiency and is simple and easy to master.

Overall, Go is better in design than Node.js, but that doesn’t diminish the value of Node.js.

Deno

I and the Node. Js

Node just a toy?

Choose between Node, Java, and Go: Each technology has its advantages and disadvantages, and there is no one-size-fits-all technology at the moment. There is no best technology, only the most suitable. As long as it solves a business problem, it’s good technology. Therefore, you still need to make a comprehensive choice according to your own technical situation, business scenarios and language characteristics.

Can Node do large projects: There are many successful cases of using Node for large projects (such as Paypal and Netflix) at home and abroad. I have participated in the development of Node back-end projects of more than a dozen sizes, including payment related systems and systems with daily requests of hundreds of millions. Compared with the back end, Node is widely used in the front or middle layer. Many large factories are using Node to do server-side rendering and data aggregation.

JS cool

JS has a lot of cool syntax and tricks that are fun to use for a while and painful to use later. JS is designed in a very short time, inevitably there are some problems. When I first learned JS in college, I was so overwhelmed by the heavy “JavaScript Authority Guide” that I couldn’t remember anything and once forgot how to write code. Discard the dross and take the essence, forget the complex syntax, and use the good syntax in common use. Try not to use confusing syntax, such as call and apply, which often leads to confusion about what this is. Node also gave the ES standard a big boost, introducing new and useful syntax and embracing new changes, such as class replacing prototype chains and Promise replacing callback.

JS is a dynamically typed language, JS types are ever-changing, and the variables you encounter may not be what you want. You can use TS, or refer to “static dynamic typing” as described in JS Data and Types, to keep things constant.

Node.js Everywhere

Node.js will launch its first official event in China in 2016: Node.js Everywhere.

reference

  • Ryan Dahl Blog
  • Interview with Ryan Dahl, Creator of Node.js
  • The C10K problem
  • The C10M problem
  • The Secret To 10 Million Concurrent Connections -The Kernel Is The Problem, Not The Solution
  • What the heck is the event loop anyway