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.

On problems with threads in Node.js

125 pointsby xylemabout 10 years ago

13 comments

spionabout 10 years ago
Not sure if it was intentional, but the article is quite misleading.<p>The thread pool in node is only used for a limited number of APIs. Pretty much all networking uses native async IO and is unaffected by the size of the thread pool. Things like Oracle&#x27;s driver are rare exceptions: the typical MySQL&#x2F;PostgreSQL&#x2F;redis etc drivers all use native async IO and are unaffected by this.<p>The author only glosses over this briefly. As a result this article leaves the impression that the problem described is the norm, which is not the case.
评论 #9426420 未加载
评论 #9426288 未加载
评论 #9428293 未加载
评论 #9427260 未加载
评论 #9426919 未加载
laggylukeabout 10 years ago
It&#x27;s worth noting that DB drivers that actually integrate with libuv are a minority - most DB divers use Node-level network APIs and are unaffected by such thread pool limits.
评论 #9425776 未加载
评论 #9425832 未加载
cddotdotslashabout 10 years ago
I actually ran into an issue recently with CPU intensive tasks blocking my web server. It turns out that &quot;querystring&quot; (used to parse request bodies in web applications) is an asynchronous, blocking request. You&#x27;d never notice much slowness, until your request bodies are massive (think 50 nested JSON objects and some base64 image data for good measure) and you have multiple per second. Now, every request is blocked until the previous one is processed. I&#x27;m still trying to figure out a solution, after looking into worker threads, etc.
评论 #9426281 未加载
GordyMDabout 10 years ago
Thank you for sharing your findings here. Very pertinent to me right now - could actually drastically minimise the amount of research I have to do today. I love HN.
albertzeyerabout 10 years ago
Is there a detailed overview about which functions in libuv (Nodejs) rely on blocking primitives and thus are using the thread pool to work async?<p>From [here](<a href="http:&#x2F;&#x2F;docs.libuv.org&#x2F;en&#x2F;latest&#x2F;design.html" rel="nofollow">http:&#x2F;&#x2F;docs.libuv.org&#x2F;en&#x2F;latest&#x2F;design.html</a>), it sounds like all file IO is always based on blocking primitives, and native async file IO primitives are not used, although such async file IO primitives do exists and were tried out in libtorrent (<a href="http:&#x2F;&#x2F;blog.libtorrent.org&#x2F;2012&#x2F;10&#x2F;asynchronous-disk-io&#x2F;" rel="nofollow">http:&#x2F;&#x2F;blog.libtorrent.org&#x2F;2012&#x2F;10&#x2F;asynchronous-disk-io&#x2F;</a>). The result of that experiment however was mostly that the thread pool solution was simpler to code (I guess).<p>From the libuv design doc, the overview is:<p>* Filesystem operations<p>* DNS functions (getaddrinfo and getnameinfo)<p>* User specified code via uv_queue_work()<p>I wonder whether this is really the best solution of if some combination of a thread pool and native async disk IO primitives could perform better.
评论 #9426009 未加载
评论 #9425972 未加载
评论 #9425954 未加载
mjpaabout 10 years ago
Is this another case of &quot;here&#x27;s the code I ran&quot; when in fact they didn&#x27;t? There should be 3 lines of output, not 6!<p>Also, the code says it will print the time taken since the start of the program, which again doesn&#x27;t go with the output and the conclusion being made!<p>Anyway, how come the output isn&#x27;t in order?
评论 #9425782 未加载
评论 #9425772 未加载
z3t4about 10 years ago
Use named functions to avoid closures!<p><pre><code> for (var i = 0; i &lt; 3; ++i) { namedFunction(i); } function namedFunction(id) { fs.readdir(&#x27;.&#x27;, function () { var end = process.hrtime(start); console.log(util.format(&#x27;readdir %d finished in %ds&#x27;, id, end[0] + end[1] &#x2F; 1e9)); }); };</code></pre>
vinceyuanabout 10 years ago
Does it mean the async functions about file system operations are not really asynchronous in Node.js? The whole node.js server will be blocked if the number of file system operations is bigger than the thread pool size. :-(
评论 #9426132 未加载
imaginenoreabout 10 years ago
4 seems like a ridiculously low default size for the thread pool.
评论 #9425881 未加载
ryankshawabout 10 years ago
(noob question) why would the libuv threadpool choose to use a static 4 instead of something like matching the number of processor cores available by default?
评论 #9426665 未加载
jaysoncenaabout 10 years ago
Is there an easy way to identify the number of tasks queued in uv&#x27;s work queue?
babuskovabout 10 years ago
To sum it up: node.js runs only 4 background threads. Be aware of this.<p>No big deal really. I have multiple servers running node.js under load for 3+ years and never had an issue with this.<p>In fact, I found it helpful. If your database is under load and is already running 4 heavy queries, not giving it any more jobs is actually a good thing.
评论 #9425774 未加载
评论 #9425685 未加载
评论 #9426094 未加载
exo762about 10 years ago
Piece of badly engineered software conquering the world. As if more JavaScript was really something worth pursuing.
评论 #9425654 未加载
评论 #9425796 未加载
评论 #9425786 未加载
评论 #9425612 未加载