[GH-ISSUE #1480] Using Node.js's built-in load-balancer in cluster mode for better distribution requests among workers #30551

Closed
opened 2026-07-14 16:49:27 -05:00 by GiteaMirror · 4 comments
Owner

Originally created by @firsturdiev on GitHub (Dec 9, 2025).
Original GitHub issue: https://github.com/imputnet/cobalt/issues/1480

bug description

Currently cobalt uses OS-level port-binding method for workers, in that way OS handles distribution of incoming requests among workers. But due to some OS-scheduling problems, it becomes very unbalanced, even node.js official warns about this:

Image

But in another (default) method, node.js itself handles distribution in round-robin fashion with some minor latency in much reliable way.

The main reason of spawning multiple workers in cobalt is benefiting from equal distribution of requests so in this case using node.js built-in method rather than OS-level port binding looks right approach.

Open to discuss more about this by technical side

source

cobalt's code

Originally created by @firsturdiev on GitHub (Dec 9, 2025). Original GitHub issue: https://github.com/imputnet/cobalt/issues/1480 ### bug description Currently cobalt uses OS-level port-binding method for workers, in that way OS handles distribution of incoming requests among workers. But due to some OS-scheduling problems, it becomes very unbalanced, even node.js [official warns about this](https://nodejs.org/api/cluster.html#clustersetupprimarysettings:~:text=The%20second%20approach%20should%2C%20in%20theory%2C%20give%20the%20best%20performance.%20In%20practice%20however%2C%20distribution%20tends%20to%20be%20very%20unbalanced%20due%20to%20operating%20system%20scheduler%20vagaries.%20Loads%20have%20been%20observed%20where%20over%2070%25%20of%20all%20connections%20ended%20up%20in%20just%20two%20processes%2C%20out%20of%20a%20total%20of%20eight): ![Image](https://github.com/user-attachments/assets/83042965-1770-43b5-a907-6fcf6bb97ec4) But in another (default) method, node.js itself handles distribution in round-robin fashion with some minor latency in much reliable way. The main reason of spawning multiple workers in cobalt is benefiting from equal distribution of requests so in this case using node.js built-in method rather than OS-level port binding looks right approach. Open to discuss more about this by technical side ### source [cobalt's code](https://github.com/imputnet/cobalt/blob/main/api/src/misc/cluster.js)
GiteaMirror added the bug label 2026-07-14 16:49:27 -05:00
Author
Owner

@firsturdiev commented on GitHub (Dec 9, 2025):

I'm in wait-mode of sending pull-request to fix this (just some lines of code), if repo owners interested in this approach of workers. I myself used this method for my own cobalt instance and results were significant

<!-- gh-comment-id:3634317884 --> @firsturdiev commented on GitHub (Dec 9, 2025): I'm in wait-mode of sending pull-request to fix this (just some lines of code), if repo owners interested in this approach of workers. I myself used this method for my own cobalt instance and results were significant
Author
Owner

@dumbmoron commented on GitHub (Dec 9, 2025):

results were significant

Do you have any benchmarks/stats? SO_REUSEPORT afaik should be very efficient, and does not suffer from what is described in OP (it should be actually more efficient than node:cluster balancing, because it does not rely on Node IPC).

<!-- gh-comment-id:3634338007 --> @dumbmoron commented on GitHub (Dec 9, 2025): > results were significant Do you have any benchmarks/stats? `SO_REUSEPORT` afaik should be very efficient, and does not suffer from what is described in OP (it should be actually more efficient than `node:cluster` balancing, because it does not rely on Node IPC).
Author
Owner

@firsturdiev commented on GitHub (Dec 9, 2025):

Do you have any benchmarks/stats?

Don't have benchmarks currently, but after reading about this on nodejs offical docs, I switched to built-in method, latency difference wasn't noticiable, and distibution was very equal (i checked manually with printing worker index next to each request log).

Will share with benchmark test video/proofs soon. Maybe you can also test it manually,

<!-- gh-comment-id:3634407255 --> @firsturdiev commented on GitHub (Dec 9, 2025): > Do you have any benchmarks/stats? Don't have benchmarks currently, but after reading about this on nodejs offical docs, I switched to built-in method, latency difference wasn't noticiable, and distibution was very equal (i checked manually with printing worker index next to each request log). Will share with benchmark test video/proofs soon. Maybe you can also test it manually,
Author
Owner

@firsturdiev commented on GitHub (Dec 9, 2025):

Also one bottleneck about SO_REUSEPORT is that it uses it's own hashing mechanism (ip + client info) for distributing, not round-robin

<!-- gh-comment-id:3634419928 --> @firsturdiev commented on GitHub (Dec 9, 2025): Also one bottleneck about SO_REUSEPORT is that it uses it's own hashing mechanism (ip + client info) for distributing, not round-robin
Sign in to join this conversation.