Node JS

Node JS

Since its birth in 1995, JavaScript has established itself as the de facto programming language on the client side of a web application. It executes within a web browser and thereby delivers more dynamic content and makes user experience more interactive. However, to execute code on the server side, we had PHP, ASP, Perl, Python, etc.?Until Node.js, there was no popular JavaScript support on the server side.

Node.js is a JavaScript runtime that can be executed on servers, that is, outside a web browser environment. By default, it's based on Google's V8 JavaScript engine. It's open source and cross platform. It's steered by the Node.js Foundation.

Node.js adopts an event-driven architecture and asynchronous I/O. This enables it to scale better, particularly for real-time web apps or apps that do a lot of I/O. The Node.js ecosystem includes thousands of packages and frameworks.

What's the context for the creation of Node.js?

The web was invented based on the client-server model. Clients (such as web browsers) request the server for a resource. The server serves the content as a response. In this model, the server cannot initiate a response. Even if data has changed on the server, it has to wait for the client to give a request. This was alright when users were passive consumers of information.

When social sites such as Facebook and Twitter arrived, everyone became a potential producer of information. There was a rise of chat apps, multi-user gaming and apps for real-time collaboration. We needed a method for servers to push data to clients.?While Flash and Java Applets enabled this, they were isolated environments that used non-standard ports.

Secondly, typical web servers launch a new thread to handle every client request. This means that scalability is limited by memory and the penalty of switching across threads.

Node.js solves these problems by using single-threaded event-driven processing. It offloads I/O tasks to worker threads and processes them asynchronously via callbacks. Ultimately, this makes it easier for us to develop scalable real-time apps.


What sort of applications can benefit from Node.js?

No alt text provided for this image

Node.js is suited for apps that have lots of database accesses or waits on network connections. When there's a need to scale your app to thousands of parallel requests, Node.js does this well without hitting performance limits. On the flip side, Node.js is not suited for CPU-intensive tasks, which will block the main thread.

With Node.js, stream processing of data is possible. Worker threads do this processing.?It's also good for queueing inputs for later batch processing.

Node.js with Socket.IO makes it easy to build real-time apps such as chat apps or games.?It can be used as a proxy to handle many simultaneous connections. Apps with rich real-time dashboards can be created.?Microservices architecture can be implemented with Node.js.

If your codebase spans both client and server concerns, Node.js helps you to write in a single language, JavaScript. This makes it easier to later refactor the code if necessary.?JavaScript is a natural fit for transferring?JSON?data or working with object databases such as MongoDB without the extra processing of encoding or decoding.

How does Node.js work under the hood?

No alt text provided for this image

When a Node app starts, callbacks are registered for events. A?callback?is nothing more than executable code that must be called when the event occurs. After this initialization phase, Node enter the?Event Loop. In this loop, Node waits for events to happen and trigger the appropriate callbacks. It's for this reason Node.js is said to use?event-driven programming?paradigm.

For example, a client requests a web page. Event loop will invoke the associated callback. This is?synchronous: event loop will wait for the callback to complete. However, if the callback requires some data from a database, the event loop being a single thread will get?blocked?since I/O is slower compared to CPU. Other events will start queueing up.

To prevent this, Node.js adopts?non-blocking asynchronous I/O: slow tasks will be handed over to the?Worker Pool. This frees the event loop to move on the next event. When a worker completes its task, it will signal an event, at which point, the event loop will process its callback synchronously.

Could you share more details on the event loop?

No alt text provided for this image

  • Event loop is also called main loop, main thread, or event thread. It's where code executes synchronously, that is, the event loop will wait until execution completes. If there are asynchronous calls to be made, event loop will register their callbacks and handover the tasks to worker pool. The idea is to perform only lightweight tasks on the event loop so that it can handle as many requests as possible. Throughput (requests per second) is an important concern for the event loop.
  • It's important to note that the application and the event loop run within a single thread. All events are processed in the same thread. Pending events are not stored in a single queue. Rather, the event loop is a set of phases, with each phase having events queued for processing.
  • Could you share more details on workers?
  • Worker Pool manages a number of threads that can be used for both I/O-intensive or CPU-intensive tasks. The worker pool maintains a queue of tasks to be processed. When a worker becomes available, it's assigned a task.
  • Both event loop and worker threads make use of?libuv, a multi-platform support library for asynchronous I/O. It's implemented in C.?Since operating systems often provide asynchronous interfaces, libuv will use these in preference to its own thread pool.
  • Since Node v10.5.0, on an experimental basis, it's been possible to create pools, specify the number of workers or have control of one worker communicating with another. This is part of the?worker-threads-pool?package.
  • For my next web app, should I use Node.js or Express.js?
  • Express is a web application framework built on top of Node. You can build a web app with either of these but development will be faster when built with Express. Even better is to adopt the?MEAN?stack. This is a technology stack that combines MongoDB, Express, Angular and Node.
  • Having said this, developers have a choice. Express is not the only Node.js framework. Others include hapi, koa or Sails. For fullstack, Meteor, Feathers or Keystone are alternatives to MEAN.
  • What if you're building a REST?API? In this case, performance is important. Express may be too heavy and slow for building REST?API?endpoints or an?API?gateway. You could use Node directly but there are Node frameworks that are minimalistic and optimized for REST?APIs: fastify, restana, restify, koa, polka.
  • What are some success stories with Node.js adoption?
  • Paypal built a Node.js app in half the time with 33% fewer lines of code compared to its Java codebase. LinkedIn's mobile app using Node.js backend is 20x faster and uses only 3 servers compared to 30 that Ruby on Rails needed. At Netflix, Node.js has reduced app startup time by 70%. Groupon web pages are 50% faster with Node.js compared to Rails. They can also serve much higher traffic. At GoDaddy, they now require 10x fewer servers and time-to-first-byte is down to 12ms from 60ms.
  • Uber is able to process 2 million remote procedure calls per second, thanks to Node.js. Medium is now able to deploy faster. At NASA, by moving to a single database and Node.js they reduced access times by 300%.

要查看或添加评论,请登录

社区洞察

其他会员也浏览了