171 points · by jkreeftmeijer · 16y ago · 84 comments
In the Python world, for example, eventlet, gevent, and diesel (disclosure: my project) all use coroutines to achieve very high performance asynchronous I/O that's still written in a "synchronous" style. And before that, CCP games has been using stackless python to do coroutine-based networking for the servers behind the very popular MMORPG eve online (http://www.tentonhammer.com/node/10044).
Even slicker, in languages like Erlang and Haskell (via forkIO + the select/epoll I/O manager), support for asynchronous networking using "blocking-style" code is a fundamental language/compiler/vm feature. When you write any code in these languages, you're transparently taking advantage of the same scaling characteristics node.js and its ilk provide.
So, I'd humbly suggest to anyone getting serious about doing this kind of programming that they investigate some of these systems (which are usually built by weary callback-style async I/O veterans) and leapfrog the rest of their peers. Consider Node.js a gateway drug.
Linkage:
http://eventlet.net/ http://github.org/jamwt/diesel http://en.wikipedia.org/wiki/Asynchronous_I/O#Light-weight_p... http://www.galois.com/~dons/slides/a-scalable-io-manager-for...
That might be true. Maybe for Big Programs, something else is better. But I think the problem is that setting out to build a Big Program for Serious Business is Doing It Wrong.
The best frameworks (or, at least, my favorite frameworks) are collections of small tools with consistent interfaces and interchangeable parts.
If you can manage callbacks for a 100-200 LOC program, then you can build a Big Program out of such modules with a little bit of forethought. Instead of setting out to write a Big Program, why not try to figure out how to express your Big Problem in terms of a bunch of Little Problems, and then come up with a way to have Little Programs assemble.
Then, assemble the Little Programs to solve the Little Problems. There is no solveable Big Problem which cannot be broken down into some finite set of Little Problems.
Callbacks make it natural to solve problems in this manner.
I actively develop a several thousand line NodeJS project. It's quite nice, actually.
Callback style, within an I/O handler, is (almost always) a concession to the event loop. When I'm writing a program, I write:
routine():
result = do_one_thing()
do_next_thing(result)
do_whatever()
With callback based I/O, you need the routine to end so the "reactor" up the call stack can get control again to do I/O on your behalf: routine_1():
return make_some_io_request(callback=routine_2)
routine_2():
return make_another_io_request(callback=routine_3)
routine_3():
return send_response_to_original_socket()
Now, if you were desperate, you could just call over to the reactor like this: routine():
resp = do_io_for_me()
next_resp = do_more_io_for_me(resp)
return transform(next_resp)
.. but, there are (at least) two problems with this:1. You're making the stack deeper every time you "call over" to the reactor... you will stack overflow eventually
2. In most languages, the reactor doesn't have a way to "call back into" your stack frame and resume it.
Coroutines are a way to "call over" to the reactor, have your stack state frozen (in heap space, without going deeper in the call stack), and then "unfreeze it" later on, when the reactor has completed the IO.
The greenlet package provides this for Python; Haskell and Erlang have native support for this. And, this is ultimately how you want to program. A(); B(); C(). Sure, there are exceptions for UI driven callbacks, and callback patterns aren't all bad, but typically in network IO cases, the callbacks are explicitly a concession to the I/O loop's need to be scheduled, made necessary by a missing language or vm feature.
And: cleaner code benefits 200 LOC apps as much as it does 200k LOC apps. A good design is a good design at any scale.
function routine () {
doIOForMe(function (resp) {
doMoreIO(resp, function (nextResp) {
transform(nextResp)
})
})
}
// or with a little helper function:
function routine () {
chainTogether
( doIOForMe
, doMoreIO
, transform
)
}
The "Step" library from Tim Caswell (creationix) lets you do a LOT of very interesting parallel/serial stuff. In npm, I have a few helper functions that make it easy to do serial "this then that" stuff.It would be nice to have something in the language that does this. Maybe something for the Coffeescript guys to check out. For me, plain old JavaScript is enough, I guess.
Node is an attempt at using this successful model on the server too where is can solve massive scalability issues using the exact paradigm front-end devs already know.
Also while there are many technical ways to make code look blocking, but really be running other events under the hood, it's this exact implicit running of "other stuff" that makes writing threaded code so hard. You have to assume that things can change between every function call because you don't know if somewhere down the chain it's doing pseudo-blocking.
JavaScript's model is simple, you provide callbacks and you know exactly at what boundaries things can happen asynchronously.
In summary, node is one way of doing it. We think it's a better model and it's proven itself in the browser. If you think another model is better, than by all means go for it!
Let the leapfrogging begin ;)
But: I don't make my server-side language decisions based on what happens to be in the browser. To think that a language required for use in such a constrained environment is just _coincidentally_ also the best language to use server-side where people have been doing event-based programming for decades seems... unlikely. It would be as ridiculous as asserting that we should all use postscript in the browser because that's the standard we've been using in printers for the last 20 years.
I suppose if you make the argument that browsers dictate that developers _must_ learn javascript, and therefore, with Node.js they can also program server-side without ever needing to learn a second language, that is a sound argument. I don't, however, know many good programmer that spend a career knowing exactly one language.
It's a lot better than writing C, (which is what Ryan was doing before starting node) since JS has closures, anonymous functions and other functional niceties that make event based programming much easier.
The fact that you can now code your server-side code in the same language and paradigm as your client-side code is a huge bonus, but was not the reason node was created. V8 is an amazing VM and it's a language that lots of talented developers know. Why not let them loose on the server and see what comes out of this talent.
Given the constraint of using JavaScript on the server, node is the best solution. If you don't want that constraint, then maybe erlang or something with no-shared state is a better solution.
Since many of the Node.js demos like this one are open-source it's much easier for developers to download working code and make modifications to see how everything works.
The technically superior solutions often get overlooked for the most accessible ones.
Are there any server-side Javascript environments being overlooked? And would it be fair to say that these are technically superior to V8 + Node?
The technologies you are describing were already there, but it's not just a problem of technology, feature set or execution power. It's also a matter of simplicity.
Node.js is working because it gives so much power with a very simple approach. And by approach I mean also how much is simple to understand it and start producing something useful.
That's exactly the reason why we use more abstract languages, and that's the reason why Node.js is getting a lot of attention recently. If you don't know any language, do you think it's simpler to start with Erlang or Haskell, or with JavaScript? I don't have any proof, but my bet is surely on JavaScript. :)
And even the most obvious things are important. Because maybe a uber-developer can ignore the small details, but most of the programmers aren't uber, they just want to develop easily and happily. Every little detail matters. Just see how many steps you need to install Erlang on your machine, and how many steps you need to do the same with node. It seems stupid, I know. But when you sum every detail... it matters. :)
At the same time, this gives power to the uber-programmers out there to bring on more cutting-edge solutions when they need them, and feed the "simple" level with their discoveries and experience, making the language and frameworks evolve.
If you're right, one day we are going to have simple coroutines - in the complex, environmental and social sense expressed above. Maybe even in Node.js, because in the end JavaScript 1.7 afaik supports yield and V8 could implement it in the future. ;)
Besides, are you seriously implying that the hard part of getting started with Erlang is installing it? A lot of developers aren't willing to sink time into learning a language that actually has new ideas, you know? Hell, it's not even OO. :)
I do agree, though, that developers should not ignore other ways of doing things, just don't discredit the callback way of doing things as it can be useful.
No, no, a thousand times no. Node.js partisans really need to start actually using Erlang or Haskell for a little while before spouting this canned line off. You do not have to jump through enormous hoops to deal with IO in Haskell or Erlang, it just works.
Callbacks are behind the state of the art. Coroutines or generators or any other cooperative-multitasking primitives in languages that didn't support them from day one and have enormous sets of libraries not "cooperative-aware" are behind the state of the art, too.
This is all just "cooperative multithreading" again and I am yet to see anyone explain why this time is going to turn out any different than last time we tried cooperative multithreading.
The actual cutting edge of event-driven server architecture[1] looks something like this:
server = do {
sys_call_1;
fork client;
server;
}
client = do {
sys_call_2;
}
(And even then it has marginal benefits in terms of throughput and latency compared to threaded implementations.)[1] Li and Zdancewic. Combining events and threads for scalable network services. Proc. 2007 PLDI (2007).
With a callback-based system, stacks are short, and you explicitly carry that state. You KNOW what state you're storing, and you can see it easily.
There's some benefit to that.
And the pattern for aggregating functions is different: Receive a starting event, emit a done event -- you aggregate processes into sets of events, not into function calls. So yeah, it's not going to follow some of the same patterns that non-event-driven code follows, and event-driven code is going to look rather different than callback-passing code.
I think Ryan's right about coroutines, too: Coming back to vastly different state after a simple function call is basically going back to the days of using global variables.
function main(socket)
io.stdout:write("connection from " .. tostring(socket))
while(true)
local cmd = string.upper(socket:read())
if cmd == "SHOW" then
socket:write("show me some stuff\n")
elseif cmd == "PING" then
for i = 1 , 15 do
socket:write(".")
socket:sleep(1)
end
socket:write("\n")
elseif cmd == "QUIT" then
socket:write("Good bye\n");
return
elseif cmd == nil then
return
end
end
end
Makes writing simple servers nice and easy.As with select(2) in general, this doesn't scale up past 100ish idle sockets - it has to do a full scan over all sockets to check which are ready for IO, and the latency eventually dominates. (Not a big deal for most uses, but problematic for web applications.) If that's not an issue, though, it's quite easy. Lua is very underrated, IMHO.
I'm working on a Lua library (octafish) for doing libev + coroutine and/or callback-based servers. It's been on the back burner for a bit, though - a couple other projects have been crowding it out. I'll put it on github once it's further along.
Diesel looks great. When looking into alternatives in Erlang/Haskell/Scala, or XMPP/BOSH, either it's not easy to find quick how-to examples, or I'm looking in the wrong places. Any fingers in the right direction would be welcome.
Just fork of a thread per connection (using forkIO) and just call normal I/O actions. The implementation will multiplex those forkIO threads onto a single OS thread that calls epoll/kqueue.
http://howtonode.org/control-flow http://howtonode.org/control-flow-part-ii http://howtonode.org/control-flow-part-iii http://howtonode.org/do-it-fast http://howtonode.org/step-of-conductor
However, none of the systems you suggest make it possible to write code that can be run by both the server and the browser.
If you were writing web applications, which represent the typical use-case for Node, then this would be a prerequisite.
It's a convenience, at least intuitively, but in practice one has to wonder what parts of your code run in both browser and server and do async I/O.
1. Work offline.
2. Provide sub-20ms interface switching response rates.
3. Protect the interface against extended network or server failure.
4. Buffer against poor network performance.
So I would agree with you that they have "managed to do without it" (code-sharing) so far. But they have done so only by avoiding responsibility for these goals, by considering them a convenience, or by not considering them at all.
Secondly, I can tell you from experience that a non-Javascript server-side framework can do the above but only with difficulty. Existing popular frameworks do not provide "convention-over-configuration" assistance in terms of straddling the network or exposing Model definitions to Javascript etc. They render interfaces on the server-side, rather than letting controllers and views interact on the client for example. Indeed it would not even be fair to expect these frameworks to meet the above requirements, simply because they were not designed to do so.
Eventually one must arrive at a realization that web applications (as distinct from "web sites") will mostly be written in the same language AND framework on both client and server. Fear not, this will probably be more fun.
I think the real problem is that callbacks are too simple for people that want to obsess about engineering new primitives and programming styles.
It was also interested to see if I could get other people to form lines and other constructs, without any communication other than my own cursor.
I'm not using any server monitoring so it can crash at any moment.
Would love to get more than 15 users on here moving around and see what happens.
Very cool concept though.
the blinky body toggle was pretty cool to see though :)
The idea was 'mouse games'. I had a similar setup where you could see everyones mouse cursor. I planned a whole load of multiplayer games where you could chase each other, complete puzzles, click on other people to kill them, draw circles round other players to trap them, etc etc. You could have messages show up like "First to draw a square gets a point". "Move to the left get a point". "Move as far away from everyone else as you can. Most isolated player gets a point". You could have objects being thrown at cursors, and you have to dodge them. Tons of potential to be a fun way for people to waste time :)
I'm sure it would have been great fun to make :)
* Come back regularly to play it?
* Pay to play it?!! :/
* Click on ads? (If the creator were evil, they'd confuse
the users into clicking on ads, making them think it's
part of the game).
Still a fun thing to make though!- How easy would this be to implement in another event-driven server, such as Tornado?
- I have an identifiable "mouse ID" in this app. Is this secure? Surely the security aspects of web sockets have been key in their design, but I am left with a bit of uneasiness with my browser opening a TCP connection to another server. I believe WebSockets will pave the way for a whole new class of web-based attacks, and possibly even highly mobile worms.
1. Shared code between client and server.
2. Specialization in one language.
V8 and Node are at least a very good combination for server-side Javascript.
Re: "is it absolutely not suitable for production use?"
Node in alpha state is already more production ready than many "production ready" tools.
Edit: This lame comment is the highest rated here? You disappoint me.
I feel OK about doing this.
I'm genuinely impressed by how it's holding out now. :)
Come on guys - it's _Web_ sockets, not intranet sockets.
if 80 is impractical, many people suggest running off 443 as that (the https port) is unlikely to be filtered.
Some of the node.js websocket impls let you do this easily while others don't. Don't use the ones that require a separate port. Nobody behind a corporate firewall will be able to use your app if you do.
Just for sake of completeness, you can basically copy-paste that code into Ruby as well via em-websocket: http://github.com/igrigorik/em-websocket
Also, to avoid some of the callback muck that a few have brought up: http://github.com/igrigorik/em-synchrony
(same idea, closures + coroutines to abstract the callbacks).