WebSockets & Realtime Systems Programming · 课时

横向扩展策略

了解如何将 WebSocket 连接分配到多个服务器实例,以提升性能。

第 1 / 4 课11 个步骤

横向扩展策略 是 CoddyKit 上的免费 WebSockets & Realtime Systems Programming 课时。 这是第 1 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 WebSockets & Realtime Systems Programming 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 WebSockets & Realtime Systems Programming 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

Why Scale WebSockets?

Imagine your awesome app suddenly gets super popular! Thousands, even millions, of users want to connect simultaneously.

A single server can only handle so many active WebSocket connections before it gets overwhelmed. It's like a single lane highway trying to handle rush hour traffic!

To keep your app fast and reliable, we need strategies to handle this high traffic.

Grow Up or Grow Out?

When a single server isn't enough, you have two main options to scale:

  • Vertical Scaling: Upgrade your existing server with more CPU, RAM, or faster storage. Think of it as making your single highway lane wider.
  • Horizontal Scaling: Add more servers to share the load. This is like adding more lanes to your highway, or even building parallel highways!

For WebSockets, horizontal scaling is often preferred. It offers better resilience and flexibility.

The Stateful Challenge

WebSockets are different from traditional HTTP requests. While HTTP is often stateless (each request is independent), WebSockets create a stateful, persistent connection.

This means a client and server maintain an open line of communication. If you just randomly send a client to a different server mid-conversation, it won't know what's going on!

This 'state' makes horizontal scaling a bit trickier than with stateless APIs.

Meet the Load Balancer

To distribute traffic across multiple servers, we use a load balancer. Think of it as a smart traffic cop standing at the entrance of your server farm.

Its job is to efficiently direct incoming client connections to one of your available backend WebSocket servers. This prevents any single server from becoming a bottleneck.

WebSocket Handshake & LB

Remember, a WebSocket connection starts as an HTTP request and then 'upgrades' to a WebSocket. Your load balancer needs to understand this process.

It must be configured to correctly handle the Upgrade header in the HTTP request and then maintain the TCP connection for the WebSocket traffic. Without this, the connection won't establish!

Keeping It 'Sticky': Sticky Sessions

Because WebSockets are stateful, it's often important that a client continues talking to the same backend server it initially connected to.

This is achieved using a technique called sticky sessions (or session affinity). The load balancer remembers which server a client used and directs all subsequent requests from that client to the same server.

Common methods include using the client's IP address or a special cookie.

Sticky Session Example

Let's see a simple Node.js WebSocket server. Imagine you have multiple instances of this server running. With sticky sessions, your client would consistently connect to the same server, getting the same 'Server ID'.

To run this: npm install ws then node server.js

const WebSocket = require('ws');
const http = require('http');

const serverId = `Server-${Math.floor(Math.random() * 100) + 1}`; 

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

const wss = new WebSocket.Server({ server });

wss.on('connection', ws => {
  console.log(`Client connected to ${serverId}`);
  ws.send(`Welcome from ${serverId}!`);

  ws.on('message', message => {
    console.log(`Received on ${serverId}: ${message}`);
    ws.send(`Echo from ${serverId}: ${message}`);
  });

  ws.on('close', () => {
    console.log(`Client disconnected from ${serverId}`);
  });
});

server.listen(8080, () => {
  console.log(`${serverId} listening on port 8080`);
});

Sticky Sessions' Limits

While sticky sessions are great for maintaining a client's connection to a single server, they have drawbacks:

  • Server Failure: If the sticky server crashes, the client loses its connection and might need to re-establish state on a new server.
  • Cross-Server Communication: If a client on Server A needs to send a message to a client on Server B, sticky sessions alone won't solve this.

For more complex scenarios, you'll need more advanced strategies, which we'll cover later!

Load Balancer Methods

Load balancers use different algorithms to decide where to send new connections:

  • Round Robin: Sends connections to servers in a rotating order (Server A, then B, then C, then A...).
  • Least Connections: Sends connections to the server with the fewest active connections.
  • IP Hash: Uses the client's IP address to consistently direct it to the same server (ideal for sticky sessions).

The choice depends on your application's needs.

Quick Check: Scaling WebSockets

You've learned about horizontal scaling and the role of load balancers and sticky sessions. Let's test your understanding!

Recap: Scaling Up!

Great job! In this lesson, you learned why horizontal scaling is vital for high-traffic WebSocket applications.

  • Horizontal scaling adds more servers to handle increased load.
  • Load balancers distribute incoming connections across these servers.
  • They must support the WebSocket upgrade process.
  • Sticky sessions ensure a client consistently connects to the same backend server, maintaining its state.

Next, we'll explore how to configure these load balancers effectively!

免费开始

用 AI 导师学习 WebSockets & Realtime Systems Programming — 免费

在浏览器中编写并运行真实代码,获得全天候 AI 导师的即时帮助,并在网页或应用中继续学习。

课程
12
课程
47

常见问题解答

「横向扩展策略」课时是免费的吗?

是的 — 「横向扩展策略」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 WebSockets & Realtime Systems Programming 课程的其余内容,请升级到 CoddyKit PRO。 WebSockets & Realtime Systems Programming 课程共包含 4 节课。

「横向扩展策略」这节课中我会学到什么?

了解如何将 WebSocket 连接分配到多个服务器实例,以提升性能。 你通过在浏览器中直接运行的动手代码来练习 WebSockets & Realtime Systems Programming,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 WebSockets & Realtime Systems Programming 需要有经验吗?

无需任何先前经验。CoddyKit 上的 WebSockets & Realtime Systems Programming 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 1 节课,共 4 节。

「横向扩展策略」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 WebSockets & Realtime Systems Programming 课中编写并运行代码吗?

能。每节 WebSockets & Realtime Systems Programming 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 横向扩展策略
  2. WebSockets 负载均衡
  3. 分布式状态管理
  4. 使用 Redis 构建发布/订阅后端
← 返回 WebSockets & Realtime Systems Programming