NOTE

Designing a TCP Connection Pool

A historical note on TCP connection pooling and examples for MySQL, Redis, and HTTP clients.

System DesignCreated Updated 1 min readhistorical

This is a historical learning note and may contain outdated or incomplete understanding.

1. What Is a TCP Connection Pool?

A pool that reuses TCP connections.

2. Why Do We Need a TCP Connection Pool?

Designing Object Pooling

  • Creating a TCP connection requires a three-way handshake, and closing one requires a four-way termination handshake, which is expensive.
  • The number of TCP connections is limited. Under high concurrency, the side that actively closes connections enters TIME_WAIT and can exhaust the available connections: TCP TIME_WAIT.

3. How to Implement a TCP Connection Pool

Designing Object Pooling

3.1. Applications of TCP Connection Pools

3.1.1. MySQL Connection Pool

The ClientTransport of the MySQL client wrapped by tRPC is:

type ClientTransport struct {
	opts   *transport.ClientTransportOptions
	dbs    map[string]*sql.DB
	dblock sync.RWMutex

	MaxIdle     int // maximum idle connections
	MaxOpen     int // maximum active connections
	MaxLifetime time.Duration // maximum connection lifetime (milliseconds)
}

The default instance is:

&ClientTransport{
		opts:        opts,
		dbs:         make(map[string]*sql.DB),
		MaxIdle:     10,
		MaxOpen:     10000,
		MaxLifetime: 3 * time.Minute,
	}

3.1.2. Redis Connection Pool

The ClientTransport of redigo wrapped by tRPC is:

type ClientTransport struct {
	opts            *transport.ClientTransportOptions
	redisPool       map[string]*redigo.Pool
	redisPoolLock   sync.RWMutex
	MaxIdle         int    // maximum idle connections
	MaxActive       int    // maximum active connections
	IdleTimeout     time.Duration // maximum idle waiting time (milliseconds)
	MaxConnLifetime time.Duration
	DefaultTimeout  time.Duration // default connection timeout
	IsWait          bool          // whether to wait for an idle connection when the pool is exhausted
	AllowClientName bool          // whether to set Client Name when creating a connection
	ClientName      string        // Client Name set when creating a connection
}

The default instance is:

&ClientTransport{
		opts:            opts,
		redisPool:       make(map[string]*redigo.Pool),
		MaxIdle:         2048,
		MaxActive:       0,
		IdleTimeout:     3 * time.Minute,
		MaxConnLifetime: 0,
		DefaultTimeout:  time.Second,
		IsWait:          false,
	}

3.1.3. HTTP Connection Pool

HTTP

4. References

Discussion

Sign in with GitHub to comment. Discussions are stored as GitHub Issues.View on GitHub