How to Increase Redis Connection Pool Efficiency in Go Applications

May 27,2025

vlogize

2016-11-23T10:23:24Z

Discover how to effectively manage Redis connection pools in Go for performance-oriented REST APIs. Learn solutions for common connection issues and optimize your application handling under load.
---
This video is based on the question https://stackoverflow.com/q/68530461/ asked by the user 'whatever.idc' ( https://stackoverflow.com/u/8110165/ ) and on the answer https://stackoverflow.com/a/68587882/ provided by the user 'whatever.idc' ( https://stackoverflow.com/u/8110165/ ) at 'Stack Overflow' website. Thanks to these great users and Stackexchange community for their contributions.

Visit these links for original content and any more details, such as alternate solutions, latest updates/developments on topic, comments, revision history etc. For example, the original title of the Question was: Redigo connection pool - how to get more connections?

Also, Content (except music) licensed under CC BY-SA https://meta.stackexchange.com/help/licensing
The original Question post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/by-sa/4.0/ ) license, and the original Answer post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/by-sa/4.0/ ) license.

If anything seems off to you, please feel free to write me at vlogize [AT] gmail [DOT] com.
---
Increasing Redis Connection Pool Efficiency in Go Applications

Managing a high-performance REST API is a challenging task, especially when it hinges on efficient data retrieval from an external store like Redis. One common issue developers encounter is ensuring adequate handling of connections to Redis, particularly under high-load scenarios. This guide will address a specific situation faced by one developer and provide solutions to optimize Redis connection pooling in Go.

The Problem Statement

In the performance-oriented REST API experience, developers sometimes find themselves facing limited Redis connectivity under stress. For example, a typical issue may arise while a Go-swagger-based API effectively responds to requests in milliseconds during low usage. However, when pushing to higher loads—say 4000 requests per second—response times may drastically slow down.

The configuration in question involved:

Creating a Redis connection pool with a high MaxActive value (10,000 connections).

Encountering performance degradation after some time under high stress—evidenced by a stagnant connection count between 2 and 5 for both active and idle connections.

The developer sought answers, wondering if their configuration was adequate, or if there were any additional settings to tweak.

Understanding the Root Cause

Upon investigation, the root cause of the issue was not actually related to Redis itself. Instead, the problem was tied to the handling of TCP connections. A high volume of connections still in a TIME_WAIT state indicated that the TCP connections were not being closed properly. This led to thousands of hanging connections, ultimately choking the server's ability to handle incoming requests.

Symptoms of the Problem

High TIME_WAIT states on sockets, leading to over 60,000 connections being held open.

Sluggish response times under heavy loads that starkly contrasted the expected 3ms response time.

Limited visibility in connection states, with only 2 to 5 active or idle connections being reported.

Solutions for Connection Pool Management

To address and combat the underlying issue, developers can implement several best practices geared toward optimizing Redis usage and handling TCP connections more efficiently. Here are key steps to follow:

1. Properly Close Connections

Ensure that all connections to Redis are closed immediately after being used. The use of defer conn.Close() ensures the connection is returned to the pool; however, it can potentially create delays if improperly managed. Instead, consider closing connections at the end of use without reliance on deferred closure where necessary.

2. Use Live-Traffic Stress Testing

Stress-testing your application using tools like jMeter is helpful, but it can be misrepresentative of real-world outcomes. Engage in live traffic tests that simulate actual user loads. This adjustment revealed that using live traffic alleviated the problem.

3. Monitor Connection Counts

Keep a close eye on ActiveCount and IdleCount from your Redis connection pool. It helps to ensure that connections are being utilized as expected. If not, you may need to reassess your MaxIdle and MaxActive settings, but be aware that underlying issues must also be addressed.

4. Optimize TCP Settings

Due to the intricacies of networking and underlying server configurations, it might be essential to tune TCP settings relevant to your application's requirements. The net.core.somaxconn and vm.overcommit_memory settings can impact how your server interfaces with large volumes of requests, so ensure they are set adequately.

Conclusion

Addressing Redis connection pooling issues within a Go application re

Redigo connection pool - how to get more connections?redisredigogo swagger