Why should I use Node. js? Case Study

Source: Internet
Author: User
Tags nginx reverse proxy stock prices

Introduction

The popularity of JavaScript has brought about many changes, so that the form of using it for network development has become completely different. Just like in a browser, we can also run JavaScript on the server, from the frontend to the backend. This huge contrast is hard to imagine, just a few years ago, Javascript was embedded as a web page in a sandbox as a Flash or Java applet.

Go deep into Node. before using JavaScript, you may need to read and understand the benefits of using cross-stack JavaScriptJavaScript into ss the stack. It unifies the programming language and data format in JSON ), this allows you to reuse developer resources in the best way. Because this is more about the characteristics of JavaScript, we will not discuss it here. But it is indeed a key advantage for developers to use Node in the development process.

As Wikipedia says: "Node. js is a packaging shell that combines the Google V8 engine, the libuv abstraction layer, and the core library written by the main body using Javscript ." In addition, it is worth noting that the goal of Node. js author Ryan Dahl is to create a website with real-time pushing capabilities. In Node. js, He gave developers an excellent solution to achieve asynchronous development using event-driven. Note: V8 is developed by Google and is currently recognized as the fastest Javascript parsing engine. libuv is an open-source cross-platform asynchronous IO library customized for Node .)

In short: Node. js uses WebSocket-based push technology in real-time Web applications. What kind of revolution does this mean? Well, after more than 20 years of stateless Interaction Based on the stateless request-return mechanism, we finally have a real-time, bidirectional connection web application, both the client and the server can initiate communications and exchange data freely. In contrast to the traditional web response mode, the Client Always actively initiates communications and the server passively returns responses. In addition, these are based on open Web Components HTML, CSS, and JS running on the standard port 80 ).

It may be said that we have been using Flash and Java applets for many years-but in fact, these methods only use the network to transmit data to the sandbox environment on the client. They are all isolated and often operate non-standard ports that require additional permissions.

With its unique advantages, Node. js has now played a key role in the products of many famous companies.

In this article, we will not only discuss how these advantages are achieved, but also why you use Node. js to replace some classic Web application models.

How does Node. js work?

The main idea of Node. js is to use non-blocking, event-driven I/O operations to maintain lightweight and efficient processing of cross-platform (distributed SS distributed devices) data-intensive real-time applications. This sounds a bit difficult.

Its true meaning is that Node. js is not a silver bullet-level platform that will take the lead in the world of Web development. On the contrary, it is a platform that meets special requirements. You certainly do not want to use Node. js for CPU-intensive operations. In fact, using it for heavy computing means abandoning almost all the advantages of Node. The real highlight of Node is the construction of high-performance, highly scalable Internet applications-because it can handle massive and high-throughput concurrent connections.

Its working principle is quite interesting. The traditional network service technology generates a new thread for each new connection request. This new thread will occupy the system memory and eventually occupy all available memory. Node. js only runs in a single thread and uses non-blocking asynchronous I/O calls. All connections are processed by this thread. In addition to libuv, allows tens of thousands of concurrent connections to be stored in the event loop of the thread ).

Make a simple calculation: Assuming it is a common Web program, a new connection will occupy 2 MB of memory and run on an 8 gb ram system, calculate the context switching cost between threads, and the maximum theoretical value of concurrent connections is 4000. This is the processing situation under the traditional Web server technology. Node. js has reached the extended level of about 1 M concurrent connections (related proof ).

Of course, all client requests share a single thread, which is also a write Node. potential defects of js applications. first, a large amount of computing may temporarily cause Node's single thread to lose response, and cause all requests from other clients to be blocked until the computing ends. Second, developers need to be very careful not to let an Exception block the core event loop, because this will lead to the termination of the Node. js instance is actually a program crash ). Note: For example, hanging a page in PHP will not affect website running, but Nodejs is a thread and a thread to process all links, therefore, all other links may be affected whether the computing is stuck or blocked due to exceptions. The solution will be discussed later .)

The method used to prevent process interruption when an exception is thrown is to pass the exception through callback rather than throw them, just like in other environments ). Even if some unprocessed exceptions block the program, there are still many solutions to this problem, there are also many policies and tools that can be used to monitor the Node process to execute the necessary crash recovery policies and tools, although you will not be able to restore the user's Session), the most common is to use the Forever module, or use other external system tools such as upstart and monit.

NPM: The Node Package Manager

When we discuss Node. js, a default built-in module management tool, NPM, should never be ignored. It is inspired by Ruby Gems and provides version and dependency management functions, allowing you to easily install Reusable Component Management tools through online databases ).

For a complete list of public modules, you can find https: // npmjs.org/on the NPM website, or ask by using the npm cli tool installed with Node. js. The ecosystem of this module is open to everyone. Anyone can publish their own modules. All modules can be found in the NPM database. You can find a brief introduction to npm on the http://howtonode.org/introduction-to-npm page that is somewhat old, but still can see ).

Some popular NPM modules include:

  • Express-Express. js is a concise and flexible node. js Web application framework, which is now the majority of nodes. the standard framework of js applications, you can already find. js books see it.
  • Connect-Connect is a Node. js HTTP service expansion framework that provides a high-performance "plug-in" set. It is famous for its middleware and is one of the basic parts of Express.
  • Socket. io and sockjs-currently the two most popular websocket components on the server side.
  • Jade is one of the popular template engines and is the default template engine of Express. js. It was inspired by HAML.
  • Mongo and mongojs-encapsulate various MongoDB APIs, but I usually recommend mongoose for my work.
  • Redis-Redis client function library.
  • The coffee-script-CoffeeScript compiler allows developers to use Coffee to write their Node. js programs.
  • Underscore (lodash, lazy)-the most popular JavaScript tool library for Node. js package encapsulation, and two different implementation methods to achieve better performance in the same industry.
  • Forever-probably the most popular tool used to ensure the continuous running of node scripts.

There are still a lot of good modules, so I will not list them here. I hope they will not offend me ).

Where should Node. js be used?

Chat

Chat is the most typical application of real-time interaction among multiple users. Since IRC, many open-source or non-open-source protocols run on non-standard ports. Now, using Node. js can solve these problems-running WebSockets on standard port 80.

Chat applications are examples that best reflect the advantages of Node. js: lightweight, high-traffic, and good response to running-intensive data on cross-platform devices, although with low computing power ). At the same time, chatting is also a case worth learning, because it is very simple and covers most of the solutions used by a typical Node. js so far.

Let's try to figure out how it works.

In the simplest case, we have a chat room on our website, where users can send messages, of course, in a one-to-many format. For example, assume that a total of three people are connected to our website.

On the server side, we use Express. A simple site built by js. This site implements two things. 1) when processing a GET request with a path, sending a message board and a page for sending the message 'send' button 2) a listener client sends a new message to the websockets service.

On the client side, we have an HTML page with two js methods. One is the "send" button used to trigger the event. This will send the input message through webscoket, another method is to use webscoket to listen to messages sent by the server on the client, for example, messages sent by other users ).

When a client sends a message, the following occurs:

This is the simplest example. For better solutions, you can use the Redis database for a simple cache. In a more advanced solution, you may need a message route to specifically process message queues and a more robust sending mechanism, for example, when sending a message, it overwrites offline users at the time of sending or stores unreceived messages for offline registered users. However, no matter what improvements you have made, Node. js will follow a basic principle: Responding to events, handling multiple concurrent connections, and maintaining a mobile user experience.

OBJECT Database Interface api on top of an object db)

Although Node. js is really good at real-time interaction applications, it is also very suitable for querying data such as MongoDB through object database (object DB ). Data stored in JSON format can be processed directly by Node. js without the need for data conversion and matching.

For example, if you are using Rails, you will convert the JSON data into a binary model. when the data is called by Backbone. js, Angular. js or jQuery AJAX, it will be converted back to JSON. For Nodejs, you can use a rest api to export JSON objects for the client. In addition, if MongoDB is used for reading and writing data from a database, you do not have to worry about the format between JSON and any data. In short, you can avoid the problem of multi-dimensional data conversion, whether on the client, server, or database.

Queue Input

If you are receiving a high amount of concurrent data, your database may become a bottleneck for your processing. As described above, Node. js can easily process concurrent connections. However, because database operations are a blocking operation in this case), this is troublesome. The Node. js solution is to acknowledge that the client data is authentic before the data is actually written.

In this way, the system continues to maintain its response during high load, which is particularly useful when the client does not need to strictly confirm whether a data is successfully written. Typical examples include: log records or user-tracking data records, which are processed in batches and used later. They also include eventual consistency so, which is often used in NoSQL) acceptable. operations that do not require immediate response, such as the number of likes updated on Facebook ).

Data enters the queue through some caching or basic components of the message queue, such as RabbitMQ and ZeroMQ, and is digested one by one through an independent database batch writing process, or it can be processed through a higher-performance computing-intensive backend service. Other languages/frameworks can also achieve similar operations, but under the same configuration, nodejs's high throughput and high concurrency cannot be achieved.

Simply put: With Node, you can throw Database Operations aside and process them later. If they are successful, they can continue to be executed. Note: during development, the time-consuming operations are usually processed asynchronously using the callback function, and the main thread continues to execute)

Data Stream

On a traditional network platform, HTTP requests and responses are more like isolated events. However, they are all data streams. This observation result can be used to build some cool functions on Nodejs. Because data is received as a stream, we can process files being uploaded online on the website. In this way, real-time audio and video encoding can be implemented, and code proxy between different data sources can be found in the next section ).

Note: Node replaces webserver such as apache to process data. Therefore, developers can directly receive the uploaded data from the client and process it in real time. The above paragraph sounds a little abstract, but you can simply imagine that you do not need to enable YY or QQ to enable voice and video functions when you open the webpage .)

Proxy

Node. js can process a large number of concurrent connections asynchronously, so it is easy to use as a proxy for the server. This is especially useful when proxy is performed between services with different response times or when data from multiple sources is collected.

For example, consider a server application communicating with third-party resources to update data from different sources, or store some images and video resources on the server to a third-party cloud service.

Although the dedicated Proxy Server does exist, if you do not have a dedicated proxy server, or you need a locally developed solution, using Node as the proxy may be a better choice. For this solution, I mean that when you are developing, you can use Node. set up a service in the js development environment to handle requests for resources and proxies. in the production environment, you can use dedicated proxy services such as nginx and HAProxy) to process these interactions.

Stock operator dashboard

Let's continue to discuss the application. Real-time network solutions can easily implement securities trading software-used to track stock prices, perform calculations, perform technical analysis, and generate reports.

Using a real-time web-based solution will allow traders to easily switch their work software and locations. We believe that we will soon see them on Florida, ivisa, or a beach in Bali.

Application Monitoring Instrument disk table

In another common case, Node + Web + Socket is ideal for tracking website visitors and visualizing real-time interaction between them. If you are interested, you can check out Hummingbird)

You may need to collect the real-time status of users, or even open a communication channel when they reach a specific point in the channel, and move to the next stage through targeted interaction introduction. if you are interested, we recommend you look at CANDDi)

Imagine how much improvement your business will bring if you know the real-time operations of your visitors and can visually view their interactions. With the real-time and bidirectional socket communication of Node. js, you can now do it.

System Monitoring Instruments

Now let's look at the infrastructure of things. Imagine an SaaS operator who wants to provide service monitoring pages for its users, such as the status pages on GitHub. Through the Node. js event loop, we can create a powerful Web-based dashboard, asynchronously check the service status, and use WebSockets to push data to the client.

Internal company) and the status of public services can be reported in real time using this technology. Let's extend this idea far, and try to imagine the monitoring application of a network operation center NOC in a telecom operator, cloud/Network/Server Operator, or some financial institutions, all run on this Node. applications composed of js and WebSocket, rather than Java and/or Java Applet.

Note: do not try to use Node to create a hard real-time system, that is, a system with consistent response time requirements ). Erlang may be a better choice for such applications.

Where can I use Node. js?

Server WEB Application

Using Express. js through Node. js can also be used to create typical web applications on the server. However, although it is possible that using Node. js to present HTML in the form of request + response is not the most typical case. Some people agree and others disagree with this practice. Here are some opinions for your reference:

Advantages:

  • If you do not need CPU-intensive computing, you can use Javascript For Development from start to end and even databases such as MongoDB. This significantly reduces the development process, including costs ).
  • For a single-page application or websocket application that uses Node. js as the server, crawlers can receive a completely HTML-rendered response, which is more SEO-friendly.

Disadvantages:

  • Any CPU-intensive computing will impede the response of Node. js, so using a multi-threaded platform is a better way. Alternatively, you can try to scale out the computation [*].
  • Node. js is still very painful to use relational databases. For details, see the following ). Please, if you want to perform relational data operations, consider other environments: Rails, Django, and even ASP. net mvc ....

*] Another solution is to create a highly scalable MQ-supported environment and backend processing for these CPU-intensive computing, to keep Node as a front-end specialist to process client requests asynchronously.

Node. js should not be used somewhere

Server WEB applications using relational databases

Compared with Express. js and Ruby on Rails on Node. js, do not hesitate to select the latter when using relational databases.

Node. js's relational database tool is still in its early stages and is not yet mature enough to make people use it happily. At the same time, Rails comes with Data Access Components, along with DB schema migration support tools and some Gems, which are similar to the treasure tools and ruby gems packages ). Rails and its partner frameworks have a well-known and proven activity Record) or Data mapping Data Mapper) implementation of the Data access layer, these are the things you really want to use when using JavaScript to copy these applications.

However, if you really prefer to use all JS and make preparations that may be crazy), continue to pay attention to sequencee and Node ORM2, although the two are still immature, but they will eventually catch up.

[*] Using Node as the frontend and Rails as the backend to connect to a relational database is entirely possible and not uncommon. Note: There is a foreign saying that PHP programmers can also count as front-end programmers)

Heavy server computing and Processing

When a large amount of computing is involved, Node. js is not the best solution. You certainly do not want to use Node. js to create a Fibonacci number computing service. Generally, any CPU-intensive operation will weaken the advantage of Node in throughput caused by event-driven and asynchronous I/O models, this is because any incoming request will be blocked when the thread is occupied by non-asynchronous high computing workload.

As mentioned above, Node. js is single-threaded and only uses a single CPU core. As for the multi-core concurrent processing on the server, the Node's core team has done some work in the form of the cluster module (refer to: http://nodejs.org/api/cluster.html ). Of course, you can also easily run multiple Node. js server instances through the nginx reverse proxy to avoid single thread blocking.

With regard to cluster, you should transfer all heavy computing to the background processes written in a more appropriate language for processing, and allow them to communicate through the Message Queue Server like RabbitMQ.

Even if your background processing may initially run on the same server, this method has a very high scalability potential. These background processing services can be easily separated as separate worker servers without configuring the load of the entry web server.

Of course, you can use the same method on other language platforms, but use Node. js you can get a high throughput. Every request is processed very quickly and efficiently as a small task. We have discussed this.

Conclusion

We have discussed Node. js from theory to practice, from its goals and ambitions to its advantages and disadvantages. 99% of Node. js development problems are caused by misuse of blocking operations.

Remember: Node. js has never been created to solve large-scale computing problems. It is designed to solve large-scale I/O problems, and is very good at this point.

To sum up, if your project requirements do not contain CPU-intensive operations and you do not need to access any blocked resources, you can use the Node. with the advantages of js, you can enjoy fast and scalable network applications.

Http://blog.jobbole.com/53736/.

Related Article

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.