Mastering Node.js - Second Edition
eBook - ePub

Mastering Node.js - Second Edition

Sandro Pasquali, Kevin Faaborg, Glenn Geenen

Buch teilen
  1. 498 Seiten
  2. English
  3. ePUB (handyfreundlich)
  4. Über iOS und Android verfügbar
eBook - ePub

Mastering Node.js - Second Edition

Sandro Pasquali, Kevin Faaborg, Glenn Geenen

Angaben zum Buch
Buchvorschau
Inhaltsverzeichnis
Quellenangaben

Über dieses Buch

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.

Häufig gestellte Fragen

Wie kann ich mein Abo kündigen?
Gehe einfach zum Kontobereich in den Einstellungen und klicke auf „Abo kündigen“ – ganz einfach. Nachdem du gekündigt hast, bleibt deine Mitgliedschaft für den verbleibenden Abozeitraum, den du bereits bezahlt hast, aktiv. Mehr Informationen hier.
(Wie) Kann ich Bücher herunterladen?
Derzeit stehen all unsere auf Mobilgeräte reagierenden ePub-Bücher zum Download über die App zur Verfügung. Die meisten unserer PDFs stehen ebenfalls zum Download bereit; wir arbeiten daran, auch die übrigen PDFs zum Download anzubieten, bei denen dies aktuell noch nicht möglich ist. Weitere Informationen hier.
Welcher Unterschied besteht bei den Preisen zwischen den Aboplänen?
Mit beiden Aboplänen erhältst du vollen Zugang zur Bibliothek und allen Funktionen von Perlego. Die einzigen Unterschiede bestehen im Preis und dem Abozeitraum: Mit dem Jahresabo sparst du auf 12 Monate gerechnet im Vergleich zum Monatsabo rund 30 %.
Was ist Perlego?
Wir sind ein Online-Abodienst für Lehrbücher, bei dem du für weniger als den Preis eines einzelnen Buches pro Monat Zugang zu einer ganzen Online-Bibliothek erhältst. Mit über 1 Million Büchern zu über 1.000 verschiedenen Themen haben wir bestimmt alles, was du brauchst! Weitere Informationen hier.
Unterstützt Perlego Text-zu-Sprache?
Achte auf das Symbol zum Vorlesen in deinem nächsten Buch, um zu sehen, ob du es dir auch anhören kannst. Bei diesem Tool wird dir Text laut vorgelesen, wobei der Text beim Vorlesen auch grafisch hervorgehoben wird. Du kannst das Vorlesen jederzeit anhalten, beschleunigen und verlangsamen. Weitere Informationen hier.
Ist Mastering Node.js - Second Edition als Online-PDF/ePub verfügbar?
Ja, du hast Zugang zu Mastering Node.js - Second Edition von Sandro Pasquali, Kevin Faaborg, Glenn Geenen im PDF- und/oder ePub-Format sowie zu anderen beliebten Büchern aus Informatica & Servizi web e API. Aus unserem Katalog stehen dir über 1 Million Bücher zur Verfügung.

Information

Jahr
2017
ISBN
9781785883033

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...

Inhaltsverzeichnis