Better Programming

Advice for programmers.

Follow publication

Member-only story

Build a TCP Connection Pool From Scratch With Go

Jonathan Seow
Better Programming
Published in
8 min readJan 3, 2022
Photo by Maarten van den Heuvel on Unsplash

My entry task as a junior software engineer was to implement a user registration system with Go.

Sounds simple, right? But, there are performance requirements and design constraints.

Firstly, I am required to place all business logic in a TCP server, and have a client-facing HTTP server interact with it via network connections.

HTP-TCP server architecture

On top of that, I am not allowed to use any Remote Procedure Call (RPC) frameworks like Go’s net/rpc or Google's gRPC. I need to create my own logic to handle connections between the two servers.

Secondly, the entire system is expected to support 1,000 concurrent HTTP requests and 3,000 login requests per second without cache. There need to be 10 million existing users in the database as well.

The key to cracking this entry task is to properly manage the network connections between the HTTP and TCP server. With too few available connections, you’ll end up with slow requests. If too many, your servers will struggle with connectivity problems.

In this article, I will share with you how I built a custom connection pool to achieve the right balance. I will explain the design overview with code snippets. Let’s get started! 🏃‍♂️

What is a Connection Pool?

A connection pool is a set of cached connections in a server that can be used again in the future.

Usually, opening a new network connection is expensive. Think TCP connection as an example, where the client and server need to perform three-way handshakes. It is an expensive operation that consumes data bandwidth and causes round-trip delays.

With pooling, instead of closing a connection after it is used, we can keep it idle while it waits to handle another request. This saves the resources required to create a new connection.

Create an account to read the full story.

The author made this story available to Medium members only.
If you’re new to Medium, create a new account to read this story on us.

Or, continue in mobile web

Already have an account? Sign in

Jonathan Seow
Jonathan Seow

Written by Jonathan Seow

Software Engineer @ TikTok I also share byte-sized coding insights on my blog! https://www.thebytearray.com/

Responses (7)