News for hackers. Stuff that matters.
Re:Interview with Ryan Dahl, Creator of Node.js by bit_logic on Thursday August 31, 2017 @01:39PM (#15142105)
Many are forgetting the initial reason node.js became popular. Consider the popular server-side landscape before node.js. It was dominated by Java, Python, etc. The primary way these ecosystems handle concurrency is OS-level threading. There was nothing else in the popular languages. Each language had some niche library that did non-blocking I/O (Twisted for Python, Netty for Java), but these all had a critical flaw, which is the rest of the ecosystem didn't support it. Basically every library, almost all existing code used the threading model. And when you mix threaded libraries with a non-blocking I/O server, it completely falls apart because the threaded code blocks.

Then came node.js. Look at the ecosystem it came into. JS itself has very little standard library, and nothing for I/O. It has a highly optimized VM supported by the resources of Google. It's a language known by many developers. node.js took this well developed clean slate and built a non-blocking I/O server on top of it. It offered a concurrency model on the server side that's completely new to many developers, an alternative to the traditional threading model everyone knew. Since server-side JS didn't exist yet, it forced all libraries to be written in this non-blocking way. Every package in NPM is written to support the core design of node.js which is non-blocking I/O. And it was exciting to many developers, it was a reason to rewrite everything in this new way.

Re:Interview with Ryan Dahl, Creator of Node.js by gazaren on Thursday August 31, 2017 @02:12PM (#15142445)
IMO, the biggest reason Node.js got popular was because of JavaScript. You just need to learn one language to do full stack development. No need to deal with Python or Java or whatever. Being single-threaded is a side effect of using V8, which is mostly single-threaded. Since you only have one thread, you need IO to be non-blocking.
Re:Interview with Ryan Dahl, Creator of Node.js by rmrfrmrf on Thursday August 31, 2017 @06:24PM (#15144492)
I disagree because Rhino and Narwhal were already around. Libuv and the event/callback approach to IO (which is the part that's familiar to front-end devs) are the secret sauce to Node.js.
Re:Interview with Ryan Dahl, Creator of Node.js by jrs95 on Thursday August 31, 2017 @02:16PM (#15142472)
Yeah, I think this is the primary reason it really had traction. There was definitely some initial hype around async and performance, but there are a lot of ways (especially now) to achieve that without Node. The real win for me is a familiar language with a huge community and package ecosystem on the server that you likely have to use anyways in the browser.
Re:Interview with Ryan Dahl, Creator of Node.js by kitd on Thursday August 31, 2017 @06:13PM (#15144407)
Agreed. And if it had just stayed at JS and Node, I wouldn't have minded. But then they had to go and inflict NPM on the world, and what should have been a tidy stack got idiotically messy.
Re:Interview with Ryan Dahl, Creator of Node.js by nulagrithom on Thursday August 31, 2017 @02:18PM (#15142487)
On that note, coming back from node.js has been a really frustrating experience for me. I want to scream every time somebody suggests threading as a solution to non-blocking database calls and HTTP requests. It's made me painfully aware of how often parallelism is suggested in place concurrency.

C# in particular does a great job at conflating concurrency and parallelism. Both fall under the same Task namespace and it drives me mad to no end. I've also seen a couple instances of Execute and ExecuteAsync both blocking because the underlying driver didn't support async, with no indication other than my async calls blocking.

It's quite different from the async-by-default sort of thing that's going on in the node.js world.

Re:Interview with Ryan Dahl, Creator of Node.js by corford on Thursday August 31, 2017 @08:17PM (#15145165)
> I want to scream every time somebody suggests threading as a solution to non-blocking database calls and HTTP requests.

Rather than screaming, perhaps read this (from the developer of the Python world's most popular ORM) to see why they might be suggesting it:

http://techspot.zzzeek.org/2015/02/15/asynchronous-python-an...

tl;dr: If your database is properly setup and provisioned it shouldn't be a source of blocking I/O (and if it is, throwing thousands of concurrent async connections at it isn't going to help you). For standard CRUD applications, your program will spend much more time in CPU processing the database response than it will waiting on the database to deliver it.

Furthermore, when querying a traditional RDBMS like postgres or mysql, safe concurrency is ultimately enforced by the ACID capabilities built in to the database itself i.e. transactions. Whether you're using async, threads, multiple processes or something else entirely doesn't matter. Given the database itself controls how much concurrency it can support, real world tests (at least for Python) show that threads give you better performance than event driven callbacks.

Of course, threads are completely wrong for the HTTP side of things and this is where async (e.g. nginx or node) offers all of its advantages. That doesn't mean it's wrong to put your DBMS behind a thread pool.

Re:Interview with Ryan Dahl, Creator of Node.js by AgentME on Thursday August 31, 2017 @04:14PM (#15143517)
A little tip from someone coming back into Java after using modern javascript / node.js lately: Java 8's CompletableFutures are basically Java's Promises, and they have good support for doing steps in specific thread pools. (Regular Futures are way too limited: if you want to do an action as soon as a Future resolves, then you have to dedicate a thread just to blocking on that Future, which is not exactly what you want if you have a lot of simultaneous asynchronous operations of multiple steps going on.)
Re:Interview with Ryan Dahl, Creator of Node.js by cdelsolar on Thursday August 31, 2017 @02:36PM (#15142669)
but why? Go's model is perfect. They're not real threads, it's basically very similar to Node, except that you can write code without a mess of callbacks and it's easier to reason about...
Re:Interview with Ryan Dahl, Creator of Node.js by stouset on Thursday August 31, 2017 @03:15PM (#15142978)
Go's model doesn't even statically prevent data races (as Rust's does). You might think Go's model is good, but it's a far cry from perfect.

Although I'll be happy to point out why Go's model isn't even good. Go's lack of ability to perform meaningful abstraction combined with the low level of its concurrency primitives means you end up having to copy/paste and slightly tweak dozens of lines of flow-control logic every time you use them.

This[1] is an excellent critique of Go's concurrency model.

[1]: https://gist.github.com/kachayev/21e7fe149bc5ae0bd878#so-wha...

Re:Interview with Ryan Dahl, Creator of Node.js by brightball on Thursday August 31, 2017 @05:32PM (#15144124)
Nothing's perfect. Go's is Erlang's with a shared memory model. That creates a lot of trade offs and limitations. In some places, it's a perk. In others, it's not.

No such thing as perfect, only trade offs.

Re:Interview with Ryan Dahl, Creator of Node.js by owaislone on Thursday August 31, 2017 @04:53PM (#15143827)
Not just that. It can use multiple OS threads and multiplex the goroutines on actual threads so programs can actually leverage multiple cores.
Re:Interview with Ryan Dahl, Creator of Node.js by int_19h on Friday September 01, 2017 @06:27AM (#15147327)
Go's model breaks down as soon as you have to invoke into non-Go code, because the latter has no idea about goroutines.

This is a general problem with all "green thread" abstractions - they are not OS level, or if they are (as e.g. fibers are on Win32), nobody cares to use them.

Promises, on the other hand, can be mapped to a straightforward callback-oriented C API, thus allowing a fully async execution stack end-to-end across different languages. You can even go one step further and define a uniform API for all such callbacks, as WinRT did.

Re:Interview with Ryan Dahl, Creator of Node.js by a13n on Thursday August 31, 2017 @03:16PM (#15142994)
async await though
Re:Interview with Ryan Dahl, Creator of Node.js by paulddraper on Thursday August 31, 2017 @03:19PM (#15143028)
And no shared-memory CPU parallelism.
Re:Interview with Ryan Dahl, Creator of Node.js by a13n on Saturday September 02, 2017 @04:42AM (#15154972)
No threading bugs though.
Re:Interview with Ryan Dahl, Creator of Node.js by davedx on Thursday August 31, 2017 @04:20PM (#15143563)
Elixir does it very well, I think.
Re:Interview with Ryan Dahl, Creator of Node.js by thomasz on Thursday August 31, 2017 @03:40PM (#15143232)
It's hardly the fault of the language if people are lying in their method signatures. I'm not aware of a mainstream PL with a type system that is powerful enough to avoid something like that.
Re:Interview with Ryan Dahl, Creator of Node.js by nulagrithom on Thursday August 31, 2017 @05:08PM (#15143933)
In this case, the .NET-provided base class defaults to using the synchronous version when the async version hasn't been overridden.

https://msdn.microsoft.com/en-us/library/system.data.common....

> Providers should implement this method to provide a non-default implementation for ExecuteReader overloads.

> The default implementation invokes the synchronous ExecuteReader method and returns a completed task, blocking the calling thread. The default implementation will return a cancelled task if passed an already cancelled cancellation token. Exceptions thrown by ExecuteReader will be communicated via the returned Task Exception property.

Found that after several hours of tearing my hair out trying to figure out why my async calls were still blocking...

Re:Interview with Ryan Dahl, Creator of Node.js by int_19h on Friday September 01, 2017 @06:25AM (#15147321)
This is a bug in the library. The correct way to do these "async but not really" wrappers is to call the synchronous implementation in a separate thread pool worker thread, and wrap that into a task (as Task.Run does). This is hardly optimal perf-wise, but it is truly asynchronous wrt to the caller. If I remember correctly, it's what Stream does for its default implementations of async methods.
Re:Interview with Ryan Dahl, Creator of Node.js by gshulegaard on Thursday August 31, 2017 @03:18PM (#15143022)
> Many are forgetting the initial reason node.js became popular. Consider the popular server-side landscape before node.js. It was dominated by Java, Python, etc. The primary way these ecosystems handle concurrency is OS-level threading. There was nothing else in the popular languages.

What? I am sorry but this is not true.

Coroutines have been a concept for...well a long time:

https://en.wikipedia.org/wiki/Coroutine

Green threads also have been around for a while.

> but these all had a critical flaw, which is the rest of the ecosystem didn't support it. Basically every library, almost all existing code used the threading model. And when you mix threaded libraries with a non-blocking I/O server, it completely falls apart because the threaded code blocks.

I am not sure how you can say this while using Python as an example. Python has the GIL which, typically viewed as a limitation, actually prevents the situation your assertion suggests. In fact, one of the largest barriers to removing the GIL is the wealth of libraries and C-extensions to Python that implicitly rely on the thread safety guarantees/limitations of the GIL (as covered by Larry Hastings as part of his first Gilectomy talk).

To be clear, there are reasons Node became popular, but let's not pretend that it brought some sort of concurrency revolution to the "server-side landscape" in 2009.

Edit: Heck, NGINX was an asynchronous, event driven web engine released in 2004 and written in C:

https://en.wikipedia.org/wiki/Nginx

Re:Interview with Ryan Dahl, Creator of Node.js by AgentME on Thursday August 31, 2017 @03:36PM (#15143208)
>Coroutines have been a concept for...well a long time: ... Green threads also have been around for a while.

What web frameworks were built around using them? Was there a good ecosystem of libraries that were easy to use with the web framework without introducing blocking I/O?

>I am not sure how you can say this while using Python as an example. Python has the GIL which, typically viewed as a limitation, actually prevents the situation your assertion suggests. In fact, one of the largest barriers to removing the GIL is the wealth of libraries and C-extensions to Python that implicitly rely on the thread safety guarantees/limitations of the GIL (as covered by Larry Hastings as part of his first Gilectomy talk).

Aren't all python threads backed by OS threads? And pointing out that threading has been used by too many Python libraries to change supports the previous comment's assertion that non-blocking I/O was hard to use exclusively in languages like Python because too many libraries used blocking I/O.

>Edit: Heck, NGINX was an asynchronous, event driven web engine released in 2004 and written in C:

Nginx is pretty specialized. Not really sure it's useful to compare it to a programming language / web framework with an ecosystem of compatible libraries like PHP, Ruby on Rails, django, nodejs, etc.

Re:Interview with Ryan Dahl, Creator of Node.js by btown on Thursday August 31, 2017 @04:57PM (#15143848)
I often wonder why Python's gevent didn't become more popular. It would monkey patch the socket libraries to yield to an event loop, allowing you to magically use blocking code and libraries with practically zero code changes, and zero callback hell. I imagine it had to do with marketing - when Node was released, everyone thought the spaghetti horror that was Twisted was the only other event-loop-based game in town.

Ironically, now that we have async/await to replace nested callbacks, Node finally has the code readability that gevent users took for granted all this time.

Re:Interview with Ryan Dahl, Creator of Node.js by bastawhiz on Thursday August 31, 2017 @07:33PM (#15144915)
> I often wonder why Python's gevent didn't become more popular.

Quite honestly, it wasn't very good. It worked magically until the magic ran out. Mysterious breakages caused by stuff deep in gevent interacting badly with other libraries hampered my ability to use it effectively. For instance, you couldn't use it with python-requests directly, you need to use another library that makes requests compatible, unless you roll your own magic, which was often very complicated. And, at least in my experience, gevent stuff tends not to work with Python 3.

Now that asyncio is a thing, I'm glad I never have to debug gevent-related problems anymore.

Re:Interview with Ryan Dahl, Creator of Node.js by cstrahan on Thursday August 31, 2017 @07:03PM (#15144730)
It should also be noted that there's another statically typed language that provides multiplexed M:N threading (aka "green threads") and a programming style that looks synchronous while actually using asynchronous file and network IO behind the scenes (so all of the perf benefits of JavaScript without the awkward, nested callbacks and/or promises).

The language I'm referring to is none other than Haskell (which has been around in some form since 1990).

Here's the earliest paper I know of detailing the internals of the Glasgow Haskell Compiler's IO event handling system (from 2010, I believe): https://static.googleusercontent.com/media/research.google.c...

And here's a more recent update: http://haskell.cs.yale.edu/wp-content/uploads/2013/08/hask03...

(Just in case of link-rot, the titles are respectively: "Scalable I/O Event Handling for GHC", "Mio: A High-Performance Multicore IO Manager for GHC")

Here's a short example:

  main = do
      putStrLn "Hello, what's your name?"
      name <- getLine
      putStrLn ("Hey " ++ name ++ ", you rock!")
Unfamiliar syntax aside, there's not a callback in sight -- it looks just like standard, imperative, synchronous code.

It drives me crazy when people tout Node's approach to concurrency as being ideal, as if callbacks are a necessity.

They're not.

Haskell, among other languages, is an existence proof that you can have lightweight threads and asynchronous IO without callback spaghetti. And I haven't even gone into Haskell's threaded parallelism, Software Transactional Memory, etc.

Re:Interview with Ryan Dahl, Creator of Node.js by Tobani on Thursday August 31, 2017 @07:30PM (#15144898)
Your still missing the point. It's not that it was possible to do so. It's that you could quickly write web services idomatically that did so by default. Your example shows a simple hello world and doesn't speak to the point being made. I am sure you can get a more relevant example going in Scotty, but it appears that it didn't show up until 2012.
Re:Interview with Ryan Dahl, Creator of Node.js by cstrahan on Friday September 01, 2017 @06:00PM (#15152711)
> Your still missing the point.

No, I'm not missing the point.

> Your example shows a simple hello world and doesn't speak to the point being made.

Hmm... Let's do an analysis:

* My example demonstrates reading from a given file descriptor (FD 0 -- stdin -- in this case), and that reading happens asynchronously (as I had pointed out, and even provided white papers describing how this works). Again, this is despite the syntax looking like a naive, synchronous program. The GHC Run Time System handles asynchronously reading from the FD and scheduling threads as appropriate such that OS threads aren't tied up waiting on what would otherwise be a synchronous read(2).

* As a bonus, it also demonstrates writing to standard out.

Now, one could take exception to the fact that my example doesn't give any evidence that similar asynchrony should be expected in a web stack. One would be wrong: as anyone with, say, as little as a year of experience writing web applications on POSIX systems should know, the Berkeley sockets API exposes sockets as file descriptors, so the same generic async event handling system in GHC's RTS applies equally well to networked IO. GHC and Node (via libuv) uses similar primitive kernel APIs at the end of the day, the only difference being that Haskell doesn't require that you reify the callbacks in the source language.

Idiomatic? Check.

By default? Check.

So no, I think I do get the point, but if you still disagree, I'm all ears.

If my tone sounds harsh, it's because I spent the time to provide links to two papers and even gave a small example of what the syntax would look like for a minimal program that demonstrates IO. In turn, you're telling me that I'm missing the point. I would argue that, if you read either of those two papers, and you knew about the implications with respect to networked IO (if you didn't, you could easily research the Berkeley sockets API, kqueue(2), poll(2), etc -- all mentioned in the papers), you would see what I'm getting at with my example code and the papers referenced. I can only posit that you haven't read the papers, or if you did, you must have not done so in earnest. Hopefully you can see how I would be frustrated when I've put in time to research this space, and when I share something, I'm met with what (in appearance) amounts to a dissimilar level of diligence and, ultimately, an empty dismissal.

If something else is going on, I would be more than happy to issue an apology.

> I am sure you can get a more relevant example going in Scotty, but it appears that it didn't show up until 2012.

Open source web frameworks for Haskell go back to (at least as far as) 2009.

Happstack: (2009): https://hackage.haskell.org/package/happstack-server-0.1

Yesod (2010): https://hackage.haskell.org/package/yesod-0.0.0

Snap (2010): https://hackage.haskell.org/package/snap-0.3.0

I didn't provide a "hello world"-like example of a web app because my command-line example only requires a trivial inference on the part of the reader to realize that the same asynchronous treatment of FDs there applies equally well to sockets, and thus web app programming in general; I deemed a full fledged web app example as more complicated than necessary to get my point across.

For what it's worth, here's an example in Happstack, straight from the documentation:

  module Main where
  
  import Happstack.Server (nullConf, simpleHTTP, toResponse, ok)  

  main :: IO ()
  main = simpleHTTP nullConf $ ok "Hello, World!"
And ...

  $ curl http://localhost:8000/
  Hello, World!
Also, a note on tense:

> It's that you could quickly write web services idomatically that did so by default.

I wasn't questioning the rationale for using Node back in 2009. My point is this: eight years have passed since Node was released, and alternative solutions have since been released. Somehow the Node community (and the greater web dev community in general) are oblivious to these alternative approaches.

I suspect this is because most web devs see the callbacks as evidence that IO is asynchronous in Node, and at the same time lack the requisite experience to intuit how another language might implement asynchronous IO while providing a syntax that looks like the usual synchronous model. Now, they could actually read the research papers put out, but they don't, and so everyone keeps going on about the technological revolution that is Node and its approach to IO, oblivious to alternative solutions that (arguably) provide a more convenient programming model (and is, if we throw in support for multiple threads, more computationally powerful, too).

Such laziness and/or lack of curiosity is pervasive in the industry, and that's frustrating.

Re:Interview with Ryan Dahl, Creator of Node.js by Tobani on Saturday September 02, 2017 @12:17PM (#15156747)
Your assertion is essentially that an example of reading and writing from/to a file descriptor shows all needed for all aspects of a networked application. I think that is absurd. And unless you have a multithreaded multiuser shell there is not even a really way to empirically test your program without going through absurd hoops. And in fact if you run an old enough version of Haskell, or non-ghc it might not be multithreaded/evented.

Your indinctment that the reason people aren't using your language of choice as lazy is ironically lazy. There are plenty of practical reasons for using different languages and tools.

Re:Interview with Ryan Dahl, Creator of Node.js by z3t4 on Saturday September 02, 2017 @07:40AM (#15155456)

  function sendMessage(message, toClients, callback) {
    var messagesSent = 0;
    for(var i=0; i<toClients.length; i++) sendTo(toClients[i]);
    
    function sendTo(client) {
      client.send(message, function messageSent(err) {
        messagesSent++;
        if(err) console.warn(err.message);
        if(messagesSent == toClients.length) callback();
      });
    }
  }
Re:Interview with Ryan Dahl, Creator of Node.js by esfandia on Thursday August 31, 2017 @07:27PM (#15144883)
> Edit: Heck, NGINX was an asynchronous, event driven web engine released in 2004 and written in C:

In the interview, he mentions himself that Node was inspired by Nginx.

Re:Interview with Ryan Dahl, Creator of Node.js by ricardobeat on Friday September 01, 2017 @12:08PM (#15149715)
Remember the "C10K challenge"? Show me what else could achieve those ten thousand concurrent connections back then (hint: no python).
Re:Interview with Ryan Dahl, Creator of Node.js by ungamed on Tuesday September 05, 2017 @03:44AM (#15173244)
Erlang -easily- acheived the C10k challenge back then.
Re:Interview with Ryan Dahl, Creator of Node.js by sreque on Thursday August 31, 2017 @02:59PM (#15142834)
Node.js brings callback hell to the serverside world of threads and we're supposed to be grateful? It's like you don't know the history of computing. Callback-based code was the original programming model in UNIX dating back decades! Threaded code came about because it's far easier to write it than callback-based code.

The ONLY reason to do callback-based code is for performance reasons: for network-IO-heavy apps there is less OS-level overhead from managing thousands of connections with a few threads using the OS-provided non-blocking I/O operations than managing a connection per thread. Even then, if you actually want to do better than connection-per-thread, you have to avoid the POSIX API for non-blocking I/O and use whatever OS-specific API is available for better performance (epoll, kqueue, or completion ports).

The greatest joke of all though is that Node.js is dirt slow because Javascript is dirt-slow. Yes, you may be able to invent some microbenchmark where Node.js looks good, but the only reason it does in that benchmark is because the Node.js app is spending 99.9% of it's time executing code in a C library. Any real-world Node.js app is going to be slow because Javascript in general, even on V8, is anywhere from 10-100x slower than C or even well-written Java.

And worst of all, Node.js is single-threaded! If you want to scale onto all your cores you have to go multi-process, which is an incredible pain in the general case. So, you switch to a runtime and framework that forces a more difficult way of programming on you (callback hell) but which is better for general networking performance, but you use a dirt-slow, single-threaded runtime while doing so. You are basically ending up with the worst of both worlds, and any decent development team will run circles around you in performance and productivity with just about any other backend stack.

Re:Interview with Ryan Dahl, Creator of Node.js by disordinary on Thursday August 31, 2017 @05:59PM (#15144320)
V8 is not dirt slow. You may dismiss benchmarks where Node looks good as "invented" but there are countless real world examples of people moving from other languages to node and experiencing a huge speed increase.

I know of one startup that had a fortune 500 company sign up to their service and their dotnet APIs couldn't hold the load and the cost of scaling them wasn't sustainable. They rewrote the hardest hit APIs in node over a weekend and were shocked at the improvements. I also know another startup that had a similar experience, they moved off of Ruby to node and were shocked at the speed increase that they were getting. They couldn't be competitive with their competition with their old codebase.

Despite what I just said above speed isn't necessarily the most important factor. Infrastructure is cheap and developers are expensive. If I'm asked what the best programing language or platform is my answer is usually the language and platform that your team is most productive with. Only a few companies get successful enough that speed is the deciding factor in platform choice, if we say that productivity is the most important factor then the JS/Node combination is right up there as one of the more understood and productive systems.

Re:Interview with Ryan Dahl, Creator of Node.js by jonnycoder on Friday September 01, 2017 @01:34AM (#15146370)
You are spot on as I have been developing in node for the past 2 months. Coming from doing python and c# in the past, I picked up the asynchronous nature + promises and their "gotchas" within a few days of writing and rewriting an activemq module. All of our other requirements/stories have been developed at such a high speed relative to my past projects. Combined with docker and a QA team that writes automated tests with postman and protractor, our continuous integration is at a high maturity level.
Re:Interview with Ryan Dahl, Creator of Node.js by ralusek on Thursday August 31, 2017 @03:23PM (#15143076)
I should definitely make a macro at this point:

Callback paradigm has not been used in node development for 3-4 years at this point.

    return someHttpCall()
    .then(response => someOtherHttpCall(response))
    // Do 2 async operations in Parallel:
    .then(response => Promise.join(
      someCacheThing(response),
      someDBThing(response)
    ))
    .spread((cacheResponse, dbResponse) => sendBackToClient());
This isn't callback hell. Without creating or managing threads, I have established serial as well as an example of branched concurrent logic that is entirely nonblocking. Every promise is handled as a try catch, so error handling can be done at the end of the chain with a single `.catch`.

Clustered express application puts the performance well within what you would expect from Go/Phoenix, all of which are operating at an order of magnitude above Python/Ruby.

Re:Interview with Ryan Dahl, Creator of Node.js by sreque on Thursday August 31, 2017 @04:03PM (#15143423)
I'm well aware of promises, thank you. I'm also well aware that there's very little difference in reality between a promise-based style of coding and a callback one. You still lose stack continuity, it's still much harder to chain operations that hop stacks, it's still a nightmare to debug compared to stack-based programming, etc.

If you've ever actually implemented a promise it's even easier to see how little promises actually get you. Given a callback-based API, it's absolutely trivial to wrap it in a Promise-based API. Fundamentally this is because promises give you very little in the end on top of callbacks beyond some syntactic sugar.

Re:Interview with Ryan Dahl, Creator of Node.js by ralusek on Thursday August 31, 2017 @04:38PM (#15143713)
First of all, I have absolutely implemented promises from scratch, and it IS trivial. However, "callback hell" is a reference to writing code with deeply nested callbacks, particularly with manual error handling at each level, both of which promises completely negate.

If you want to change the topic from "callback hell" to debugging across stacks, then we can do that. There was some work with "domains," since moved to AsyncListener/Continuation Local Storage to keep context across stack calls, but I don't disagree that improvements in this department are the place that NodeJS paradigm would currently benefit from most.

Re:Interview with Ryan Dahl, Creator of Node.js by sreque on Thursday August 31, 2017 @05:00PM (#15143878)
It's still "callback hell" because you're still fundamentally writing code where you invoke an API and pass in a callback to handle the results. The fact that the callback you pass in happens to be a promise that allows you to stage other callbacks and/or promises doesn't really change things enough for the label "callback hell" to be inaccurate in my opinion.

A different programming paradigm, for instance, would be the use of delimited continuations such that the API you invoke suspends the current call stack, executes the I/O operation, then resume the call stack with the result of the I/O operation. Very few languages support that. One reason is that natively supported continuations in the programming language has far reaching consequences in terms of performance.

I'm not an expert on golang, but I believe this is essentially how goroutines are implemented: https://groups.google.com/forum/#!msg/golang-nuts/t3g8NbITYE...

Also keep in mind that I'm not 100% against callback-based programming. I have, for instance, implemented a non-blocking client for a custom TCP-based protocol using Netty. The client performed better than a blocking version that it was meant to replace. I played around with using Promises in conjunction with the library but ended up preferring callbacks to Promises for a number of reasons, some of which are specific to the JVM.

Re:Interview with Ryan Dahl, Creator of Node.js by Johnny_Brahms on Monday September 04, 2017 @02:02AM (#15166038)
> A different programming paradigm, for instance, would be the use of delimited continuations such that the API you invoke suspends the current call stack, executes the I/O operation, then resume the call stack with the result of the I/O operation.

guile has recently gotten guile-fibers which works like this. It is really an amazing way to do concurrency.

Re:Interview with Ryan Dahl, Creator of Node.js by dleslie on Thursday August 31, 2017 @09:17PM (#15145410)
Callback hell may as well be synonymous with stack incoherence. The hell arises from not knowing from whence you came.
Re:Interview with Ryan Dahl, Creator of Node.js by vilmosi on Thursday August 31, 2017 @05:10PM (#15143963)
>>> I'm also well aware that there's very little difference in reality between a promise-based style of coding and a callback one.

Then you're not using promises as they are intended to be used.

Re:Interview with Ryan Dahl, Creator of Node.js by mahyarm on Thursday August 31, 2017 @04:54PM (#15143834)
How do you get stack continuity in anything that is multithreaded unless the debugger adds special support for it?
Re:Interview with Ryan Dahl, Creator of Node.js by sreque on Thursday August 31, 2017 @05:01PM (#15143885)
By programming using synchronous blocking code within a single thread, of course.
Re:Interview with Ryan Dahl, Creator of Node.js by ehsanu1 on Thursday August 31, 2017 @04:42PM (#15143753)
How about that await though.
Re:Interview with Ryan Dahl, Creator of Node.js by qooleot on Thursday August 31, 2017 @10:04PM (#15145579)
Promises made some very valuable improvements to code patterns relative to callback-based implementations, but as a broader "nodejs vs synchronous style code" its still much harder. You sound pretty versed, but other folks might struggle. Here is an example explanation:

https://pouchdb.com/2015/05/18/we-have-a-problem-with-promis...

If we're trying to show the simplicity of modern nodejs code with non-blocking I/O, I would suggest we lead with examples like:

const foo = await queryFooObject(id); await writeToFile(foo);

True, there's still plenty of value in promises, especially for things like:

await Promise.all(severalParallelQueries);

Re:Interview with Ryan Dahl, Creator of Node.js by brondeau on Thursday August 31, 2017 @03:34PM (#15143177)
As with any tool, there is a right and wrong way/time to use it. Node.js would not be the monster success and influencer it is today if it was the 'worst of both worlds'. For example, using a single threaded environment can solve so many issues that are present in a multi-threaded environment, such as reducing possibility of race conditions (though you can still write crappy single threaded code that introduces race conditions). When used in the right way, it can be and is best of class. In other instances, it is a boat anchor. Just my 2 cents
Re:Interview with Ryan Dahl, Creator of Node.js by WA on Thursday August 31, 2017 @03:12PM (#15142941)
So if I don't like Java and C, what's your suggestion I should use instead to get the best of both worlds and not the worst?
Re:Interview with Ryan Dahl, Creator of Node.js by jswizzy on Thursday August 31, 2017 @05:56PM (#15144304)
Any language that uses the actor model for concurrency which is basically every language created in the last decade. Elixir, GO, Akka framework for Java and C#, Pony, Dart, Erlang, ELM, SCALA, ...
Re:Interview with Ryan Dahl, Creator of Node.js by bonesss on Friday September 01, 2017 @05:49AM (#15147220)
F# with it's built in actor model, robust OCaml based syntax, great support for functional Akka[.net] actors, simplified concurrency and asynch programming support, and very own ELM implementation is a great contextual answer for anyone who wants to be on the front lines of lightweight non-blocking concurrent & parallel web development in .Net land.
Re:Interview with Ryan Dahl, Creator of Node.js by sreque on Thursday August 31, 2017 @04:07PM (#15143457)
C# is fairly efficient while still having managed memory. And, of course, golang was designed to hit the sweet spot of automatic memory management, decent performance, and non-blocking I/O.

Beyond that, there's things like Erlang and Elixer, which I am less familiar with. My understanding is that the Erlang VM is significantly slower than JVM or CLR implementations but generally faster than scripting language runtimes.

Re:Interview with Ryan Dahl, Creator of Node.js by lackbeard on Friday September 01, 2017 @02:04AM (#15146489)
Go, Erlang, Elixer, Haskell, Clojure, Scala.
[dead]
Re:Interview with Ryan Dahl, Creator of Node.js by sreque on Thursday August 31, 2017 @04:05PM (#15143443)
I have written servers in C with epoll that were used in production, thank you. And yes, I'm aware of async/await and its limitations. State-machine-style transformations from blocking-looking code to non-blocking code are definitely helpful, but in the end still fall short in terms of usability to a full blocking style of programming.

To even begin to approach the ease-of-use of blocking-style programming you need something akin to delimited continuations in your programming language.

Re:Interview with Ryan Dahl, Creator of Node.js by cmiles74 on Thursday August 31, 2017 @02:07PM (#15142399)
I think the only new thing that node.js brought to the table was a nice way to run Javascript on the server. Node.js doesn't use threads, but as you said, their event-driven non-blocking I/O model wasn't new (Python had Twisted and Java had Netty). In addition, Java has had non-blocking I/O (in the form of the nio) packages for quite some time.

In my opinion, Node.js became popular because people wanted to run Javascript in a server environment.

Re:Interview with Ryan Dahl, Creator of Node.js by AgentME on Thursday August 31, 2017 @03:20PM (#15143043)
>Node.js doesn't use threads, but as you said, their event-driven non-blocking I/O model wasn't new (Python had Twisted and Java had Netty).

The new thing that it brought was an entire environment that used non-blocking I/O exclusively without any pre-existing thread-based code and libraries. With node.js, you don't have to wonder if a library you want to adopt supports non-blocking I/O.

Re:Interview with Ryan Dahl, Creator of Node.js by disordinary on Thursday August 31, 2017 @05:50PM (#15144265)
That's exactly the point and what people are missing. If you used Twisted you didn't have access to the entire Python ecosystem and there was a lot of work that was required to get standard packages to work in a non blocking way.

Also callbacks for all that they're derided now were an easy paradigm to understand to get into the whole philosophy of async. The first prototypes of node supported different paradigms before callbacks were settled on IIRC.

Re:Interview with Ryan Dahl, Creator of Node.js by sporkland on Thursday August 31, 2017 @02:35PM (#15142658)
I remember distinctly at the time that the fact that there was excitement around node.js because it was built from the ground up to use evented IO exclusively. All the libraries, the core runtime etc. People in other languages (C) were for sure doing it, but not in the holistic way node was. As opposed to java and python where you needed to avoid using the library calls that were blocking.
Re:Interview with Ryan Dahl, Creator of Node.js by cmiles74 on Thursday August 31, 2017 @03:16PM (#15142990)
Fair enough, I agree that Node.js uses the evented model (AFAIK) almost exclusively. Still, the evented model is not unique to Node.js nor did they invent it. In my opinion, it was the dedication of Javascript developers and their reluctance to use another language that popularized this model: they had no choice.
Re:Interview with Ryan Dahl, Creator of Node.js by sporkland on Thursday August 31, 2017 @05:23PM (#15144061)
Yeah. Ryan Dahl was pretty clear at the time that he was inspired by people using C and achieving really high connection counts on single machines. So he for sure was inspired by others, and obviously built off the same foundations in the kernel.

At some point Isomorphic Javascript became the main reason to use it, as golang has largely stolen the non-blocking IO crown with its implementation of green threads.

Re:Interview with Ryan Dahl, Creator of Node.js by bryanrasmussen on Friday September 01, 2017 @02:44AM (#15146637)
There were lots of ways to run JavaScript on the server before Node, however all the ways I am familiar with suffered from having either poor support, poor performance, or being marginally difficult to get to work.

JavaScript on the server before Node was a second-class solution.

On edit: so yeah, a nice way.

Re:Interview with Ryan Dahl, Creator of Node.js by z3t4 on Thursday August 31, 2017 @02:38PM (#15142687)
You can write JavaScript in classic ASP, there are many server JavaScript, but they are serial/blocking, and NodeJS is async/non blocking.
Re:Interview with Ryan Dahl, Creator of Node.js by lackbeard on Thursday August 31, 2017 @09:24PM (#15145445)
Your claim is that Node was popular because people were excited about writing servers using 100% non-blocking I/O? I guess I agree that's a part of it but we should note that in 99.5% of those cases the servers would've performed better written in Java using blocking threads.

I think the other parts of it are: 1) many people wanted to write server-side Javascript, 2) people did not want to use Java, and the concurrency stories for PHP, Ruby, and Python were (and still are) really crummy.

Re:Interview with Ryan Dahl, Creator of Node.js by pdeuchler on Thursday August 31, 2017 @06:01PM (#15144329)
Errrrr what? No, node.js became popular because it allowed the legions of front end devs to overnight become full stack devs without learning anything but a new framework. To a lesser extent it also provided an alternative to PHP in the "get a simple crud app up and running with minimal knowledge" space, but I think that's far dwarfed by the ability of front end devs to start leveraging their skills on the backend.
Re:Interview with Ryan Dahl, Creator of Node.js by lsdafjklsd on Thursday August 31, 2017 @06:50PM (#15144655)
There weren't really 'legions' of front end devs when node came out. People were just learning backbone.js and the idea of SPA's being viable was still forming. It was not easy to pick up if you were coming from something like rails, because it had no well documented complete framework solution. The people that were doing node in the early days were legit back end folks. I am now a front end developer, but I learned on rails and I remember having a hard time 'learning' node. It had no debugger (I was used to something like pry) and was generally a collection of 'low level' libraries.
Re:Interview with Ryan Dahl, Creator of Node.js by valarauca1 on Thursday August 31, 2017 @02:01PM (#15142346)

    Each language had some niche library that did
    non-blocking I/O [...] Netty for Java)
Non-blocking IO has been in java standard library since 1.4

More then Netty supported it. Netty, Jetty, Dropwizard, Grizzly, VERTX. Apache Tomcat supported non-blocking IO 5 years ago.

Re:Interview with Ryan Dahl, Creator of Node.js by _pmf_ on Thursday August 31, 2017 @04:19PM (#15143551)
To recap for the non-Java reader: the problem was that the Servlet specification did not initially support suspending and resuming without suspending and resuming the thread handling the request. The Servlet 3.0 specification supports it, but before you could not use multiplexed IO within containers that required the servlet interface, even though IO multiplexing worked perfectly fine (and, as you said, some containers allowed it via a custom interface, since it is pretty much trivial to add).
Re:Interview with Ryan Dahl, Creator of Node.js by tmp124567 on Thursday August 31, 2017 @03:16PM (#15142995)
This. I'am allways surprised by the amount of developers that don't understand the difference between threads, epoll-select and forking and io bound vs cpu bound.
Re:Interview with Ryan Dahl, Creator of Node.js by turtlebits on Thursday August 31, 2017 @02:52PM (#15142785)
As a Python/Ruby web dev, exactly this. Dealing with uwsgi/passenger was so painful to me. And asyncio in python3 still boggles me.
Re:Interview with Ryan Dahl, Creator of Node.js by 52-6F-62 on Thursday August 31, 2017 @03:01PM (#15142844)
I like Python a lot, and use it for a number of things even though my position doesn't require it where I work, but uwsgi was the thing that kept steering me away from it. I'll probably make myself master it at some point, but it just felt fumbling at first take and left a bad taste.
Re:Interview with Ryan Dahl, Creator of Node.js by InclinedPlane on Thursday August 31, 2017 @05:18PM (#15144018)
Yup.

Node.js isn't quite as old as you make it out to be, there were plenty of mature tech stacks out there with PHP/Perl, Ruby, C#, etc. Many of them perfectly fine to work with. Node.js came along with a very different conceptualization of scalability relative to other web-servers, and that was what drew people to it. Today we have things like varnish, nginx, fast-cgi, etc. as well as much faster servers which make it a lot easier to scale out web stuff without having to redesign it from the ground-up, but node.js was for its time a hugely advantageous way to approach scaling. And it still is for many use cases, but today it's matured into something that has a lot more to offer.

Re:Interview with Ryan Dahl, Creator of Node.js by thehardsphere on Thursday August 31, 2017 @06:22PM (#15144472)
Not to be a pedant, but all of the projects you mention in "today we have" pre-date Node and were commonly available.
Re:Interview with Ryan Dahl, Creator of Node.js by k__ on Thursday August 31, 2017 @01:53PM (#15142258)
Was it?

I had the feeling before Node.js, everything was PHP.

Re:Interview with Ryan Dahl, Creator of Node.js by disordinary on Thursday August 31, 2017 @06:09PM (#15144383)
Most things are still PHP thanks to Wordpress.
Re:Interview with Ryan Dahl, Creator of Node.js by thehardsphere on Thursday August 31, 2017 @06:19PM (#15144450)
They're both appealing to "web developers" of relatively less experience for similar reasons.
Re:Interview with Ryan Dahl, Creator of Node.js by jdc0589 on Thursday August 31, 2017 @03:33PM (#15143166)
> Since server-side JS didn't exist yet,

I'm being petty, but serverside JS did exist before node (e.g. Narwhal, which I believe is where jsgi and a lot of the commonjs stuff originated)

Re:Interview with Ryan Dahl, Creator of Node.js by pawelk on Thursday August 31, 2017 @03:59PM (#15143393)
There was also Rhino in the Java land (I remember working with Helma before Node came out), JScript on IIS, some projects embedding SpiderMonkey.

I think node.js finally made server-side JS take off because 1) it was based on V8 which was the new, performance-oriented JS engine powering Chrome, and 2) it was self-contained (i.e. didn't rely on Java on one hand, wasn't just a scripting layer of something larger on the other).

Re:Interview with Ryan Dahl, Creator of Node.js by jdc0589 on Wednesday September 13, 2017 @11:16AM (#15239026)
> some projects embedding SpiderMonkey

yea, I remember the woes of building CouchDB from source....about half the issues generally seemed to be SpiderMonkey related.

Re:Interview with Ryan Dahl, Creator of Node.js by pawelk on Wednesday September 27, 2017 @08:13AM (#15346959)
On the other hand you could use E4X in CouchDB views, which was almost like JSX, but native.
Re:Interview with Ryan Dahl, Creator of Node.js by icedchai on Thursday August 31, 2017 @08:37PM (#15145239)
Server side JS is older than that. It existed over 20 years ago. Does anyone remember Netscape Livewire?

It was basically "ASP-style" server-side javascript that ran on Netscape's "Enterprise" web server. It was a little unstable, but pretty advanced for the time (had DB connectivity, etc.)

Re:Interview with Ryan Dahl, Creator of Node.js by int_19h on Friday September 01, 2017 @06:30AM (#15147334)
While we're at it, ASP itself had JavaScript (well, JScript, technically) support alongside VBScript, out of the box. It wasn't particularly popular, probably because anyone doing ASP was likely to be using the rest of MS stack - and that meant VB for desktop LOB apps, usually.
Re:Interview with Ryan Dahl, Creator of Node.js by ori_b on Thursday August 31, 2017 @08:11PM (#15145122)
OS-level threading is often pretty good -- you can usually fork and join tens of thousands of threads per second. (I believe that the initial NPTL benchmarks showed something like 20 microseconds for thread spawn.) It's not the tens of millions per second that you get with green threads, but thread creation has an unfair reputation of being slow.

It was once true, but that was 20 years ago.

Re:Interview with Ryan Dahl, Creator of Node.js by endorphone on Thursday August 31, 2017 @05:03PM (#15143894)
You're getting a lot of pedantic replies, but your post is spot on. At the time it arrived, asynchronous operations were close to impossible in PHP, very difficult and awkward in .NET, and very uncommon in Java. In Node it not only was possible, it was impossible (without going to great efforts) to do otherwise.

People have a short memory, but at the time getting single to double digits of requests per second on a beefy server was entirely typical. Yes, someone somewhere could demonstrate an alternative, but node completely changed what was normal. It was extremely high performance for the time.

Re:Interview with Ryan Dahl, Creator of Node.js by debaserab2 on Thursday August 31, 2017 @08:28PM (#15145216)
> People have a short memory, but at the time getting single to double digits of requests per second on a beefy server was entirely typical.

Umm, what? It was not a challenge to get to double digits of requests per second on a beefy server in 2008, at least not in any popular language (including PHP and Ruby). It was (and still is) easy to scale requests of "blocking IO" languages by spawning additional threads at the server level.

Re:Interview with Ryan Dahl, Creator of Node.js by patrickthebold on Thursday August 31, 2017 @09:20PM (#15145420)
Along those lines: Has anyone seen benchmarks that show significant benefits for non-blocking IO?
Re:Interview with Ryan Dahl, Creator of Node.js by endorphone on Thursday August 31, 2017 @10:28PM (#15145680)
I said entirely typical, not that it was a dick-sized "challenge".

A completely standard developer on .NET or PHP in 2008 was building pages that rendered at less than 10 requests per second. This is experience, not a guess, given that my role was improving the performance of those disasters. A completely average developer on node was building pages that was 10x to 100x better performance.

Secondly, simply spawning threads is laughably non scalable.

Re:Interview with Ryan Dahl, Creator of Node.js by noblethrasher on Thursday August 31, 2017 @10:44PM (#15145728)
Not the OP, but I knew what you meant I soon as I read it. But, did the typical .NET app really not make use of the Asynchronous Programming Model that was part of the standard library?

I ask because in 2008 I was only about 3 years into professional web development, and even I knew about stuff like the C10K problem[1] and that I/O Completion ports were apparently one of the few things that Unix people admired about Windows.

[https://en.m.wikipedia.org/wiki/C10k_problem]

Re:Interview with Ryan Dahl, Creator of Node.js by endorphone on Friday September 01, 2017 @07:46AM (#15147582)
Their use was incredibly rare. It was common to find advice columns telling you that you don't need them.

https://blogs.msdn.microsoft.com/rickandy/2009/11/14/should-...

Re:Interview with Ryan Dahl, Creator of Node.js by debaserab2 on Friday September 01, 2017 @10:36AM (#15148637)
> Secondly, simply spawning threads is laughably non scalable

How else are you going to do it with a non-threaded language? I've ran well into the 10s of requests per second running "standard" PHP code on practically vanilla Apache configs. This was far from atypical and is no way any kind of challenge on a beefy machine.

My job was not to clean up disasters though. Maybe if your job was to clean up disasters, your typical was much different than mine?

Re:Interview with Ryan Dahl, Creator of Node.js by z3t4 on Saturday September 02, 2017 @01:15PM (#15157092)
The problem with multi threading like in PHP is that you need locks. It will not be a problem if you have low traffic but once you get hundreds of requests per seconds there's a high chance for errors like "double posts" where both threads say "post don't exist" and then both threads make the post, and you end up with double posts. Or racing conditions, double spending, etc. With NodeJS you get rid of all those problems because it's single threaded.
Re:Interview with Ryan Dahl, Creator of Node.js by debaserab2 on Saturday September 02, 2017 @02:12PM (#15157392)
The server spawns the threads and isolates the entirety of a single request to a single thread. It's not true language multithreading.
Re:Interview with Ryan Dahl, Creator of Node.js by z3t4 on Saturday September 02, 2017 @07:09PM (#15158736)
JavaScript is single threaded, but all IO operations like network and disk uses threads in Node.JS, So there are still racing conditions, like when accessing the file system. But all other operations are single threaded, like storing data in memory. In for example PHP, where the server spawns the threads everything becomes multi threaded. In node.js instead of using locks etc, you use callbacks, so when something is finished, a function is called ... It's just like event listeners in the browser. Many people complain about Node.JS being single threaded, but it's actually a big relief, they probably haven't been dealing with issues like locks and racing conditions.

To get cpu bound parallelism in Node.JS you have to spawn child processes. There's a built in module in NodeJS called "cluster" that abstracts this a bit. When talking to a child process you could just as well be talking to another machine over the network, the code will look the same, and scaling horizontally across many machines will be easier, compared to languages that solve concurrency by using threads instead of non blocking IO.

There's a learning curve though. It took me about six months to learn how to do async programming, manage messages between different processes and machines, and callbacks, I do remember the "callback hell", but now it has become a second nature, it's like brushing my teeth, but more fun. I don't think half a year is that bad, you would need just as long time to get comfortable in any other language. And I consider JavaScript very newbie friendly, you don't need a CS degree to write JavaScript.

Re:Interview with Ryan Dahl, Creator of Node.js by debaserab2 on Saturday September 02, 2017 @11:57PM (#15159610)
> In for example PHP, where the server spawns the threads everything becomes multi threaded.

No it doesn't. The only way to do truly multithreaded code in PHP is using pthreads.

Re:Interview with Ryan Dahl, Creator of Node.js by corford on Thursday August 31, 2017 @03:04PM (#15142871)
>because the threaded code blocks.

Python and Java threads don't block on I/O, only on CPU. Event loops (i.e. node) also block on CPU.

AFAIK, node's advantage over threads was just that it offered much higher concurrency for I/O constrained tasks for the same memory (and was easier to programme for people coming from the front-end world).

Re:Interview with Ryan Dahl, Creator of Node.js by int_19h on Friday September 01, 2017 @06:32AM (#15147341)
Python and Java threads block the thread on I/O. Which is wasteful, because you have a huge chunk of memory allocated for the stack of that thread, being reserved but not actually in use.
Re:Interview with Ryan Dahl, Creator of Node.js by corford on Friday September 01, 2017 @03:24PM (#15151667)
Yep, hence node's advantage of offering much higher concurrency for the same memory usage.
Re:Interview with Ryan Dahl, Creator of Node.js by mattschmulen on Thursday August 31, 2017 @11:03PM (#15145805)
All this and the developers that were driving new line of biz products were coming from the front end ( web and mobile ), of course all front ends need backends and when these these folks went looking they naturally leaned toward JavaScript the same language they were using on the front end and Async model
Re:Interview with Ryan Dahl, Creator of Node.js by tantalor on Thursday August 31, 2017 @08:58PM (#15145332)
Forgetting? It's mentioned in the 2nd paragraph of the article,

He showed us that we are doing I/O completely wrong and also taught us how to build software using pure async programming model