Mastering Node.js - Second Edition
eBook - ePub

Mastering Node.js - Second Edition

Sandro Pasquali, Kevin Faaborg, Glenn Geenen

Share book
  1. 498 pages
  2. English
  3. ePUB (mobile friendly)
  4. Available on iOS & Android
eBook - ePub

Mastering Node.js - Second Edition

Sandro Pasquali, Kevin Faaborg, Glenn Geenen

Book details
Book preview
Table of contents
Citations

About This Book

Expert techniques for building fast servers and scalable, real-time network applications with minimal effort; rewritten for Node.js 8 and Node.js 9About This Book• Packed with practical examples and explanations, Mastering Node.js contains everything you need to take your applications to the next level.• Unleash the full potential of Node.js 9 to build real-time and scalable applications.• Gain in-depth knowledge of asynchronous programming, event loops, and parallel data processing.• Explore Node's innovative event-non-blocking design, and build professional applications with the help of detailed examples.Who This Book Is ForThis book is targeted at JavaScript developers who want to take an in-depth look at the latest Node.js framework to create faster, scalable, real-time backend applications. Basic JavaScript programming knowledge—and also some previous Node.js development experience—are mandatory to get the best out of this bookWhat You Will Learn• Build an Electron desktop app using Node that manages a filesystem • Explore Streams and understand how they apply to building networked services • Develop and deploy an SMS-driven customer service application• Use WebSockets for rapid bi-directional communication• Construct serverless applications with Amazon Lambda • Test and debug with headless browsers, CPU profiling, Mocha, Sinon, and more• Scale applications vertically and horizontally across multiple cores and web servicesIn DetailNode.js, a modern development environment that enables developers to write server- and client-side code with JavaScript, thus becoming a popular choice among developers.This book covers the features of Node that are especially helpful to developers creating highly concurrent real-time applications. It takes you on a tour of Node's innovative event non-blocking design, showing you how to build professional applications. This edition has been updated to cover the latest features of Node 9 and ES6. All code examples and demo applications have been completely rewritten using the latest techniques, introducing Promises, functional programming, async/await, and other cutting-edge patterns for writing JavaScript code. Learn how to use microservices to simplify the design and composition of distributed systems. From building serverless cloud functions to native C++ plugins, from chatbots to massively scalable SMS-driven applications, you'll be prepared for building the next generation of distributed software.By the end of this book, you'll be building better Node applications more quickly, with less code and more power, and know how to run them at scale in production environments.Style and approachMastering Node.js contains all of the examples and explanations you'll need to build applications in a short amount of time and at a low cost, running rapidly and at scale.

Frequently asked questions

How do I cancel my subscription?
Simply head over to the account section in settings and click on “Cancel Subscription” - it’s as simple as that. After you cancel, your membership will stay active for the remainder of the time you’ve paid for. Learn more here.
Can/how do I download books?
At the moment all of our mobile-responsive ePub books are available to download via the app. Most of our PDFs are also available to download and we're working on making the final remaining ones downloadable now. Learn more here.
What is the difference between the pricing plans?
Both plans give you full access to the library and all of Perlego’s features. The only differences are the price and subscription period: With the annual plan you’ll save around 30% compared to 12 months on the monthly plan.
What is Perlego?
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 1000+ topics, we’ve got you covered! Learn more here.
Do you support text-to-speech?
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more here.
Is Mastering Node.js - Second Edition an online PDF/ePUB?
Yes, you can access Mastering Node.js - Second Edition by Sandro Pasquali, Kevin Faaborg, Glenn Geenen in PDF and/or ePUB format, as well as other popular books in Informatica & Servizi web e API. We have over one million books available in our catalogue for you to explore.

Information

Year
2017
ISBN
9781785883033
Edition
2

Scaling Your Application

"Evolution is a process of constant branching and expansion."
- Stephen Jay Gould
Scalability and performance are not the same things:
"The terms "performance" and "scalability" are commonly used interchangeably,
but the two are distinct: performance measures the speed with which a single request can be executed, while scalability measures the ability of a request to maintain its performance under increasing load. For example, the performance of a request may be reported as generating a valid response within three seconds, but the scalability of the request measures the request's ability to maintain that three-second response time as the user load increases."
- Steven Haines, "Pro Java EE 5"
In the last chapter, we looked at how Node clusters might be used to increase the performance of an application. Through the use of clusters of processes and workers, we learned how to efficiently deliver results in the face of many simultaneous requests. We learned to scale Node vertically, keeping the same footprint (a single server) and increasing throughput by piling on the power of the available CPUs.
In this chapter, we will focus on horizontal scalability; the idea is that an application composed of self-sufficient and independent units (servers) can be scaled by adding more units without altering the application's code.
We want to create an architecture within which any number of optimized and encapsulated Node-powered servers can be added or subtracted in response to changing demands, dynamically scaling without ever requiring a system rewrite. We want to share work across different systems, pushing requests to the OS, to another server, to a third-party service, while coordinating those I/O operations intelligently using Node's evented approach to concurrency.
Through architectural parallelism, our systems can manage increased data volume more efficiently. Specialized systems can be isolated when necessary, even independently scaled or otherwise clustered.
Node is particularly well-suited to handle two key aspects of horizontally-scaled architectures.
Firstly, Node enforces non-blocking I/O, such that the seizing up of any one unit will not cause a cascade of locking that brings down an entire application. As no single I/O operation will block the entire system, integrating third-party services can be done with confidence, encouraging a decoupled architecture.
Secondly, Node places great importance on supporting as many fast network communication protocols as possible. Whether through a shared database, a shared filesystem, or a message queue, Node's efficient network and Stream layers allow many servers to synchronize their efforts in balancing load. Being able to efficiently manage shared socket connections, for instance, helps when scaling out a cluster of servers as much as it does a cluster of processes.
In this chapter, we will look at how to balance traffic between many servers running Node, how these distinct servers can communicate, and how these clusters can bind to and benefit from specialized cloud services.

When to scale?

The theory around application scaling is a complex and interesting topic that continues to be refined and expanded. A comprehensive discussion of the topic will require several books, curated for different environments and needs. For our purposes, we will simply learn how to recognize when scaling up (or even scaling down) is necessary.
Having a flexible architecture that can add and subtract resources as needed is essential to a resilient scaling strategy. A vertical scaling solution does not always suffice (simply adding memory or CPUs will not deliver the necessary improvements). When should horizontal scaling be considered?
It is essential that you are able to monitor your servers. One simple but useful way to check the CPU and memory usage commanded by Node processes running on a server is to use the Unix ps (process status) command, for example, ps aux | grep node. A more robust solution is to install an interactive process manager, such as HTOP (http://hisham.hm/htop/) for Unix systems, or Process Explorer for Windows-based systems (https://docs.microsoft.com/en-us/sysinternals/downloads/process-explorer).

Network latency

When network response times are exceeding some threshold, such as each request taking several seconds, it is likely that the system has gone well past a stable state.
While the easiest way to discover this problem is to wait for customer complaints about slow websites, it is better to create controlled stress tests against an equivalent application environment or server.
AB (Apache Bench) is a simple and straightforward way to do blunt stress tests against a server. This tool can be configured in many ways, but the kind of test you would do for measuring the network response times for your server is generally straightforward.
For example, let's test the response times for this simple Node server:
http.createServer(function(request, response) { 
response.writeHeader(200, {"Content-Type": "text/plain"});
response.write("Hello World");
response.end();
}).listen(2112)
Here's how one might test running 10,000 requests against that server, with a concurrency of 100 (the number of simultaneous requests):
ab -n 10000 -c 100 http://yourserver.com/ 
If all goes well, you will receive a report similar to this:
 Concurrency Level: 100
Time taken for tests: 9.658 seconds
Complete requests: 10000
Failed requests: 0
Write errors: 0
Total transferred: 1120000 bytes
HTML transferred: 110000 bytes
Requests per second: 1035.42 [#/sec] (mean)
Time per request: 96.579 [ms] (mean)
Time per request: 0.966 [ms] (mean, across all concurrent requests)
Transfer rate: 113.25 [Kbytes/sec] received

Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.4 0 6
Processing: 54 96 11.7 90 136
Waiting: 53 96 11.7 89 136
Total: 54 96 11.6 90 136

Percentage of the requests served within a certain time (ms)
50% 90
66% 98
...
99% 133
100% 136 (longest request)
There is a lot of useful information contained in this report. In particular, one should be looking for failed requ...

Table of contents