One significant challenge in the implementation of large network services (such as peer-to-peer systems or massively multiplayer games) is that the system must accommodate tens of thousands of simultaneous, mostly-idle connections. Programming such concurrent software can be quite complex, and, historically, two programming models have been used, each with different tradeoffs. The first model, multithreading, gives programmers familiar structured-programming constructs (if-then-else, loops, exceptions, etc.), but makes it difficult to do asynchronous IO or custom scheduling. Event-driven programming, in contrast, provides low-level control over scheduling, which helps deal with asynchrony and can improve flexibility and performance, but makes reasoning about control flow difficult. Ideally, the programming language would seamlessly support both models: the programmer could use threads where they are the appropriate abstraction (e.g. for per-session code) and use events where they are more suitable (e.g. for custom scheduling and asynchronous IO).
In this talk, I will describe one approach to reconciling the multithreaded and event models in the Haskell programming language. I will show how, starting with a 'concurrency monad' abstract datatype, it is possible to build a simple, application-level concurrency library that supports programming with threads and events. The library crucially relies on Haskell's higher-order functions, lazy evaluation, and strong type system.
This hybrid programming model simplifies the development of massively concurrent software in a way that scales to real-world network services. Our Haskell implementation supports exceptions, symmetric multiprocessing, synchronization via software transactional memory, asynchronous I/O mechanisms and application-level network protocol stacks. Our experimental results demonstrate that this monad-based approach has good performance: the threads are extremely lightweight, and the I/O performance compares favorably to that of Linux NPTL.
This is joint work with Peng Li, and the talk content is based on the paper "Combining Events And Threads for Scalable Network Services" that appeared in PLDI 2007.