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 - A Giant Step Backwards

102 pointsby Fennalmost 14 years ago

19 comments

rbransonalmost 14 years ago
This purist evented I/O fundamentalism has to stop.<p>While evented I/O is great for a certain class of problems: building network servers that move bits around in memory and across network pipes at both ends of a logic sandwich, it is a totally asinine way to write most logic. I'd rather deal with threading's POTENTIAL shared mutable state bullshit than have to write every single piece of code that interacts with anything outside of my process in async form.<p>In node, you're only really saved from this if you don't have to talk to any other processes and you can keep all of your state in memory and never have to write it to disk.<p>Further, threads are still needed to scale out across cores. What the hell do these people plan on doing when CPUs are 32 or 64 core? Don't say fork(), because until there are cross-process heaps for V8 (aka never), that only works for problems that fit well into the message-passing model.
评论 #2848743 未加载
thristianalmost 14 years ago
It seems a bit cruel that he mentions "horror stories" about Twisted; most of the culture shock people complain about with Twisted is exactly the kind of flow-control shenanigans that he describes in Node.js. In fact, Twisted makes those particular examples easier.<p>To handle branching flow-control like 'if' statements, Twisted gives you the Deferred object[1], which is basically a data structure that represents what your call stack would look like in a synchronous environment. For example, his example would look something like this, with a hypothetical JS port:<p><pre><code> d = asynchronousCache.get("id:3244"); // returns a Deferred d.addCallback(function (result) { if (result == null) { return asynchronousDB.query("SELECT * from something WHERE id = 3244"); } else { return result; } }); d.addCallback(function (result) { // Do various stuff with myThing here }); </code></pre> Not quite as elegant as the original synchronous version, but much tidier than banging raw callbacks together - and more <i>composable</i>. Deferred also has a .addErrback() method that corresponds to try/catch in synchronous code, so asynchronous error-handling is just as easy.<p>For the second issue raised, about asynchronous behaviour in loops, Twisted supplies the DeferredList - if you give it a list (an Array, in JS) of Deferreds, it will call your callback function when <i>all</i> of them have either produced a result or raised an exception - and give you the results in the same order as the original list you passed in.<p>It is a source of endless frustration to me that despite Twisted having an excellent abstraction for dealing with asynchronous control-flow (one that would be even better with JavaScript's ability to support multi-statement lambda functions), JavaScript frameworks generally continue to struggle along with raw callbacks. Even the frameworks that <i>do</i> support some kind of Deferred or Promise object generally miss some of the finer details. For example, jQuery's Deferred is inferior to Twisted's Deferred: <a href="http://article.gmane.org/gmane.comp.python.twisted/22891" rel="nofollow">http://article.gmane.org/gmane.comp.python.twisted/22891</a><p>[1]: <a href="http://twistedmatrix.com/documents/current/core/howto/defer.html" rel="nofollow">http://twistedmatrix.com/documents/current/core/howto/defer....</a>
评论 #2849300 未加载
评论 #2851616 未加载
评论 #2849718 未加载
评论 #2850677 未加载
daleharveyalmost 14 years ago
This is a pretty common pattern for any work you have to do asynchronously, pretty much all libraries should be implementing this for you so the first 3 lines should be all you code<p><pre><code> getSomething("id", function(thething) { // one true code path }); function getSomething(id, callback) { var myThing = synchronousCache.get("id:3244"); if(myThing) { callback(null, myThing); } else { async(id, callback); } } </code></pre> a minor quibble with language style isnt exactly what I would call "A Giant Step Backwards"
评论 #2849347 未加载
评论 #2848553 未加载
评论 #2848565 未加载
pkulakalmost 14 years ago
In my experience Node.js is more difficult than synchronous code. But it's also, by far, the easiest way to get something running that's massively parallel.<p>I recently wrote a project that needs to do 100's or 1000's of possibly slow network requests per second. The first try was Ruby threads. That was a disaster (as I should have predicted). I had an entire 8-core server swamped and wasn't getting near the performance I needed.<p>The next try was node. I got it running and the performance was fantastic. A couple orders of magnitude faster than the Ruby solution and a tenth of the load on the box. But, all those callbacks just didn't sit right. Finding the source of an exception was a pain and control flow was tricky to get right. So, I started porting to other systems to try to find something better. I tried Java (Akka), EventMachine with/without fibers, and a couple others (not Erlang though).<p>I could never get anything else close to the performance of Node. They all had the same problems I have with Node (mainly that if something breaks, the entire app just hangs and you never know what happened), but they were way more complicated, _harder_ to debug, and slower.<p>I have a new appreciation for Node now. And now that I'm much more used to it, it's still difficult to do some of the more crazy async things, but I enjoy it a lot more. It's a bit of work, and you have to architect things carefully to avoid getting indented all the way to the 80-char margin on your editor, but you get a lot for that work.
评论 #2848517 未加载
评论 #2848489 未加载
评论 #2848525 未加载
评论 #2848527 未加载
benatkinalmost 14 years ago
Nice article. Be sure to read the comments, as the author links to a library that makes the second example easy to rewrite in a short and elegant way. <a href="https://github.com/caolan/async#forEach" rel="nofollow">https://github.com/caolan/async#forEach</a><p>Also the first example, the cache hitting and missing, could be rewritten with <i>async</i>, too.<p><pre><code> async.waterfall([ function(callback) { asynchronousCache.get("id:3244", callback); }, function(myThing, callback) { if (myThing == null) { asynchronousDB.query("SELECT * from something WHERE id = 3244", callback) } else { callback(myThing) } }, function(myThing, callback) { // We now have a thing from the DB or cache, do something with result // ... } ]);</code></pre>
评论 #2849647 未加载
yummyfajitasalmost 14 years ago
Just curious, is there a good futures library for Node? If so, you could write code of the form:<p><pre><code> x = db.getFutureResult("x"); y = db.getFutureResult("y"); whenFuturesReady([x,y], callback(x, y) { useResults(x,y); }); </code></pre> This looks reasonably similar to typical synchronous code,<p><pre><code> x = db.getResult("x") y = db.getResult("y") useResults(x,y) </code></pre> but it allows db queries to happen simultaneously and doesn't break the node paradigm.
评论 #2849132 未加载
richcollinsalmost 14 years ago
It's unfortunate that Node doesn't use actor based coroutines. Most of the code readability issues would go away.
评论 #2849622 未加载
olegpalmost 14 years ago
It's possible to have the best of both worlds by using node-fibers (<a href="https://github.com/laverdet/node-fibers" rel="nofollow">https://github.com/laverdet/node-fibers</a>) and mixing synchronous and asynchronous styles as appropriate.<p>I believe that JavaScript could become the dominant language on the server. We just need to have a set of consistent synchronous interfaces across the major server side JavaScript platforms. This would allow for innovation and code reuse higher up the stack.<p>I'm doing my bit by maintaining Common Node (<a href="https://github.com/olegp/common-node" rel="nofollow">https://github.com/olegp/common-node</a>), which is a synchronous CommonJS compatibility layer for Node.js.
jjmalmost 14 years ago
I would rather have seen "Async programming or Node.JS is hard to 'get right'" than a "Step backward" - which I don't see it being at all.
sausagefeetalmost 14 years ago
Nitpick: Node doesn't do anything in parallel, it can't, it does things concurrently.
评论 #2848568 未加载
评论 #2848650 未加载
评论 #2848524 未加载
Maroalmost 14 years ago
When you write an async. server in C++, where you can't inline functions, you write functions like OnRead(), OnWrite(), etc. Once you get used to it the whole thing ends up fairly easy to read and understand. Eg.<p><a href="https://github.com/scalien/scaliendb/blob/master/src/Framework/TCP/TCPConnection.cpp" rel="nofollow">https://github.com/scalien/scaliendb/blob/master/src/Framewo...</a><p>I guess the OP is saying inlining [in a language where this is even possible] leads to unreadable code, which sounds about right.
robinhowlettalmost 14 years ago
Not to nitpick but the linked article's title is "NODE.JS - A GIANT STEP BACKWARDS?". That question mark is important.
leaalmost 14 years ago
I would write something like this:<p><pre><code> function handler(yes, no) { return function (err, data) { if (data) { yes(err, data); } else { no(err, data); } } } function get() { function done(err, data) { // do something with data } function db() { asynchronousDb.query("SELECT * fomr something where id = 3244", done); } asynchronousCache.get("id:3244", handler(done, db)); }</code></pre>
karavelovalmost 14 years ago
Though async event driven programming is somehow confusing in the beginning, there are some idioms that could make your code more comprehensible.<p>My experience (mostly in perl - EV,AnyEvent, etc.) is that combining evens with finite state machines gives more structured code, with smaller functions that interact in predefined manner.
Detrusalmost 14 years ago
The V8 team is thinking of adding yield/defer support to make programming in Node neater. There's hope yet.<p>Meanwhile there are other choices that are about as easy, like Python libraries and Google's Go. Too bad they don't have the same zealous community support.
评论 #2848888 未加载
评论 #2848853 未加载
评论 #2848697 未加载
Tichyalmost 14 years ago
While I am also drawn to NodeJS, I wonder if it wouldn't make more sense to use a language that supports coroutines. Not sure which ones would apply - probably Racket, as they seem to do everything?
moonlighteralmost 14 years ago
I liked the post, sans the attention-grabberish title (which made me read the darn thing to begin with, though ;)
nirvanaalmost 14 years ago
If you're interested in keeping up to date with the project I describe below, please follow me on twitter @NirvanaCore.<p>I had many of the same concerns with node.js. Every time I attempted to wrap my head around how I'd write the code I needed to write, it seemed like node was making it more complicated. Since I learned erlang several years ago, and first started thinking about parallel programming a couple decades ago, this seemed backwards to me. Why do event driven programming, when erlang is tried and true and battle tested?<p>The reason is, there isn't something like node.js for erlang, and so I set out to fix that.<p>For about a year I've been thinking about design, and for a couple months I've been implementing a new web application platform that I'm calling Nirvana. (Sorry if that sounds pretentious. It's my personal name- I've been storing up over a decades worth of requirements for my "ideal" web framework.)<p>Nirvana is made up of an embarrassingly small amount of code. It allows you to build web apps and services in coffeescript (or javascript) and have them execute in parallel in erlang, without having to worry too much about the issues of parallel programming.<p>It makes use of some great open source projects (which do all the heavy lifting): Webmachine, erlang_js and Riak. I plan to ship it with some appropriate server side javascript and coffee script libraries built in.<p>Some advantages of this approach: (from my perspective)<p>1) Your code lives in Riak. This means rather than deploying your app to a fleet of servers, you push your changes to a database.<p>2) All of the I/O actions your code might do are handled in parallel. For instance, to render a page, you might need to pull several records from the database, and then based on them, generate a couple map/reduce queries, and then maybe process the results from the queries, and finally you want to render the results in a template. The record fetches happen in parallel automagically in erlang, as do the map/reduce queries, and components defined for your page (such as client js files, or css files you want to include) are fetched in parallel as well.<p>3) We've adopted Riak's "No Operations Department" approach to scalability. That is to say, every node of Nirvana is identical, running the same software stack. To add capacity, you simply spin up a new node. All of your applications are immediately ready to be hosted on that node, because they live in the database.<p>4) Caching is built in, you don't have to worry about it. It is pretty slick- or I think it will be pretty slick-- because Basho did all the heavy lifting already in Riak. We use a Riak in-memory backend, recently accessed data is stored in RAM on one of the nodes. This means each machine you add to your cluster increases the total amount of cache RAM available.<p>5) There's a rudimentary sessions system built in, and built in authentication and user accounts seem eminently doable, though not at first release. Also templating, though use any js you want if you don't like the default.<p>So, say, you're writing a blog. You write a couple handlers, one for reading an article, one for getting a list of articles and one for writing an article. You tie them to /, /blog/article-id, and /post. For each of these handlers, any session information is present in the context of your code.<p>To get the list of articles, you just run the query, format the results as you like with your template preference and emit the html. If it is a common query, you just set a "freshness" on it, and it will be cached for that long. (EG: IF you post new articles once a week, you could set the freshness to an hour and it would pull results from the cache, only doing the actual query once an hour.)<p>To display a particular article, run a query for the article id from the URL (which is extracted for you) and, again this can be cached. For posting, you can check the session to see if the person is authorized, or the header (using cookies) and push the text into a new record, or update an existing record. Basically this is like most other frameworks, only your queries are handled in parallel.<p>The goal is to allow rapid development of apps, easy code re-use, and easy, built-in scalability, without having to think much about scalability, or have an ops department.<p>This is the very first time I've publicly talked about the project. I think that I'm doing something genuinely new, and genuinely worth doing, but its possible I've overlooked something important, or otherwise embarrassed myself. I don't mean to hijack this thread, but felt that I needed to out my project sometime. A real announcement will come when I ship.<p>If you're interested in keeping up to date with the project I describe above, please follow me on twitter @NirvanaCore.<p>EDIT TO ADD: -- This uses Riak as the database with data persisted to disk in BitCask. The Caching is done by a parallel backend in Riak (Riak supports multiple simultaneous backends) which lives in RAM. So, the RAM works as a cache but the data is persisted to disk.
评论 #2848730 未加载
评论 #2848801 未加载
评论 #2849329 未加载
评论 #2849008 未加载
评论 #2848706 未加载
rmasonalmost 14 years ago
I think it's a job for the jquery team, a node.js for the rest of us. Once they get jquery mobile out the door it would seem to be the most obvious next project.
评论 #2848811 未加载
评论 #2848799 未加载