TCP: connections
Teardown, and who holds TIME_WAIT
Four segments to close politely, one to close rudely, and sixty seconds of consequence.
Closing is not the handshake in reverse. Each side closes its own half: a FIN and its ACK in each direction, four segments in total — unless one side has nothing more to say and can piggyback, which is why you often see three.
The side that closes first ends in TIME_WAIT and stays there for sixty seconds on Linux, holding its local port. That is who pays. A client that opens a connection per request accumulates them and runs out of ephemeral ports long before it runs out of anything else.
A RST skips all of it. No FIN, no acknowledgement, no TIME_WAIT — which is why a service that resets its connections does not accumulate them, and why a reset and a close are different information for a client deciding whether to retry.
Predict
A service handles ten thousand requests a second, one connection each, and runs out of ephemeral ports. Whose fault is TIME_WAIT?
Comes after The handshake, and what it costs.