Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

How to balance the load with NGINX and multiple servers#5396

Unanswered
El-Tommy asked this question inQ&A
Discussion options

I have a server running with PM2, which lets me use all the machine’s vCPUs (02) to get better Socket.IO performance. This server has 4GB of RAM.

Since I’m using PM2, I have an NginX server in front that receives HTTPS/WSS requests and forwards them to port 8080.

I use the configuration below to allow the nodes to communicate correctly, as per the instructions on thewebsite.

const http = require("http");const { Server } = require("socket.io");const { createAdapter } = require("@socket.io/cluster-adapter");const { setupWorker } = require("@socket.io/sticky");const infoServer = {    success: true,}const httpServer = http.createServer((req, res) => {    res.writeHead(200, { "Content-Type": "application/json" });    res.write(JSON.stringify(infoServer));    res.end();});const io = new Server(httpServer, {    cors: {        origin: "*",        methods: ["GET", "POST"]    }});io.adapter(createAdapter());setupWorker(io);

However, in my current scenario, it hasn’t been a good solution, especially for scalability.
If an issue occurs on the server—such as exhausting all RAM or hitting a file limit (tuning has already been done)—the entire service goes offline, so I’m thinking of using another machine, also with 2 vCPUs, to load-balance and provide high availability.

The scenario would be:

Nginx server:
-> Socket.IO Server 01 + 2 vCPUs running
-> Socket.IO Server 02 + 2 vCPUs running

But my question is what I need to change in my code for this to work.
From what I understand, in NginX I need to add the code below.

upstream nodes {    # enable sticky session with either "hash" (uses the complete IP address)    hash $remote_addr consistent;    # or "ip_hash" (uses the first three octets of the client IPv4 address, or the entire IPv6 address)    # ip_hash;    # or "sticky" (needs commercial subscription)    # sticky cookie srv_id expires=1h domain=.example.com path=/;    server app01:8080;    server app02:8080;    server app03:8080;  }

https://socket.io/docs/v4/using-multiple-nodes/#nginx-configuration

I really didn’t understand this part. If I’m going to use multiple servers, do I have to use only one CPU? Or can I keep using all the CPUs available on the machines?

Should I remove Socket.IO’s sticky mechanism?

Is that it? Could someone give me more details?

You must be logged in to vote

Replies: 0 comments

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Category
Q&A
Labels
None yet
1 participant
@El-Tommy

[8]ページ先頭

©2009-2025 Movatter.jp