Why is my Hello World go server getting crushed by ApacheBench?

In short, you’re running out of ports.

The default ephemeral port range on osx is 49152-65535, which is only 16,383 ports. Since each ab request is http/1.0 (without keepalive in your first examples), each new request takes another port.

As each port is used, it get’s put into a queue where it waits for the tcp “Maximum Segment Lifetime”, which is configured to be 15 seconds on osx. So if you use more than 16,383 ports in 15 seconds, you’re effectively going to get throttled by the OS on further connections. Depending on which process runs out of ports first, you will get connection errors from the server, or hangs from ab.

You can mitigate this by using an http/1.1 capable load generator like wrk, or using the keepalive (-k) option for ab, so that connections are reused based on the tool’s concurrency settings.

Now, the server code you’re benchmarking does so little, that the load generator is being taxed just as much as the sever itself, with the local os and network stack likely making a good contribution. If you want to benchmark an http server, it’s better to do some meaningful work from multiple clients not running on the same machine.

Leave a Comment