Most active commenters
  • wkyleg(4)
  • idontwantthis(3)

3 points wkyleg | 14 comments | | HN request time: 1.743s | source | bottom

Right now, JavaScript scales well with a single-threaded event loop. Certainly not as fast as something like Go for async tasks, but enough to power much of the web and be easy to write.

Why hasn't anyone abstracted the event loop model to scale across multiple machines or utilize modern processors? Perhaps with something more like an Actor model or Erlang's BEAM?

It seems like just getting the JavaScript concurrency model as an abstraction over multicore or multi-machine concurrency would be one of the easiest ways to achieve this. I realize that this is still technically difficult, but programming tends towards "just porting things to JavaScript." I would love to have something like Phoenix framework, just built with JavaScript/TypeScript, and I can scale a back end by bumping size of a machine or scaling horizontally.

1. hehehheh ◴[] No.42176945[source]
Its a good question. There are worker threads in Node but they seem clunky to use:

https://bitsfactory.lilanga.me/posts/nodejs-utilize-multi-co...

Stateless webservers are easier, since this is merely loaf balancing. Just run more processes:

https://stackoverflow.com/questions/2387724/node-js-on-multi...

2. toast0 ◴[] No.42177015[source]
I'm not in the node ecosystem, but can't you "just" use node worker-threads for multicore, and run node on multiple machines for multi-machine?

If you want the features of BEAM/dist plus running some javascript, I'd suggest you build your coordination layer in a BEAM language and have some glue to run javascript as a spawned port, or possibly connect node as a c_node to dist.

replies(1): >>42177229 #
3. idontwantthis ◴[] No.42177030[source]
I think if you want that, JavaScript isn’t the right tool. The single threaded simplicity is a big part of why it is a useful tool. You can always spin up external processes from within your app, or use a load balancer or queue to share work with multiple identical processes.

The idea of just horizontally scaling up a node process wouldn’t make a lot of sense. How would you share scope between the different processes for example? You would need a whole new construct, at which point you’re really throwing away the advantages you had and you should probably be using a different language.

replies(1): >>42177210 #
4. wkyleg ◴[] No.42177210[source]
Agreed, I realize it would need to be effectively another language, or at least a very different implementation.

This isn't to far off from what new projects like Deno and Bun are doing though, apart from also needing to spread the event loop implementation horizontally

replies(1): >>42177254 #
5. wkyleg ◴[] No.42177229[source]
Nobody really uses multithreading for node. I mean I'm some projects do (maybe pnpm?) but the threading support isn't great and the event loop performs well.
replies(1): >>42183767 #
6. idontwantthis ◴[] No.42177254{3}[source]
Can you point me to what you’re talking about in Deno? That’s really interesting.
replies(1): >>42177322 #
7. wkyleg ◴[] No.42177322{4}[source]
Deno doesn't implements this differently, it's just an alternative server side run time. It has some better defaults compared to node though
replies(1): >>42177377 #
8. idontwantthis ◴[] No.42177377{5}[source]
Oh I thought you meant they were working on horizontal scaling. I think this is very far off from what they are doing. It’s still Javascript, and you can take for granted that it follows the Ecmascript spec even if its runtime is different.
9. lunarcave ◴[] No.42179333[source]
People used to fork the JavaScript process according to the number of cores present, but it’s less popular now because most infrastructure is provisioned by vCPUs.

So people just provisioned for vCPU=1.

Node.js (v8) already offloads io tasks to their own threads so it’s already horizontal to some extent.

10. pier25 ◴[] No.42183767{3}[source]
Almost everyone running Node in a machine with multiple cores is using multithreading.

Node is multithreaded by default. I believe the default setting is using 4 threads. Most of Node is written in C++.

The JS code written by end users is single threaded (most of it at least) but IO etc is all executed with libuv.

https://docs.libuv.org/en/v1.x/threadpool.html#threadpool

11. pier25 ◴[] No.42183920[source]
Libuv which powers Node can execute multiple event loops (one per thread).

https://docs.libuv.org/en/v1.x/design.html

You'd need to write your app code in C++ which isn't very popular in web dev.

12. ActorNightly ◴[] No.42186591[source]
If you are talking about nodejs, there is https://nodejs.org/api/child_process.html

Same way that python does parallelism, by relying on the underlying OS to schedule threads and use multiple cores.

For JS running in browser, you probably don't want any allowance for such scheduling in the scripts, and let the JS engine in the browser automatically establish parallelism if needed.

13. austin-cheney ◴[] No.42198080[source]
First, let's understand what the event model is. The event model allows for a single process to use multiple call stacks. This is an example of concurrency not parallelism.

Second, when people talk about multiple threads they are typically talking about SMP, or simultaneous multi-processing. JavaScript already does this. It uses WebWorkers in the browser and clusters in Node. Each of those technologies can then coordinate task execution through messaging, such as IPC. Detached-state execution allows for child processes to execute with process independence from the calling process which means killing the calling process will not terminal the child process.

Third, when people talk about multiple machine execution they are typically talking about task distribution, which is not the same as decentralization. Distribution is similar to detached-state SMP but reliant upon network access for task distribution and status.

You can achieve all this with JavaScript right now. I have done it in a personal application built around decentralization and remote file system management.