Node. Js in actual combat

01 Service Development

Application scenarios of Node.js

General scenario

  • Generic HTTP/RPC interface
  • Generic timed task/queue consumption task
  • Ability to do most of the business logic

Advantage of scene

  • BFF front end glue layer
  • SSR server page rendering
  • Homogeneous Web applications
  • Real-time Communication Service (WebSocket)

Stability guarantee

How to determine the stability of a service

Stability statistics: SLA

  • 3 9:9 9.9% = 8760 * 0.1% = 8760 * 0.001 = 8.76 hours
  • 4 9:9 9.99% = 8760 * 0.0001 = 0.876 hours = 0.876 * 60 = 52.6 minutes
  • 5 9:9 9.999% = 8760 * 0.00001 = 0.0876 hours = 0.0876 * 60 = 5.26 minutes

Performance statistics:

  • Response time RTT
  • Processing capacity per unit time QPS/TPS
  • Concurrency Concurrency
  • Error Rate Error Rate

Statistical indicators of resources

  • CPU Load
  • Memory Usage
  • FD Count file descriptor
  • Disk Read/Write
  • Network Send/Recv

Ensure service stability during development

  • Exception handling
  • Automatic restart
  • Health check

Get metrics using the Node.js API

The node.js internal process module provides apis for some metrics

CPU Usage

  • user cpu time
  • system cpu time

Memory Usage

  • heapTotal/heapUsed
  • external
  • aeeray buffers
  • rss

IO Usage

  • fsRead / fsWrite
  • ipcSent / ipcReceived

Service container metrics can also be obtained through system directives

  • top/htop

  • lsof -p xxx

  • vmstat 1

  • iostat 1

Encapsulate the common real-time reporting metrics in the service framework or container image, collect data and display it through visual kanban, which helps us keep track of the current service status

  • Grafana buried spot kanban

    Grafana: The open observability platform | Grafana Labs

02 Releasing Deployment

Early deployment plan

  • Purchase or lease a server/public IP address

  • Install the operating system, set up the internal network environment, and a series of infrastructure tools

  • Upload the production environment code package through FTP or RSYNC

  • Run the startup command in the corresponding path

    • The early implementation ran as a daemon through Nohup

      nohup `NODE_ENV=production node test.js` 2>&1 &
      Copy the code
    • Run through pM2 / praise related startup scripts

      pm2 start app.js -i max
      Copy the code
  • Purchase a domain name, configure DNS, and reverse proxy to the service

IaaS/PaaS/FaaS development path

IaaSThe infancy of cloud computing

Infrastructure as a Service is a cloud Service vendor that provides consumer processing, storage, networking, and various basic computing resources to deploy and execute various software such as operating systems or applications.

IaaS is the lowest layer of cloud services and mainly provides basic resources. Users can deploy and run processing, storage, network, and other basic computing resources without purchasing network devices such as servers and software. They cannot control or control underlying infrastructure, but can control operating systems, storage devices, and deployed applications.

Virtual host /VPS represents the product

  • AWS EC2
  • Aliyun ECS
  • Tencent Cloud server

The technical implementation

  • Virtual machine (KVM/OpenVZ/the Hyper – V
  • OpenStack
  • Docker

PaaS: Mainstream application hosting distribution mode

Platform as a Service (PaaS) is a cloud computing Service that provides computing platforms and solutions. PaaS provides a software deployment platform (runtime) that abstracts hardware and operating system details and allows for seamless scaling. Developers only need to focus on their own business logic, not the underlying layer.

PaaS stands for product

  • Google AppEngine
  • Heroku
  • AWS Elastic Beanstalk
  • Vercel

The technical implementation

  • Docker Swarm/Docker Swarm
  • Kubernetes
    • Service choreography
    • Elastic expansion and contraction capacity
Paas-based publishing process

Most PaaS platforms provide support for the running of Node.js service. We write the build script (NPM install), startup script (NPM start) and application configuration script (app.yml) according to the Node.js Runtime specification provided by PaaS platform.

When the base Runtime does not meet the requirements of the application, the PaaS platform also supports custom functions and startup commands through Dockerfile

The PaaS and conversation

DevOps (a portmanteal of Development and Operations) is a culture, movement, or practice that values communication and cooperation between “software developers” and “IT Operations technicians”. Build, test, and release software faster, more frequently, and more reliably by automating the software delivery and architecture change processes.

Modern PaaS platforms provide a basic DevOps process that greatly simplifies the process of bringing release tests online by binding Git Branch to automate the integration of release to the Preview environment.

PaaS and automatic scaling capacity

Thanks to the capabilities of Kubernetes and the common PaaS application Runtime, modern PaaS services support defining instance performance, rapidly expanding instances when requests surge, CPU/ memory is tight, and scaling down instances when requests are low and resources are abundant, thereby reducing o&M costs and service expenses. To rapidly expand or shrink the capacity, you need to monitor performance indicators and report data to the Runtime.

ServerlessConcepts and Products

The concept of Serverless

“Serverless computing is a cloud computing execution model in which the cloud provider allocates machine resources on demand, taking care of the servers on behalf of their customers. “

Serverless Optimal configuration: FaaS + BaaS

  • FaaS (Lambda): “Functions as services” is an event-driven computational execution model running in stateless containers where functions leverage services to manage server-side logic and state. It allows developers to build, run, and manage these application packages as features without having to maintain their own infrastructure.
    • AWS Lambda
    • Google Cloud Function
    • The Aliyun function computes FC
    • Tencent Cloud Function
  • BaaS: Backend as a Service (BaaS) enables developers to focus on the front end of an application and leverage it without building or maintaining back-end services.
    • Google Firebase
Limitations of the FaaS implementation (Lambda)

The principle of FaaS charging by volume is difficult to implement in traditional container deployment solutions

  • Container cold start + service start takes seconds
  • Resident instance + standby mode can ensure the efficiency of the first visit, but the demand of charging by volume cannot be met

Node.js implements “high density Deployment” based on VM modules

  • Isolation between functions?
  • Recovery of the loop?

Alternative: WASM/V8 Worker instead of Node.js

  • Deno Deploy
  • Cloudflare Workers
  • Wasm Edge

FaaS vs PaaS

  • FaaS vs. PaaS in terms of development experience
  • The functional model is too simple
  • Writing multiple cloud functions is not engineering friendly
  • Application developers prefer to write/publish a full Node.js WebApp

Jamstack pattern and Vercel exploration: Build PaaS applications into FaaS functions and release them

03 O&M Monitoring

Log burying point and monitoring alarm

  • The log

    • process.stdout / process.stderr
    • send through udp socket
  • Buried point alarm

    • Metrics
    • Span
    • Trace

Online Troubleshooting

Pre-process: Instances are pulled out of the cluster

Before diagnosing production services, pull out the cluster to prevent external users from being affected

Node.js Inspector

Node.js provides the Inspector module, which enables debugging of running services

Inspector also supports a HeapSnapshot/CPUProfile at runtime to check for CPU/ memory problems

Strace and tcpdump: a more general system diagnostic tool
  • Tcpdump captures data that is actually transmitted over the network, which is useful for Web Server development scenarios
  • Strace can clearly output the parameters and return results of each syscall between applications and kernel, and is a universal tool for understanding system calls.
Strace: Check out syscall’s tools
Tcpdump: a universal packet capture tool

Tcpdump is a cross-platform packet capture tool that allows you to see every request packet transmitted on a network device and is available on Windows, Mac, and Linux

Common Filter commands

  • Host /net Specifies the request host/ IP address
  • Port Specifies the requested port
  • DST and SRC are used to specify whether rules are used to specify the source or destination of a packet
  • And and or logical and, or relationships used to combine multiple sets of filtering rules.
Tcpdump: Use the Wireshark to view captured packets

Tcpdump can write captured packets to a file and view the result using the Wireshark