Pool maintains a pool of connections. The application calls the Get method to get a connection from the pool and the connection's Close method to return the connection's resources to the pool.

The following example shows how to use a pool in a web application. The application creates a pool at application startup and makes it available to request handlers using a global variable. The pool configuration used here is an example, not a recommendation.

func newPool(server, password string) *redis.Pool {
    return &redis.Pool{
        MaxIdle: 3,
        IdleTimeout: 240 * time.Second,
        Dial: func () (redis.Conn, error) {
            c, err := redis.Dial("tcp", server)
            if err != nil {
                return nil, err
            if _, err := c.Do("AUTH", password); err != nil {
                return nil, err
            return c, err
        TestOnBorrow: func(c redis.Conn, t time.Time) error {
            if time.Since(t) < time.Minute {
                return nil
            _, err := c.Do("PING")
            return err

var (
    pool *redis.Pool
    redisServer = flag.String("redisServer", ":6379", "")
    redisPassword = flag.String("redisPassword", "", "")

func main() {
    pool = newPool(*redisServer, *redisPassword)

A request handler gets a connection from the pool and closes the connection when the handler is done:

func serveHome(w http.ResponseWriter, r *http.Request) {
    conn := pool.Get()
    defer conn.Close()

Pool is referenced in 499 repositories