A pipelined, non-blocking, extensible web server in under 1400 lines of Scala

Web servers are easy to write, but hard to get right. Before I became a professor, I ended up writing a few web servers to circumvent scaling issues with Apache and PHP. I say "a few" because the first two I wrote were so awful that they had to be scrapped.

I had a chat with Greg Morrisett a few summers ago about what a "correct" (or "secure") web server should do, and it didn't take long to realize that even defining correct is a challenge.

As just one (important) example, a correct web server can't use blocking I/O, ever. If you make a blocking call, you've created a denial-of-service vulnerability. Writing a web server that uses blocking I/O calls isn't hard. But you can't take a web server with blocking I/O and then drop in non-blocking I/O; it requires a fundamentally more complex design.

In my research, I claim that constructs like coroutines make it easier to design systems software (like web servers), that they expose parallelism and that their inefficiencies can be optimized away by a compiler. Ironically, while I've implemented the coroutine optimizers, I'd never actually built a system out of coroutines.

Last night, I decided to test the coroutines-make-it-easier hypothesis by writing a web server that uses non-blocking I/O. (This also gives me a benchmark on which to test my optimizations.) This informal experiment lends some credibility to my claim, since I was actually able to build a reasonable web server in just one night, and the architecture is still extensible and clean. I don't feel the need to rewrite it.

Read on for the details and the code.