Backend Development 9 min read

Using tempredis to Launch Temporary Redis Instances for Go Testing

This article introduces the Go library tempredis, which can start and automatically clean up temporary Redis server instances for isolated testing, explains its main features, setup steps, API overview, and provides a complete example demonstrating how to integrate it with go-redis in a Go project.

Go Programming World
Go Programming World
Go Programming World
Using tempredis to Launch Temporary Redis Instances for Go Testing

Recently I wrote several articles on Go concurrency, and to change the pace I introduce a lightweight open‑source library that can start a redis-server instance: github.com/stvp/tempredis . It is a Go package designed to create temporary Redis instances primarily for testing, launching a local Redis server that is automatically shut down and cleaned up after the test.

Main Features

Quickly start a temporary Redis instance without manual installation or configuration, provided the system already has the Redis binary ( redis-server ).

Isolated test environment – each test run gets its own Redis instance, preventing interference between tests.

Automatic cleanup – the temporary instance is closed and related files are removed when the test finishes.

Environment Preparation

First install the Redis binary on the host and the tempredis Go package:

# macOS can install redis-server via Homebrew
# Other platforms see the official docs: https://redis.io/docs/latest/operate/oss_and_stack/install/install-redis/
$ brew install redis

# Install the tempredis Go package
$ go get -u github.com/stvp/tempredis

Brief Overview

The exported structures and methods can be seen in the tempredis documentation. The Config type is essentially a map[string]string used to set Redis configuration; its Socket method returns the path of the redis.sock file.

The Server struct represents a Redis instance with the following methods:

Start : starts a Server based on a given Config .

Kill : forcefully terminates the redis-server .

Term : gracefully shuts down the redis-server .

Socket : proxies to Config.Socket to obtain the socket path.

Stdout : blocks and returns the standard output of the Redis process.

Stderr : blocks and returns the error output of the Redis process.

Usage Example

Below is a small demo that starts a temporary Redis instance, connects to it with the go-redis/redis client, sets a key, reads it, waits for expiration, and finally shuts down the server.

package main

import (
    "fmt"
    "log"
    "time"

    "github.com/go-redis/redis"
    "github.com/stvp/tempredis"
)

func main() {
    // Create and start a temporary Redis instance
    server, err := tempredis.Start(tempredis.Config{"port": "0"}) // auto‑assign port
    if err != nil {
        log.Fatalf("Failed to start tempredis: %v", err)
    }
    defer func() {
        fmt.Println("====================== stdout ======================")
        fmt.Println(server.Stdout())
        fmt.Println("====================== stderr ======================")
        fmt.Println(server.Stderr())
    }()
    defer server.Term()

    fmt.Println("Redis server is running at", server.Socket())

    client := redis.NewClient(&redis.Options{Network: "unix", Addr: server.Socket()})

    client.Set("name", "jianghushinian", time.Second)
    val, err := client.Get("name").Result()
    if err != nil {
        fmt.Println("Get redis key error:", err)
        return
    }
    fmt.Println("name:", val)

    time.Sleep(time.Second)
    val, err = client.Get("name").Result()
    if err != nil {
        fmt.Println("Get redis key error:", err)
        return
    }
    fmt.Println("name:", val)
}

Running the example produces output showing the socket path, the stored value, an expiration error, and the Redis server logs.

Conclusion

The article demonstrates how to use the tempredis package to start a temporary Redis server and integrate it into Go projects. Its API is simple and effective for isolated testing scenarios.

The package appears in the redsync project, a Go implementation of a distributed Redis lock, which readers may explore further.

All example source code is available on GitHub.

backendtestingRedisGotempredisgo-redis
Go Programming World
Written by

Go Programming World

Mobile version of tech blog https://jianghushinian.cn/, covering Golang, Docker, Kubernetes and beyond.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.