-
Notifications
You must be signed in to change notification settings - Fork 2.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement node:cluster
module
#2428
Comments
We haven't implemented the Node.js |
node:cluster
module
Is there currently any other way to do multiprocessing/multithreading with bun? This seems like it should be a very high priority item to support. |
+1 on this. The lack of |
|
I got indeed:
I was very surprised to see this error. EDIT: I know |
I ran bun --bun run dev to run my nextJS app. And I got this exact error. |
Well, |
+1 |
For now (Bun v.1.0.3), I can imitate the behavior of the spawn.ts import os from "node:os";
const numCPUs = os.cpus().length;
for (let i = 0; i < numCPUs; i++) {
Bun.spawn(["bun", "index.ts"], {
stdio: ["inherit", "inherit", "inherit"],
env: { ...process.env },
});
} index.ts Bun.serve({
port: 8080,
reusePort: true, // allow Bun server instances to use same port
fetch(req: Request) {
// ... run |
I performed the test and observed that the process ID remains the same every time I access the endpoint and print it. It appears that the access point is not redistributing properly. if (process.pid) {
console.log(process.pid);
} index.ts const server = Bun.serve({
port: 3000,
// @ts-ignore
reusePort: true, // allow Bun server instances to use same port
fetch(request) {
if (process.pid) {
console.log(process.pid);
}
return new Response("Welcome to Bun!");
},
});
console.log(`Listening on localhost:${server.port}`); cluster.ts import os from "node:os";
const numCPUs = os.cpus().length;
for (let i = 0; i < numCPUs; i++) {
Bun.spawn(["bun", "index.ts"], {
stdio: ["inherit", "inherit", "inherit"],
env: { ...process.env },
});
} |
Sorry, I don't know how load balancing works in Bun. To fulfill my curiosity about Bun server performance, I have tested the script in the TechEmpower Framework Benchmark, and the result is amazing. Bun is able to serve up to 2.7M requests per second for plain text test.
|
@masfahru, you're right. I've tested in a linux machine and works like a charm. |
@calebeaires Would you mind sharing the details on what hardware/setup was used to achieve those numbers? |
On M1 Mac OS 8 cores, it does not work. On linux, Ubuntu 22, 2 cores I could get a good result. Sending concurrent requests to Bun server that console log the process.id numbers, we get different results. |
THIS! |
This comment was marked as duplicate.
This comment was marked as duplicate.
+1 I Can't fully convert my node apps to bun without this feature. |
This comment was marked as duplicate.
This comment was marked as duplicate.
Can we please keep this thread productive, such as discussing potential implementations? |
I'm looking forward to seeing this feature implemented in Bun. |
+1 for this. I am also recommend implementing This is the Node.js Suggestion recommendation. nodejs/node#48350
|
Understood, and yes you'd need to handle that externally (nginx, possibly pm2 would be another option but not sure) Another approach we have done in the nodejs world is have a single process handle the raw port, then workers doing what's needed. I've modified my comment to say some rather than most |
+1
The use-case would still be to reinforce Bun's statement of being a "drop-in" replacement for Node, since most of the functionality that entails the cluster module is apparently already implemented, I hope some of the code can be reused for a node:cluster module (mock?). |
Just for any other MacOS user like me coming to this thread to find out why Not sure if Darwin will ever implement this or not. So for the time being MacOS users will need to put a proxy in front of their Bun servers to support multiple process request handling. |
Would be great to see support sometime soon! |
For a lot of us running regular servers, like ec2 instances, we cannot run Bun in production due to this. Yes its possible to rewrite your app to use bun.serve() or rearchitect it to run on single threaded containers, but thats not really a drop in replacement as is claimed. Given its the 12th most thumbs up'd issue out of ~2k, can it be added to the roadmap? |
This is power of the nodejs. I'll waiting node:cluster implemetation for migrate my all servers to bun. |
What if you need clustering for tasks other than serving http requests ? Clustering is important for non web tasks, i usually spawn workers on server side for CPU intensive tasks, nothing related to serving at all. |
We are planning to implement node:cluster towards the end of this month. |
Awesome !!! I will spread the word :) |
cool! great news |
I tried the latest bun 1.1.9. And it seemed not support currently. Hope next release could support it.
|
Hello has anyone successfully been able to use pm2 with bun compiled node binary? i've tried many different commands and variations but cannot figure it out. any help would be greatly appreciated. |
is it out? |
I assume that as soon as node:cluster is implemented, bun will also run with pm2 in cluster mode out-of-the-box. At least I hope so ;-) |
+1 if you are here in June or later, waiting for it to be implemented 😄 |
I need using clusters for s42-core. |
Initial support for the |
Magnificent… thanks to @nektro 👏 Now I can finally go into production with my project 🤩 |
Thank you bro! |
It's happening! Thanks! It would be nice if you provide somewhere in Documentation prons and cons of usage |
I guess also provide benchmarks of normal mode, cluster, reusePort will be great) |
Not sure it's up to the Bun team to recommend one way or another (cluster vs reusePort) but happy to share our experience. We have been running two nodejs applications in production that rely upon the cluster functionality to spin up numbers of workers, each with high volume websocket servers (using the websocket module, not ws for performance). One of these applications also serves http/s content. These applications would happily service 16k concurrent connections and traffic per worker on nodejs. |
Internally, cluster relies on reusePort: true, so it's mostly helpful for automatic restarts and integration with existing codebases relying on cluster |
What version of Bun is running?
0.5.7
What platform is your computer?
Linux 6.2.0-76060200-generic x86_64 x86_64
What steps can reproduce the bug?
bun i rate-limiter-flexible
import { RateLimiterMemory } from "rate-limiter-flexible";
const rateLimiter = new RateLimiterMemory({
points: 5,
duration: 120,
});
What is the expected behavior?
No response
What do you see instead?
No response
Additional information
No response
The text was updated successfully, but these errors were encountered: