Js 事件系统与 Akka 的参与者模式有什么不同?

我使用 Node.js已经有一段时间了,我认为自己对 Java 非常在行。但是我刚刚发现了 Akka,并立即对它的参与者模式感兴趣(据我所知)。

现在,假设我的 JavaScript 技能与我的 Scala/Java 技能相当,我想把重点放在这两个系统的实用性上。特别是在 Web 服务方面。

据我所知,Node 非常擅长处理许多并发操作。我认为一个优秀的资产管理系统 Node Web 服务将擅长于同时处理多个用户提交的更改(在一个大型、流量大的应用程序中)。

但在读过阿卡演员的故事之后,似乎它也会在同样的事情上表现出色。而且我喜欢把工作量减少到一小块的想法。此外,多年前我曾涉足 Erlang,并爱上了该公司使用的信息传递系统。

我从事的许多应用程序都处理复杂的业务逻辑,我认为现在是时候进行更深入的研究了。特别是升级遗留 Struts 和 C # 应用程序。

无论如何,避免圣战,这两个系统有什么根本的不同?两者似乎都是为了同一个目标。也许阿卡的“自我修复”架构有一个优势。

剪辑

It looks like I am getting close votes. Please don't take this question as a "which is better, node or akka?". What I am looking for is the fundamental differences in event driven libraries like Node and actor based ones like Akka.

17327 次浏览

I didn't yet use Akka, but it seems it's erlang-like but in java. In erlang all processes are like actors in Akka, they have mailboxes, you can send messages between them, you have supervisors etc.

Node.js uses cooperative concurrency. That means you have concurrency when you allow it (for example when you call io operation or some asynchronous event). When you have some long operation (calculating something in long loop) whole system blocks.

Erlang uses preemptive task switching. When you have long loop, system can pause it to run other operation and continue after some time. For massive concurrency Node.js is good if you do only short operations. Both support millions of clients: http://blog.caustik.com/2012/08/19/node-js-w1m-concurrent-connections/ http://blog.whatsapp.com/index.php/2012/01/1-million-is-so-2011/

In java you need threads to do any concurrency, otherwise you can't pause execution inside function which erlang does (actually erlang pauses between function calls, but this hapens with all functions). You can pause execution between messages.

Without going into the details (about which I know too little in the case of Node.js), the main difference is that Node.js supports only concurrency without parallelism while Akka supports both. Both systems are completely event-driven and can scale to large work-loads, but the lack of parallelism makes it difficult in Node.js (i.e. parallelism is explicitly coded by starting multiple nodes and dispatching requests accordingly; it is therefore inflexible at runtime), while it is quite easy in Akka due to its tunable multi-threaded executors. Given small isolated units of work (actor invocations) Akka will automatically parallelize execution for you.

Another difference of importance is that Akka includes a system for handling failure in a structured way (by having each actor supervised by its parent, which is mandatory) whereas Node.js relies upon conventions for authors to pass error conditions from callback to callback. The underlying problem is that asynchronous systems cannot use the standard approach of exceptions employed by synchronous stack-based systems, because the “calling” code will have moved on to different tasks by the time the callback’s error occurs. Having fault handling built into the system makes it more likely that applications built on that system are robust.

The above is not meant to be exhaustive, I’m sure there are a lot more differences.

I'm not sure this is a fair comparison to draw. I read this more as "how does an evented based system compare with an actor model?". Nodejs can support an actor model just as Scala does in Akka, or C# does in Orleans, in fact check out nactor, somebody appears to already be trying it.

As for how an evented system vs. actor model compare, I would let wiser people describe it. A few brief points through about the Actor model:

  • Actor model is message based
  • Actor models tend to do well with distributed systems (clusters). Sure event based systems can be distributed, but I think the actor model has distribution built in with regards to distributing computations. A new request can be routed to a new actor in a different silo, not sure how this would work in event based.
  • The Actor model supports failure in that, if hey cluster 1 apperas to be down, the observer can generally find a different silo to do the work

Also, check out drama. It is another nodejs actor model implementation.