0Pricing
Node.js Backend Development Bootcamp · บทเรียน

การทำสมดุลโหลดให้แอป Node.js

ทำความเข้าใจวิธีที่ตัวกระจายโหลดแบ่งการรับส่งข้อมูล และกำหนดค่าให้แบ็กเอนด์ Node.js เพื่อเพิ่มความพร้อมใช้งาน

การทำสมดุลโหลดให้แอป Node.js เป็นบทเรียน Node.js Backend Development Bootcamp ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Node.js Backend Development Bootcamp และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Node.js Backend Development Bootcamp มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

What is Load Balancing?

Imagine a popular restaurant with only one chef. If too many orders come in, customers wait, or leave! Load balancing is like adding more chefs and a smart manager to distribute orders efficiently.

In web development, a load balancer distributes incoming network traffic across multiple servers. This ensures no single server becomes a bottleneck, keeping your application responsive.

การทำสมดุลโหลดให้แอป Node.js — ภาพประกอบ 1

Node.js & Scalability Needs

Node.js is known for its non-blocking I/O and efficiency. However, a single Node.js process runs on a single CPU core. To handle more users or heavy loads, you need:

  • Scaling: Run multiple Node.js instances to utilize more CPU cores and memory.
  • High Availability: If one instance fails, others can take over, preventing downtime.

Load balancers are crucial for achieving both scalability and high availability for your Node.js applications.

How Load Balancers Work

Think of the load balancer as a traffic cop. When a request comes in, it doesn't go directly to one server. Instead, it hits the load balancer first.

The load balancer then decides which of the available backend servers (or "instances") is best suited to handle that request and forwards it. The client never knows which specific server processed their request.

Distribution Algorithms

Load balancers use different algorithms to decide where to send traffic:

  • Round Robin: Distributes requests sequentially to each server in the pool. Server A gets the first, Server B the second, then C, then back to A. Simple and fair.
  • Least Connections: Sends the request to the server with the fewest active connections. This is good for servers that might have varying processing times.

Other methods exist, like IP Hash (for sticky sessions) or Weighted Round Robin.

Nginx as a Load Balancer

While hardware load balancers exist, software load balancers are common and flexible. A popular choice is Nginx (pronounced "engine-X").

Nginx is a high-performance web server that can also act as a reverse proxy and load balancer. It's often used in front of Node.js applications to manage traffic and improve performance and security.

Node.js Instance Example

Let's create a simple Node.js server. We'll simulate multiple instances by running this same code on different ports. This server will respond with its port number so we can see which instance handled the request.

Try running this example:

const http = require('http');
const port = process.env.PORT || 3000;

const server = http.createServer((req, res) => {
  res.writeHead(200, { 'Content-Type': 'text/plain' });
  res.end(`Hello from Node.js instance on port ${port}!\n`);
});

server.listen(port, () => {
  console.log(`Server running on port ${port}`);
});

Another Node.js Instance

This is the same Node.js application as before, but imagine it's running as a separate process, perhaps on a different server or just another port (e.g., 3001) on the same machine.

A load balancer would distribute requests between this instance and the one on port 3000.

If you were to run this locally, you'd set the PORT environment variable before running the script (e.g., PORT=3001 node app.js).

const http = require('http');
const port = process.env.PORT || 3001; // Changed default port for instance 2

const server = http.createServer((req, res) => {
  res.writeHead(200, { 'Content-Type': 'text/plain' });
  res.end(`Hello from Node.js instance on port ${port}!\n`);
});

server.listen(port, () => {
  console.log(`Server running on port ${port}`);
});

Nginx Upstream Concept

To load balance our Node.js instances with Nginx, you'd define an upstream block listing your backend servers. Then, a server block would proxy requests to this upstream group.

Here's a conceptual Nginx configuration snippet:

http {
  upstream my_nodejs_app {
    server 127.0.0.1:3000;
    server 127.0.0.1:3001;
  }

  server {
    listen 80;
    server_name example.com;

    location / {
      proxy_pass http://my_nodejs_app;
    }
  }
}

Nginx would listen on port 80 and distribute requests to ports 3000 and 3001 using a Round Robin strategy by default.

More Load Balancer Benefits

Load balancers offer more than just distributing traffic:

  • Fault Tolerance: If a server goes down, the load balancer automatically stops sending requests to it, redirecting traffic to healthy servers.
  • SSL Termination: They can handle SSL/TLS encryption and decryption, offloading this work from your backend Node.js servers.
  • Session Persistence (Sticky Sessions): Can ensure a user's requests always go to the same backend server, which is important for some applications.

Quick Check: Load Balancing

Which of the following are key benefits of using a load balancer for a Node.js application?

Recap: Load Balancing Power

You've learned that load balancing is essential for scaling Node.js applications and ensuring high availability. By distributing incoming requests across multiple backend instances, load balancers prevent bottlenecks and provide fault tolerance.

Tools like Nginx are commonly used to implement software load balancing, offering various algorithms to efficiently manage traffic. This architecture is vital for robust, production-grade applications.

คำถามที่พบบ่อย

บทเรียน “การทำสมดุลโหลดให้แอป Node.js” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การทำสมดุลโหลดให้แอป Node.js” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Node.js Backend Development Bootcamp ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Node.js Backend Development Bootcamp มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การทำสมดุลโหลดให้แอป Node.js”

ทำความเข้าใจวิธีที่ตัวกระจายโหลดแบ่งการรับส่งข้อมูล และกำหนดค่าให้แบ็กเอนด์ Node.js เพื่อเพิ่มความพร้อมใช้งาน คุณปฏิบัติ Node.js Backend Development Bootcamp ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Node.js Backend Development Bootcamp หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Node.js Backend Development Bootcamp บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน

บทเรียน “การทำสมดุลโหลดให้แอป Node.js” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Node.js Backend Development Bootcamp นี้ได้ไหม

ได้ บทเรียน Node.js Backend Development Bootcamp ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. กลยุทธ์การแคชสำหรับ Node.js
  2. การทำสมดุลโหลดให้แอป Node.js
  3. การเพิ่มประสิทธิภาพลูปเหตุการณ์ของ Node.js
  4. การทำโปรไฟล์และการตรวจจับหน่วยความจำรั่ว
← กลับไปที่ Node.js Backend Development Bootcamp