TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Node.js: Cluster vs. Async

98 pointsby skazka16almost 10 years ago

18 comments

onestonealmost 10 years ago
This article is wrong on so many levels...<p>- It implies that async execution is equivalent to callback hell. In reality there are excellent ways to have async code which looks just like sync (generators, async&#x2F;await).<p>- It benchmarks multi-core (sync) vs single-core (async) and makes claims based on the results.<p>- It presents async execution as an antipode of clustering. In reality it&#x27;s a best practice to make use of both.<p>...and everything that follows is just irrelevant.
评论 #9587686 未加载
评论 #9587529 未加载
eldudealmost 10 years ago
I don&#x27;t understand these strawmans. The virtue of the asynchronous programming model is low-memory overhead as compared to threads AND low latency for IO-bound tasks in highly concurrent scenarios.<p>Request per-process&#x2F;thread has all the same memory overhead implication it has always had. It&#x27;s almost like the author is ignorant of the reason for node.js&#x27; success or why it was built in the first place.<p>Also, &quot;callback hell&quot; is just FUD. Nobody who does this for a living and knows what they&#x27;re doing really has an issue with this. Promises solve the unreliability issues, and async&#x2F;await solves the syntax complexity issues.<p>I&#x27;d like to see this same analysis for 1000 req concurrency measuring memory overhead and using async&#x2F;await for code comparison. Cooperative multitasking will always be capable of lower latency when you know what you&#x27;re doing, and async programming is lightyears simpler than multi-threaded programming.
评论 #9587207 未加载
kellrosalmost 10 years ago
Edit: The whole idea behind clustering is to run an application instance per thread&#x2F;core for better performance and load balance requests between the application instances. This article seems absurd in its intention to force you to choose between multi-threaded synchronous application instances or a single application instance using callbacks.<p>We&#x27;ve been running a Koa.js API server using Cluster in production for over a year now with no hiccups (on a Windows machine).<p>I&#x27;ve been thinking about making the switch to iisnode, as it handles clustering, graceful shutdown and zero-downtime from within IIS (and does a couple of other things). It uses named pipes to proxy connections and also supports web sockets among other things.<p>With the nodeProcessCommandLine configuration setting, you can pass parameters to node (e.g. --harmony), use babel-node or io.js.<p>See: <a href="http:&#x2F;&#x2F;www.hanselman.com&#x2F;blog&#x2F;InstallingAndRunningNodejsApplicationsWithinIISOnWindowsAreYouMad.aspx" rel="nofollow">http:&#x2F;&#x2F;www.hanselman.com&#x2F;blog&#x2F;InstallingAndRunningNodejsAppl...</a><p>A blog post I wrote a while ago: <a href="https:&#x2F;&#x2F;shelakel.co.za&#x2F;hosting-almost-any-application-on-iis-via-iis-node&#x2F;" rel="nofollow">https:&#x2F;&#x2F;shelakel.co.za&#x2F;hosting-almost-any-application-on-iis...</a>
crueberalmost 10 years ago
Hogwash. It seems like this person doesn&#x27;t understand that node is an event based, asynchronous language, and that&#x27;s one of its big advantages over other languages that force threads, or don&#x27;t generally offer parallel execution.<p>If this had compared Node.js with clustering and async vs a synchronous language like Ruby, it might have been interesting. Maybe. But non-asynchronous operations in Node are an antipattern that core node contribs are trying to remove (the -Sync in node stdlib are trying to be removed).<p>Good coding conventions, promises, generators, and async&#x2F;await are your friends for making callback hell go away.
评论 #9588334 未加载
bunglealmost 10 years ago
OpenResty does something similar. Code can be written synchronously, but all the network io for example happens in a non-blocking manner. The code still looks like synchronous, though - without call back hell. This doesn&#x27;t come without issues as you need to change libraries to use OpenResty (Nginx) network primitives. Overall it is one of the nicest platforms I have worked with. A great webserver (Nginx) that can be programmed with a great language (Lua + LuaJIT).<p>At Nginx conf the Nginx developers where showing interest to bring Javascript to the platform. They said that they will take similar approach that OpenResty uses (aka no callback hell).
评论 #9587032 未加载
pmalyninalmost 10 years ago
I recently started programming more using promises and I can say that I am very satisfied with the way it does away with the callback hell problem.<p>Instead of taking a callback, a function returns a promise, which can be daisy-chained to do work.<p>Ex:<p>file.read().then(console.log);<p>Or using the example in the article:<p>var a = fileA.read();<p>var b = fileB.read();<p>promise.all([a,b]).then((data) =&gt; console.log(data[0], data[1]));
评论 #9586863 未加载
评论 #9587403 未加载
DonPellegrinoalmost 10 years ago
Why is this even being upvoted? it confuses important concepts and what isn&#x27;t wrong is irrelevant.
bhoustonalmost 10 years ago
We (<a href="http:&#x2F;&#x2F;Clara.io" rel="nofollow">http:&#x2F;&#x2F;Clara.io</a>) run multiple NodeJS instances per machine and our code base is Async. I believe this gives us the best of both worlds.<p>Also Sync versions of calls in NodeJS are likely going to be deprecated, thus this won&#x27;t even be possible in NodeJS going forward.
评论 #9587933 未加载
评论 #9587698 未加载
zjonssonalmost 10 years ago
If you look more closely at the actual code, this exercise compares the performance of readFileSync (an operation that deliberately blocks) on 1 core vs 2 cores.
andrewmutzalmost 10 years ago
It makes sense to have both event-based and non-event-based options for server side javascript development.<p>OS-level multitasking won&#x27;t be able to achieve the same level of concurrency, but the simplicity and maintainability of the application will go up. The right choice depends on the needs of the application, of course.<p>Both evented and non-evented approaches have their place, and most server-side languages allow development with either approach: Ruby, Python, C, Java all have solid options for evented and non-evented solutions.
评论 #9586843 未加载
martin-adamsalmost 10 years ago
I&#x27;ve adapted the node-fibers library to write synchronous style code. It works really well for my needs, but I do understand that my approach does litter the function prototype which is not ideal.<p>Code looks like this:<p><pre><code> var sync = require(&#x27;.&#x2F;sync&#x27;); sync(function () { try { var result = someAsyncFunc1.callSync(this, &#x27;param1&#x27;, param2&#x27;); if (result.something === true) { var result2 = someAsyncFunc2.callSync(this, &#x27;param1&#x27;); } else { var result2 = someAsyncFunc3.callSync(this, &#x27;param1&#x27;); } console.log(result2.message); } catch (ex) { &#x2F;&#x2F; One of them returned an err param in their callback } }); </code></pre> I haven&#x27;t tested the performance, so no idea if it&#x27;s a running like a dog.
评论 #9588140 未加载
babbyalmost 10 years ago
&gt;Cons: Larger memory footprint<p>The more annoying con is lack of shared memory. A single process can be much less complex when it doesn&#x27;t have to worry about messaging systems and off process caching.
bcoatesalmost 10 years ago
This is unsuitable for real-world applications, where you will, inevitably, need at least a little mutable shared state. Async handles this reasonably well at a decent performance cost; shared-memory threads (near-certain catastrophic failure) and database-only state (awful performance) do not.<p>The only real competition is transactional memory but it hasn&#x27;t become mainstream yet.
评论 #9587340 未加载
tlrobinsonalmost 10 years ago
<i>&quot;Asynchronous event-driven programming is at the heart of Node.js, however it is also the root cause of Callback Hell.&quot;</i><p>I&#x27;d argue the root cause is... callbacks.<p>Asynchronous programming can be done elegantly, in a synchronous style using &quot;async&#x2F;await&quot;, originally (?) in C# [1], likely to be added to the next version of JavaScript [2], also in Dart [2], Hack [4], and Python 3.5 [5]. It can also be emulated in languages with coroutines&#x2F;generators [6][7][8] (which in turn can be implemented by a fairly simple transpiler [9][10])<p>This:<p><pre><code> function foo(a, callback) { bar(a, function(err, b) { if (err) { callback(err) } else { baz(b, function(err, c) { if (err) { callback(err) } else { &#x2F;&#x2F; some more stuff callback(null, d) } }) } }) } </code></pre> Becomes this:<p><pre><code> async function foo() { var a = await bar(a) var c = await baz(b) &#x2F;&#x2F; some more stuff return d; } </code></pre> And you&#x27;ll see even greater improvements when using other constructs like try&#x2F;catch, conditionals, loops, etc.<p>[1] <a href="https:&#x2F;&#x2F;msdn.microsoft.com&#x2F;en-us&#x2F;library&#x2F;hh191443.aspx" rel="nofollow">https:&#x2F;&#x2F;msdn.microsoft.com&#x2F;en-us&#x2F;library&#x2F;hh191443.aspx</a><p>[2] <a href="http:&#x2F;&#x2F;jakearchibald.com&#x2F;2014&#x2F;es7-async-functions&#x2F;" rel="nofollow">http:&#x2F;&#x2F;jakearchibald.com&#x2F;2014&#x2F;es7-async-functions&#x2F;</a><p>[3] <a href="https:&#x2F;&#x2F;www.dartlang.org&#x2F;articles&#x2F;await-async&#x2F;" rel="nofollow">https:&#x2F;&#x2F;www.dartlang.org&#x2F;articles&#x2F;await-async&#x2F;</a><p>[4] <a href="http:&#x2F;&#x2F;docs.hhvm.com&#x2F;manual&#x2F;en&#x2F;hack.async.asyncawait.php" rel="nofollow">http:&#x2F;&#x2F;docs.hhvm.com&#x2F;manual&#x2F;en&#x2F;hack.async.asyncawait.php</a><p>[5] <a href="https:&#x2F;&#x2F;lwn.net&#x2F;Articles&#x2F;643786&#x2F;" rel="nofollow">https:&#x2F;&#x2F;lwn.net&#x2F;Articles&#x2F;643786&#x2F;</a><p>[6] <a href="https:&#x2F;&#x2F;github.com&#x2F;petkaantonov&#x2F;bluebird&#x2F;blob&#x2F;master&#x2F;API.md#promisecoroutinegeneratorfunction-generatorfunction---function" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;petkaantonov&#x2F;bluebird&#x2F;blob&#x2F;master&#x2F;API.md#...</a><p>[7] <a href="https:&#x2F;&#x2F;github.com&#x2F;kriskowal&#x2F;q&#x2F;tree&#x2F;v1&#x2F;examples&#x2F;async-generators" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;kriskowal&#x2F;q&#x2F;tree&#x2F;v1&#x2F;examples&#x2F;async-genera...</a><p>[8] <a href="http:&#x2F;&#x2F;taskjs.org&#x2F;" rel="nofollow">http:&#x2F;&#x2F;taskjs.org&#x2F;</a><p>[9] <a href="https:&#x2F;&#x2F;babeljs.io&#x2F;docs&#x2F;learn-es6&#x2F;#generators" rel="nofollow">https:&#x2F;&#x2F;babeljs.io&#x2F;docs&#x2F;learn-es6&#x2F;#generators</a><p>[10] <a href="https:&#x2F;&#x2F;facebook.github.io&#x2F;regenerator&#x2F;" rel="nofollow">https:&#x2F;&#x2F;facebook.github.io&#x2F;regenerator&#x2F;</a>
评论 #9587192 未加载
评论 #9587318 未加载
merittalmost 10 years ago
This is great. I wonder how long til the node community &quot;discovers&quot; that using a dedicated httpd and communicating over a standardized middleware (fcgi, wsgi, rack, etc) is also a superior approach instead of handling http directly.
评论 #9586942 未加载
评论 #9586978 未加载
KaiserProalmost 10 years ago
Now forgive my naivety, but isn&#x27;t this just threads?<p>I understand that because if the funny scoping rules it means that threading is actually surprisingly hard? surely you&#x27;d want more control over your threaded event loops?
iglalmost 10 years ago
Do your apps run single-threaded? Why wouldn&#x27;t you cluster? In-memory state is easily avoidable. Using in-memory sessions even prints a warning in express by default.
bricssalmost 10 years ago
Just add a pickle Promises