News for hackers. Stuff that matters.
Re:Why Events Are A Bad Idea For High-Concurrency Servers (2... by rdtsc on Friday May 02, 2014 @03:07AM (#7684436)
Events (to translate to more common lingo) are another way today of saying callbacks, promises, async. Usually a big select/poll/epoll system call sits at the bottom of those. Callback chains (abstracted as promises, deferreds or futures for example) branch out from it. As in select-c1()->c2()->c3()...

The popularity of that been going up and down in waves for various reasons. Lack of multi-core machines in early 2000s was one reason. Fast CPU speeds was another. Expensive RAM was also important (threads would need a larger context while a callback context is very small).

This technique is performant still, but in specific situations and mostly in cases were callback chains are short. So something like select->c1()->c2() is nice. Some servers like HAProxy or nginx fall in that category. Those are battle tested very performant pieces of code. Another category is demos. Short examples of "look at my async code example". Those also have very shallow callback chains and also look simple and pretty. [+]

What doesn't work for events too well is deep callback chains. This where real world business logic gets to be implemented. With layers of API. Shopping carts, payment processing, various backend databases. Now you are up to ...->c14()->c15(). That doesn't work in my opinion. The concurrency context instead of being in one thread executing instructions c1....c15 is now spread across many function interspersed with then()'s or yields()'s. Oh and usually errors need to be handled, so the chain becomes a tree with some splits into errbacks for example.

I think we are seeing a second wave of resurgence of threads in forms of goroutines in Go. Rust tasks. Dart Isolates. That is a nice thing to see, I general I believe those are better at handling concurrency.

[+] Python greenlet module (gevent is based on it) is an interesting case of a hybrid. At its core it is based on an event loop but then switches to green threads (think of them as co-routines). So user effectively spawns threads and sends messages between them for example.

Re:Why Events Are A Bad Idea For High-Concurrency Servers (2... by ryanjshaw on Friday May 02, 2014 @06:25AM (#7684882)
I don't build web servers so perhaps I'm going off topic, but I have found it useful to sometimes combine both models - which may not be all that surprising because both models developed to satisfy different requirements, which can crop up in a single problem at the same time.

A concrete example: I'm experimenting with a small analytics service where one part of the implementation is that I need to scrape about ~10 million forum posts across ~500k web pages.

A purely event-driven approach makes scraping very easy but throttling requests, handling errors and retries quickly becomes messy and difficult to reason about. A purely thread-driven approach makes throttling much simpler but scraping harder.

I found that implementing scraping with events (i.e. push a URL to a queue + specify a callback type + state) and downloading with threads (i.e. process the queue(s) with a request throttling window) much more manageable with some nice side benefits (e.g. persistent queues are easy to implement).

I haven't looked at goroutines, Rust tasks or Dart Isolates -- are you suggesting that one model can be sufficient for general purpose computing?

Re:Why Events Are A Bad Idea For High-Concurrency Servers (2... by jlouis on Friday May 02, 2014 @05:33AM (#7684764)
How could you forget Erlang? It has been avoiding events for years and has done so with high success. Same with Haskell and Standard ML :)
Re:Why Events Are A Bad Idea For High-Concurrency Servers (2... by lmm on Friday May 02, 2014 @06:13AM (#7684861)
I think of Erlang as an event-oriented system; actor messages do not necessarily receive exactly 1 reply (or any reply at all), which seems to be the critical property that the paper is talking about. Why do you say Erlang avoids events?
Re:Why Events Are A Bad Idea For High-Concurrency Servers (2... by pron on Friday May 02, 2014 @07:18AM (#7685033)
The key point discussed in the article is blocking. Erlang processes, like Go's goroutines and Quasar fibers on the JVM block without sacrificing any of the performance associated with asynchronous callback-based styles. The article says that there is no problem with the thread abstraction, and blocking in particular -- there's just a problem with the implementation of kernel threads that makes blocking expensive.

Callbacks, and the various attempts to make them more palatable (composable promises, monads), change the abstraction to get around implementation problems. Lightweight threads (or user-mode threads) simply fix the implementation, and preserve the threading/blocking abstraction.

Re:Why Events Are A Bad Idea For High-Concurrency Servers (2... by oelmekki on Friday May 02, 2014 @03:23AM (#7684476)
Sorry if this is a naive question (I'm more a user than a developer in that field), but isn't the point of multiprocess webservers precisely to keep that callback chain small (especially if we talk about multiple API calls, which is spawned in multiple requests) ?
Re:Why Events Are A Bad Idea For High-Concurrency Servers (2... by rdtsc on Friday May 02, 2014 @03:36AM (#7684515)
A multiprocess web server (like Apache for example) would usually not have callback chains. It would listen on a socket, in the accept() call, and when a new connection comes in it would return with a client connection socket and then spawn a process (or pick one from an existing pool) and let it handle it. It wouldn't have any callback chains (or I guess you can think of the spawn() / fork() / pick_from_process_pool() as 1 callback ).

If it is OS processes (or Erlang lightweight processes) then you gain failure isolation. If something has gone wrong and a child process crashes, only that request will fail, others will keep working. Real OS processes are usually on the heavy side.

In Go you might create a channel and spawn a goroutine for example.

Re:Why Events Are A Bad Idea For High-Concurrency Servers (2... by oelmekki on Friday May 02, 2014 @03:45AM (#7684531)
Ok, thanks. So I guess we're talking about events as a problem for things like node.js or eventmachine.

Out of curiosity, you mentioned nginx as using callback chain ; has it any difference with apache in that regard ?

Re:Why Events Are A Bad Idea For High-Concurrency Servers (2... by rdtsc on Friday May 02, 2014 @04:07AM (#7684579)
It is usually consumes less memory for the same amount of concurrent connections. The callback chain for it should be pretty short. Instead of spawning a process, it would get an event that there is data on a client socket. Then parse http (this might happens across multiple callbacks, and then handle it (serve the file).
Re:Why Events Are A Bad Idea For High-Concurrency Servers (2... by nitrogen on Friday May 02, 2014 @11:57AM (#7686434)
I'm a fan of EventMachine, EM-synchrony, and Sinatra-synchrony for a Fiber-based cooperative Ruby green threads system backed by an event reactor, but the author of at least one those projects has abandoned the concept entirely in favor of native threads.