Chapter 12. Concurrent Network Servers
Server-type applications that communicate with many clients simultaneously demand both a high degree of concurrency and high performance from the I/O subsystem. A good web server should be able to handle hundreds of thousands of concurrent connections and service tens of thousands of requests per second.
Ideally, we would like to write these kinds of applications using threads. A thread is the right abstraction. It allows the developer to focus on programming the interaction with a single client and then to lift this interaction to multiple clients by simply forking many instances of the single-client interaction in separate threads. In this chapter, we explore this idea by developing a series of server applications, starting from a trivial server with no interaction between clients, then adding some shared state, and finally building a chat server with state and inter-client interaction.
Along the way, we will need to draw on many of the concepts from
previous chapters. We’ll discuss the design of the server using both
MVar and STM, how to handle failure, and building groups of threads
using the abstractions introduced in Symmetric Concurrency Combinators.
A Trivial Server
In this section, we will consider how to build a simple network server with the following behavior:
- The server accepts connections from clients on port 44444.
- If a client sends an integer n, then the service responds with the value of 2n.
-
If a client sends the string
"end" ...