Dragonfly

Redis with Golang: Top 3 Packages and a Quick Tutorial

Redis is an open-source, in-memory data store with rich data types for caching, messaging, analytics, and sessions. Go is a fast, scalable language suitable for backend services and infra.

January 19, 2026

Guides Cover | Redis & Golang

Why Use Redis with Go?

Redis is an open-source, in-memory key-value data store. It supports various data structures, such as strings, hashes, lists, sets, and sorted sets, enabling a wide range of use cases beyond simple caching, including message brokering, real-time analytics, and session storage. Go (or Golang) is a performant, statically typed language well-suited for building scalable backend services. 

Developers can use Go with Redis to build fast, concurrent, and distributed applications. Here are the main reasons to use Redis with Go:

  • High performance compatibility: Both Go and Redis are built with performance in mind. Go’s lightweight goroutines align well with Redis’s in-memory speed, allowing thousands of concurrent operations with minimal latency.
  • Client libraries: Libraries like go-redis provide a mature and well-maintained interface for interacting with Redis. These clients support full Redis command sets, pipelining, connection pooling, and more.
  • Integration: Go’s standard library and ecosystem make it easy to integrate Redis into projects for caching, message queuing, or session management with minimal setup.
  • Scalability and concurrency: Go handles concurrency with ease using goroutines and channels. This pairs effectively with Redis’s ability to support high-throughput workloads in distributed architectures.
  • Common backend use cases: Redis is often used in Go applications for caching API responses, implementing rate limiters, queueing tasks, or storing ephemeral session data.
  • Production-ready tools: Go and Redis both have strong support in cloud environments, with Docker images, monitoring tools, and orchestration options that make them suitable for deployment at scale.

This is part of the series of Redis tutorial articles.


1. go‑redis

go-redis is one of the most widely used and actively maintained Redis clients for Go. It supports the full Redis command set, including pub/sub, transactions, scripting, and pipelining. The client also includes built-in support for Redis Sentinel and Cluster, making it suitable for high-availability and distributed deployments.

The library offers advanced features such as connection pooling, context-based request cancellation, and structured logging. Its API is idiomatic and consistent with Go’s conventions, making it easy to integrate into production applications.

Official Repo: https://github.com/redis/go-redis

2. redigo

redigo is a stable and minimalist Redis client that emphasizes simplicity and performance. It provides low-level access to Redis commands and is known for its small footprint and efficiency.

While redigo lacks some higher-level abstractions found in newer libraries, it remains a solid choice for developers who prefer explicit control over Redis interactions. It supports pipelining and connection pooling but does not natively handle Sentinel or Cluster setups.

Official Repo: https://github.com/gomodule/redigo

3. rueidis

rueidis is a fast Redis client for Go. It emphasizes developer experience, providing high-level features like client-side caching, auto pipelining, and generic object mapping. In the meantime, it supports extended feature sets like RedisJSON, RedisBloom, RediSearch, etc.

Official Repo: https://github.com/redis/rueidis


Tutorial: Getting Started with go-redis

This section walks through how to set up and use the go-redis client in a Go application. We’ll cover installation, basic usage, authentication options, and configuration settings with code examples. Instructions are adapted from the go-redis documentation.

Step 1: Install go-redis

First, initialize a Go module if you haven’t already:

go mod init github.com/my/repo

Then, add the go-redis package:

go get github.com/redis/go-redis/v9
Redis Golang Tutorial | Installing the 'go-redis' Client Library

This installs version 9 of the official Redis client for Go, compatible with recent Redis versions like 7.2, 7.4, and 8.x.

Step 2: Basic Redis Client Setup

Here’s a minimal example of connecting to Redis, setting a key, and retrieving it:

package main

import (
    "context"
    "fmt"

    "github.com/redis/go-redis/v9"
)

func main() {
    ctx := context.Background()

    rdb := redis.NewClient(&redis.Options{
        Addr:     "localhost:6379",
        Password: "", // no password
        DB:       0,  // default DB
    })

    err := rdb.Set(ctx, "key", "value", 0).Err()
    if err != nil {
        panic(err)
    }

    val, err := rdb.Get(ctx, "key").Result()
    if err != nil {
        panic(err)
    }

    fmt.Println("key:", val)
}

Save above code in a file called basic.go. You can run it using the following command:

go run basic.go

Explanation:

  • redis.NewClient sets up a Redis connection.
  • Set stores a value with optional expiration (0 means no expiration).
  • Get retrieves the stored value.

Step 3: Authentication

The simplest way to provide credentials is by passing a username and password:

rdb := redis.NewClient(&redis.Options{
	Addr:     "localhost:6379",
	Username: "user",
	Password: "pass",
})

Note: For production environments, go-redis also supports dynamic and context-based credential providers for environments that rotate credentials or supply them at runtime.

Step 4: Connecting via Redis URL

The client can also be configured using a Redis URL string:

package main

import "github.com/redis/go-redis/v9"

func connect() *redis.Client {
	url := "redis://user:password@localhost:6379/0?protocol=3"
	opts, err := redis.ParseURL(url)
	if err != nil {
		panic(err)
	}return redis.NewClient(opts)
}

The URL can include username, password, database number, and protocol version.

Step 5: Advanced Options

Here are a few advanced options available with go-redis.

Handling Missing Keys

If a key does not exist, Get returns a redis.Nil error:

val, err := rdb.Get(ctx, "missing").Result()
if err == redis.Nil {
	fmt.Println("Key does not exist")
} else if err != nil {
	panic(err)
}

This allows applications to distinguish between "key not found" error and other errors.

Using the RESP3 Protocol

To enable the newer RESP3 protocol, which is necessary for some advanced features and improved data types:

rdb := redis.NewClient(&redis.Options{
	Addr:     "localhost:6379",
	Protocol: 3,
})

Buffer Size Configuration

You can tune buffer sizes to optimize performance for high-throughput workloads:

rdb := redis.NewClient(&redis.Options{
	Addr:            "localhost:6379",
	ReadBufferSize:  1024 * 1024,
	WriteBufferSize: 1024 * 1024,
})

Disabling Client Identity

To disable automatic client identification during the connection:

rdb := redis.NewClient(&redis.Options{
	Addr:            "localhost:6379",
	DisableIdentity: true,
})

This avoids sending the HELLO & CLIENT SETINFO metadata during handshake.

Instrumentation with OpenTelemetry

You can monitor Redis operations with tracing and metrics. This allows integration with observability platforms.

package main

import (
	"log"
	"errors"

	"github.com/redis/go-redis/v9"
	"github.com/redis/go-redis/extra/redisotel/v9"
)

func main() {
	rdb := redis.NewClient(&redis.Options{Addr: "localhost:6379"})
	
	if err := errors.Join(
		redisotel.InstrumentTracing(rdb),
		redisotel.InstrumentMetrics(rdb),
	); err != nil {
		log.Fatal(err)
	}
}

Note: You will need to install redisotel/v9, please use the following command:

go get github.com/redis/go-redis/extra/redisotel/v9

Best Practices for Using Redis in Go 

1. Use Context and Cancellation Properly

Always use contexts when performing Redis operations to enable timeouts, deadline enforcement, and request cancellation. Each Redis command should accept a context.Context parameter, letting you manage the operation’s lifecycle in line with your application’s requirements. This prevents stuck goroutines, orphaned requests, and uncontrolled resource consumption caused by network partitions or slow responses.

Contexts allow integration with higher-level lifecycle management tools, helping services scale predictably and respond to external signals (like shutting down gracefully). By propagating contexts through your call chains, you ensure consistent resource management and proper cleanup, which is especially important for services under heavy load or those running in containerized, orchestrated infrastructure.

2. Reuse Clients Instead of Reconnecting

Creating a new Redis client instance for every operation leads to excessive resource usage and degraded performance. Establishing a Redis connection is expensive, and frequent reconnections exhaust file descriptors, overload the server, and introduce unnecessary delays. Instead, initialize a single client at application startup and reuse it throughout the application’s lifetime.

Most Redis clients for Go are thread-safe and designed for concurrent use, so sharing a single instance across multiple goroutines is safe and efficient. Pooling mechanisms within these clients manage connection reuse, reducing overhead and avoiding connection floods. This approach minimizes latency, conserves system resources, and promotes predictable application behavior.

3. Handle Errors and Retries Gracefully

Robust error handling is essential when working with Redis in production. Network issues, server timeouts, and command failures can occur, and your application must detect and respond to these events correctly. Check error returns on all Redis operations, distinguish between transient and permanent errors, and implement retry logic, preferably with exponential backoff, to avoid overwhelming the Redis server during outages.

It’s important to log errors effectively to aid troubleshooting and to propagate meaningful error information up the call stack. Consider circuit breaker patterns or custom middleware to manage failure scenarios, especially for critical data paths. Proper error and retry strategies help keep your application resilient, maintain data consistency, and ensure user-visible impact is minimized during service disruptions.

4. Prefer Pipelines for Batch Operations

When your application needs to execute multiple Redis commands at once, such as inserting or retrieving multiple keys, use pipelining to batch those operations into a single round-trip to the server. This reduces network overhead and drastically improves throughput, especially under high load or in distributed environments where latency is significant. Pipelines are well-supported in most Go Redis clients with simple APIs.

Care must be taken to handle responses correctly, as pipelined commands return their results in order, and errors must be matched to their originating commands. Pipelining is not a substitute for transactions: If atomicity is important, use Redis transactions along with pipelining where appropriate. Following these batch processing patterns optimizes resource use without sacrificing correctness.

5. Use Proper Serialization/Deserialization Techniques

When storing complex Go data structures in Redis, use established serialization formats like JSON, MessagePack, or protocol buffers. Convert your objects to a byte slice or string before storing them and deserialize them upon retrieval. Improper serialization can lead to data corruption, loss of type information, and interoperability issues, especially in multi-language or evolving systems.

Choose a serialization method that balances encoding/decoding speed with output size and maintains schema compatibility as your data models evolve. Popular Go packages like encoding/json or github.com/vmihailenco/msgpack integrate directly into Redis client workflows. Strong serialization practices ensure reliable data exchange, type safety, and system maintainability.

6. Secure Credentials and TLS Configuration

Redis is often deployed without authentication or encryption by default, which is risky for any production environment, especially on public or shared networks. Always secure Redis endpoints using authentication (via the requirepass directive or ACLs in new Redis versions) and configure your Go client to use passwords or tokens securely, avoiding hard-coded credentials and using environment variables or secret stores instead.

Enable and enforce TLS on both the Redis server and in your Go client’s connection settings to protect data in transit from eavesdropping or tampering. Regularly rotate credentials and monitor for configuration drift. By securing both credentials and transport, you prevent unauthorized access and data breaches, ensuring compliance and protecting critical systems from attack.


Dragonfly: Next-Gen In-Memory Data Store with Limitless Scalability

Dragonfly is a modern, source-available, multi-threaded, Redis-compatible in-memory data store that stands out by delivering unmatched performance and efficiency. Designed from the ground up to disrupt legacy technologies, Dragonfly redefines what an in-memory data store can achieve.

Dragonfly Scales Both Vertically and Horizontally

Dragonfly’s architecture allows a single instance to fully utilize a modern multi-core server, handling up to millions of requests per second (RPS) and 1TB of in-memory data. This high vertical scalability often eliminates the need for clustering—unlike Redis, which typically requires a cluster even on a powerful single server (premature horizontal scaling). As a result, Dragonfly significantly reduces operational overhead while delivering superior performance.

For workloads that exceed even these limits, Dragonfly offers a horizontal scaling solution: Dragonfly Swarm. Swarm seamlessly extends Dragonfly’s capabilities to handle 100 million+ RPS and 100 TB+ of memory capacity, providing a path for massive growth.

Key Advancements of Dragonfly

  • Multi-Threaded Architecture: Efficiently leverages modern multi-core processors to maximize throughput and minimize latency.
  • Unmatched Performance: Achieves 25x better performance than Redis, ensuring your applications run with extremely high throughput and consistent latency.
  • Cost Efficiency: Reduces hardware and operational costs without sacrificing performance, making it an ideal choice for budget-conscious enterprises.
  • Redis API Compatibility: Offers seamless integration with existing applications and frameworks running on Redis while overcoming its limitations.
  • Innovative Design: Built to scale vertically and horizontally, providing a robust solution for rapidly growing data needs.

Dragonfly Cloud

Dragonfly Cloud is a fully managed service from the creators of Dragonfly, handling all operations and delivering effortless scaling so you can focus on what matters without worrying about in-memory data infrastructure anymore.

Was this content helpful?

Help us improve by giving us your feedback.

Switch & save up to 80%

Dragonfly is fully compatible with the Redis ecosystem and requires no code changes to implement. Instantly experience up to a 25X boost in performance and 80% reduction in cost