How do web servers avoid TIME_WAIT?

DougN picture DougN · Sep 17, 2010 · Viewed 8.5k times · Source

I'm writing a simple HTTP server and learning about TIME_WAIT. How do real web servers in heavy environments handle requests from thousands of users without all the sockets getting stuck in TIME_WAIT after a request is handled? (Not asking about keep-alive -- that would help for a single client, but not for thousands of different clients coming through).

I've read that you try and get the client to close first, so that all the TIME_WAITs get spread out among all the clients, instead of concentrated on the server.

How is this done? At some point the server has to call close/closesocket.

Answer

Len Holgate picture Len Holgate · Sep 17, 2010

The peer that initiates the active close is the one that goes into TIME_WAIT. So as long as the client closes the connection the client gets the TIME_WAIT and not the server. I go into this all in a little more detail in this blog posting.