Distributed Systems with Node.js: Building Enterprise-Ready Backend Services

Distributed Systems with Node.js: Building Enterprise-Ready Backend Services

by Thomas Hunter
Distributed Systems with Node.js: Building Enterprise-Ready Backend Services

Distributed Systems with Node.js: Building Enterprise-Ready Backend Services

by Thomas Hunter

Paperback

$55.99 
  • SHIP THIS ITEM
    Qualifies for Free Shipping
  • PICK UP IN STORE
    Check Availability at Nearby Stores

Related collections and offers


Overview

Many companies, from startups to Fortune 500 companies alike, use Node.js to build performant backend services. And engineers love Node.js for its approachable API and familiar syntax. Backed by the world's largest package repository, Node's enterprise foothold is only expected to grow.

In this hands-on guide, author Thomas Hunter II proves that Node.js is just as capable as traditional enterprise platforms for building services that are observable, scalable, and resilient. Intermediate to advanced Node.js developers will find themselves integrating application code with a breadth of tooling from each layer of a modern service stack.

  • Learn why running redundant copies of the same Node.js service is necessary
  • Know which protocol to choose, depending on the situation
  • Fine-tune your application containers for use in production
  • Track down errors in a distributed setting to determine which service is at fault
  • Simplify app code and increase performance by offloading work to a reverse proxy
  • Build dashboards to monitor service health and throughput
  • Find out why so many different tools are required when operating in an enterprise environment

Product Details

ISBN-13: 9781492077299
Publisher: O'Reilly Media, Incorporated
Publication date: 11/24/2020
Pages: 377
Sales rank: 739,307
Product dimensions: 7.00(w) x 9.19(h) x (d)

About the Author

Thomas has contributed to dozens of enterprise Node.js services and has worked for a company dedicated to securing Node.js. He has spoken at several conferences on Node.js and JavaScript, published a book on microservices, and is an organizer of NodeSchool SF.

Table of Contents

Foreword xi

Preface xiii

1 Why Distributed? 1

The Single-Threaded Nature of JavaScript 1

Quick Node.js Overview 6

The Node.js Event Loop 9

Event Loop Phases 10

Code Example 11

Event Loop Tips 14

Sample Applications 15

Service Relationship 16

Producer Service 17

Consumer Service 18

2 Protocols 21

Request and Response with HTTP 22

HTTP Payloads 23

HTTP Semantics 25

HTTP Compression 26

HTTPS/TLS 29

JSON over HTTP 34

The Dangers of Serializing POJOs 35

API Facade with GraphQL 36

GraphQL Schema 37

Queries and Responses 38

GraphQL Producer 40

GraphQL Consumer 43

RPC with gRPC 45

Protocol Buffers 45

gRPC Producer 48

gRPC Consumer 50

3 Scaling 53

The Cluster Module 53

A Simple Example 54

Request Dispatching 57

Cluster Shortcomings 58

Reverse Proxies with HAProxy 61

Introduction to HAProxy 63

Load Balancing and Health Checks 64

Compression 69

TLS Termination 70

Rate Limiting and Back Pressure 72

SLA and Load Testing 75

Introduction to Autocannon 76

Running a Baseline Load Test 76

Reverse Proxy Concerns 80

Protocol Concerns 84

Coming Up with SLOs 87

4 Observability 91

Environments 92

Logging with ELK 93

Running ELK via Docker 94

Transmitting Logs from Node.js 95

Creating a Kibana Dashboard 98

Running Ad-Hoc Queries 100

Metrics with Graphite, StatsD, and Grafana 102

Running via Docker 103

Transmitting Metrics from Node.js 104

Creating a Grafana Dashboard 106

Node.js Health Indicators 108

Distributed Request Tracing with Zipkin 111

How Does Zipkin Work? 112

Running Zipkin via Docker 115

Transmitting Traces from Node.js 115

Visualizing a Request Tree 118

Visualizing Microservice Dependencies 119

Health Checks 120

Building a Health Check 121

Testing the Health Check 124

Alerting with Cabot 124

Create a Twilio Trial Account 125

Running Cabot via Docker 126

Creating a Health Check 127

5 Containers 131

Introduction to Docker 133

Containerizing a Node.js Service 140

Dependency Stage 141

Release Stage 143

From Image to Container 146

Rebuilding and Versioning an Image 148

Basic Orchestration with Docker Compose 151

Composing Node.js Services 152

Internal Docker Registry 156

Running the Docker Registry 157

Pushing and Pulling to the Registry 158

Running a Docker Registry UI 160

6 Deployments 163

Build Pipeline with Travis CI 165

Creating a Basic Project 165

Configuring Travis CI 167

Testing a Pull Request 168

Automated Testing 170

Unit Tests 172

Integration Tests 174

Code Coverage Enforcement 177

Deploying to Heroku 183

Create a Heroku App 184

Configure Travis CI 185

Deploy Your Application 187

Modules, Packages, and SemVer 190

Node.js Modules 191

SemVer (Semantic Versioning) 193

npm Packages and the npm CLI 197

Internal npm Registry 204

Running Verdaccio 205

Configuring npm to Use Verdaccio 205

Publishing to Verdaccio 205

7 Container Orchestration 209

Introduction to Kubernetes 210

Kubernetes Overview 210

Kubernetes Concepts 211

Starting Kubernetes 214

Getting Started 214

Deploying an Application 219

Kubectl Subcommands 219

Kubectl Configuration Files 222

Service Discovery 226

Modifying Deployments 232

Scaling Application Instances 232

Deploying New Application Versions 233

Rolling Back Application Deployments 235

8 Resilience 239

The Death of a Node.js Process 239

Process Exit 240

Exceptions, Rejections, and Emitted Errors 242

Signals 247

Building Stateless Services 249

Avoiding Memory Leaks 251

Bounded In-Process Caches 252

External Caching with Memcached 256

Introducing Memcached 257

Running Memcached 258

Caching Data with Memcached 259

Data Structure Mutations 260

Database Connection Resilience 262

Running PostgreSQL 262

Automatic Reconnection 263

Connection Pooling 269

Schema Migrations with Knex 272

Configuring Knex 274

Creating a Schema Migration 275

Applying a Migration 276

Rolling Back a Migration 279

Live Migrations 280

Idempotency and Messaging Resilience 284

HTTP Retry Logic 286

Circuit Breaker Pattern 289

Exponential Backoff 289

Resilience Testing 293

Random Crashes 294

Event Loop Pauses 294

Random Failed Async Operations 295

9 Distributed Primitives 297

The ID Generation Problem 298

Introduction to Redis 301

Redis Operations 302

Strings 304

Lists 305

Sets 307

Hash 308

Sorted Sets 310

Generic Commands 311

Other Types 312

Seeking Atomicity 313

Transactions 315

Lua Scripting 317

Writing a Lua Script File 318

Loading the Lua Script 320

Tying It All Together 322

10 Security 325

Wrangling Repositories 326

Recognizing Attack Surface 328

Parameter Checking and Deserialization 328

Malicious npm Packages 331

Application Configuration 332

Environment Variables 333

Configuration Files 334

Secrets Management 337

Upgrading Dependencies 339

Automatic Upgrades with GitHub Dependabot 340

Manual Upgrades with npm CLI 342

Unpatched Vulnerabilities 344

Upgrading Node.js 346

Node.js LTS Schedule 346

Upgrade Approach 347

A Installing HAProxy 349

B Installing Docker 351

C Installing Minikube & Kubectl 353

Index 355

From the B&N Reads Blog

Customer Reviews