Skip to content

Instantly share code, notes, and snippets.

@oddmario
Last active June 24, 2024 09:10
Show Gist options
  • Save oddmario/3782033e4efac2b09b48f135a9273060 to your computer and use it in GitHub Desktop.
Save oddmario/3782033e4efac2b09b48f135a9273060 to your computer and use it in GitHub Desktop.
Tuning a Golang (Go) HTTP server

Tune a Go http.Server

by adjusting its default buffer sizes


In some situations, you may need to adjust the default buffer sizes of a Golang HTTP server.

Adjusting the default buffer sizes allows you to benefit from higher throughput and improve the performance of your HTTP servers overall.

For the cherry on the top,

You may also tune the performance of your Linux kernel overall [if you are hosting your application on a Linux system]

I recommend https://github.com/klaver/sysctl as a good starting point on this.

To avoid the occurrence of any inconveniences, please make sure not to blindly copy and then paste things. Feel free to check the documented comments above each kernel parameter before actually using it to learn what each parameter does :)


Tuning the ResponseWriter buffer size

At the present, the Golang HTTP server does not have an option that allows you to configure the write buffer size for unknown reasons.

The net/http package will use a hard coded 4 KB buffer size for the response writer.

To modify this, check out https://github.com/oddmario/go-http-server-custom-write-buffer-patch

  • Recommended tuned value: a value of 8 KB to 1 MB should be optimal for a higher throughput.

Tuning the TCP connection read & write buffer sizes

This is achievable by hooking into the ConnContext of a http.Server instance.

oneLovelyServer := &http.Server{
	...
    
	ConnContext: func(ctx context.Context, c net.Conn) context.Context {
		// https://github.com/golang/go/issues/50795

		(c.(*net.TCPConn)).SetReadBuffer(4096)
		(c.(*net.TCPConn)).SetWriteBuffer(4096)

		return ctx
	},
	
	...
}
  • Recommended tuned value: a value of 4 KB to 4 MB.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment