Golang Redis Tutorial for Beginners

Author

Reads 1.3K

From above crop faceless male developer in black hoodie writing software code on netbook while working in light studio
Credit: pexels.com, From above crop faceless male developer in black hoodie writing software code on netbook while working in light studio

Golang and Redis are a match made in heaven, and I'm excited to share this tutorial with you.

In this tutorial, we'll explore how to use Redis with Golang, a popular programming language known for its simplicity and performance.

First things first, let's talk about what Redis is. Redis is an in-memory data store that can be used as a database, message broker, and even a cache. It's fast, flexible, and easy to use.

Here's a simple example of how to connect to a Redis server using Golang's built-in redis package. We'll create a new Redis client and use it to set and get a value.

With Redis and Golang, you can build high-performance applications that scale easily. This is especially useful for real-time applications, such as live updates and chat systems.

Consider reading: Next Js Redis

Installation and Setup

To get started with Go-Redis, you'll need to ensure you're running a Go version with module support, as it's a requirement for the library. Make sure to initialize a Go module.

Broaden your view: Go vs Golang

Credit: youtube.com, Beginner's Guide to Redis with Go!

You'll also need to install the Go-Redis client using the command `go get github.com/redis/go-redis/v9`.

To connect to your Redis service, you'll need to get your Redis service up and running, which can be done by running a Redis container on your local machine.

You can run a Redis container using Docker with the commands `docker pull redis` and `docker run -d -p 6379:6379 --name my-redis redis`. This will map your local port 6379 to the port that Redis is running against within the container.

Make sure you have Redis installed on your machine or have access to a Redis server.

If this caught your attention, see: Windows Azure Redis

Basic Operations

In Redis, you can set a value with the Set command, which also takes a time.Duration indicating how long the key should live in the cache before expiring.

The Get command is used to retrieve values, and it returns a *StringCmd type, which has a val field containing the underlying value.

If the key doesn't exist, Get returns redis.Nil, a sentinel error you can check for and handle.

Consider reading: Golang Test Command

Clients

A developer working on a laptop, typing code, showcasing programming and technology skills.
Credit: pexels.com, A developer working on a laptop, typing code, showcasing programming and technology skills.

There are several popular Go clients for Redis, including go-redis, redigo, and rueidis. Go-redis is the most popular client, with decent documentation and an active community.

Redigo is a stable and tenured client that supports all standard Redis data types and features, but its API is a bit too flexible. It's also used to implement other Go client libraries.

Rueidis is a relatively new client library that supports RESP3 protocol, client-side caching, and various Redis Modules. It provides a Do function and uses a builder pattern to create commands, retaining strong type checking.

For those looking for deeper performance numbers, a benchmark comparison with the go-redis library might be interesting.

Intriguing read: Golang Go

Using Hashes

Using Hashes in Redis is a powerful way to store multiple field-value pairs under a single key. This can be especially useful when working with complex data.

In older versions of Redis, HSet only supports a single key-value pair, which can be limiting. HMSet is used for multiple field and value arguments in these versions.

Detailed view of a black data storage unit highlighting modern technology and data management.
Credit: pexels.com, Detailed view of a black data storage unit highlighting modern technology and data management.

Starting from Redis 4.0, HMSet has been marked as deprecated, which means it may not be supported in the future. This is why it's recommended to use HSet instead.

HSet is the preferred method for storing multiple field-value pairs in Redis 4.0 and later. This is because it's the recommended replacement for HMSet.

Demos

In the demo, I covered the basics of connecting to Redis, which is a crucial step in getting started with the database.

Connecting to Redis is straightforward, and I showed how to do it.

Using Redis requires an understanding of its common data types, such as strings with TTL (time to live), which is a feature that allows you to set a time limit for how long data is stored.

Strings with TTL are useful for storing temporary data, like session IDs.

Go-redis also supports hash and struct data types, which are useful for storing complex data.

Adult male programmer working on code at a modern desk setup with a large monitor.
Credit: pexels.com, Adult male programmer working on code at a modern desk setup with a large monitor.

Hashes are like dictionaries, where you can store key-value pairs.

Structs are similar to hashes, but they allow you to define a custom structure for your data.

In the demo, I also showed how to use sets in Redis, which are useful for storing unique values.

Sets are unordered collections of values, and they're great for storing data that you don't need to access in a specific order.

Another technique I demonstrated is pipelining, which is a way to send multiple commands to Redis in a single request.

Pipelining can improve performance by reducing the number of requests needed to complete a task.

I also touched on Hyperloglog, which is a data structure that's similar to a set, but it's designed for estimating the cardinality of a set.

Hyperloglog is useful when you need to estimate the number of unique values in a set, rather than storing the actual values.

Here's a quick summary of the data types I covered:

  • Strings with TTL
  • Hashes
  • Structs
  • Sets
  • Hyperloglog

Advanced Operations

Credit: youtube.com, What’s Inside Go-Redis? A Peek Under the Hood of The Go Redis Client

In GoLang, you can use the Redis client to execute more complex operations, such as transactions and pipelining.

Transactions allow you to execute multiple commands as a single, all-or-nothing unit of work. This is particularly useful when performing operations that need to be atomic, such as transferring funds between accounts.

To use transactions, you can use the Redis client's `Tx` object, which allows you to execute multiple commands and then commit or rollback the transaction as needed. For example, you can use the `Tx` object to transfer funds between accounts by executing multiple Redis commands in a single transaction.

Pipelining, on the other hand, allows you to execute multiple commands in a single request to the Redis server, improving performance by reducing the number of network round-trips required. This can be particularly useful when performing operations that require executing multiple commands in a specific order.

The GoLang Redis client provides a `Pipeline` object that allows you to execute multiple commands in a single request. For instance, you can use the `Pipeline` object to execute multiple Redis commands in a single request, improving the performance of your application.

If this caught your attention, see: Golang Use Cases

Features and Ecosystem

Credit: youtube.com, I've been using Redis wrong this whole time...

The Go Redis client is a powerful tool that offers a wide range of features to make your life easier. The client supports Redis commands except QUIT and SYNC, which means you can use it to perform a variety of tasks, from simple data storage to complex data processing.

One of the standout features of the Go Redis client is its automatic connection pooling, which helps improve performance and efficiency. This feature allows you to reuse existing connections to the Redis server, reducing the overhead of creating new connections.

Here are some of the key features of the Go Redis client:

  • Automatic connection pooling.
  • StreamingCredentialsProvider (e.g. entra id, oauth) (experimental)
  • Pub/Sub.
  • Pipelines and transactions.
  • Scripting.
  • Redis Sentinel.
  • Redis Cluster.
  • Redis Ring.
  • Redis Performance Monitoring.
  • Redis Probabilistic [RedisStack].
  • Customizable read and write buffers size.

In terms of ecosystem, the Go Redis client is part of a larger community that includes other popular Redis clients, such as Jedis and Redisson. According to the data, the Go Redis client is used by a significant number of developers, and is even used in some of the most popular Redis workloads.

Features

Computer Program Language Text
Credit: pexels.com, Computer Program Language Text

Our software supports a wide range of Redis commands, excluding QUIT and SYNC.

You can take advantage of automatic connection pooling, which helps to improve performance and efficiency.

The StreamingCredentialsProvider feature allows for secure authentication using entra id or oauth credentials, and is currently experimental.

Pub/Sub is also supported, enabling real-time communication between clients.

Pipelines and transactions are available, making it easier to manage complex operations.

Scripting is another feature that's been implemented, giving you more flexibility and control.

We've also included support for Redis Sentinel, which helps to ensure high availability and failover.

Redis Cluster is supported, allowing for horizontal scaling and improved performance.

Additionally, Redis Ring is available, providing a way to connect to multiple Redis instances.

Customizable read and write buffers size is also a feature, giving you more control over performance and memory usage.

Here's a summary of the supported features:

Ecosystem

The go-redis project has a strong ecosystem that supports its use. This client also works with Kvrocks, a distributed key-value NoSQL database that uses RocksDB as storage engine and is compatible with Redis protocol.

Close-up view of a computer screen displaying code in a software development environment.
Credit: pexels.com, Close-up view of a computer screen displaying code in a software development environment.

The go-redis project was originally initiated by uptrace/uptrace, an open-source APM tool that supports distributed tracing, metrics, and logs. Uptrace can be used to monitor applications and set up automatic alerts to receive notifications via email, Slack, Telegram, and others.

Here are some key components of the go-redis ecosystem:

  • Redis Mock
  • Distributed Locks
  • Redis Cache
  • Rate limiting

It's worth noting that the go-redis client is not the only Go client for Redis, but it is the most popular one.

Configuration and Settings

go-redis uses 32KiB read and write buffers by default for optimal performance.

For high-throughput applications or large pipelines, you can customize buffer sizes to suit your project's needs.

go-redis supports extending the client identification phase, allowing you to send your own custom client identification.

To start using Redis in your Golang project, import the Redis client package and establish a connection with your Redis server.

Another option is using a connection string to set up the Redis connection, which can be a more straightforward approach.

Authentication and Connection

Credit: youtube.com, Golang Code - Redis Database

Authentication is supported by the Redis client, with a clear priority order for providing credentials.

The available options include using a connection string, which is another popular way to authenticate. You can also use a password, but if you don't have one set, you can leave it blank.

To establish a connection with your Redis server, import the Redis client package and use the address of the instance you want to connect to, along with the password and database you wish to use. The default database is denoted by the value 0.

Setting Up Connection:

To set up a connection to your Redis server, you can use the redis.NewClient function and pass in the connection parameters, such as the address of the Redis instance and the password.

One way to do this is by importing the github.com/go-redis/redis package, which is the most popular Go package for interacting with Redis. This package is widely used and has been the most popular option at the time of writing this.

Recommended read: Install Golang Package

Man Working on Computers Coding
Credit: pexels.com, Man Working on Computers Coding

You can also connect to your Redis instance by using a Redis URI specification, which is a string that adheres to the redis uri specification. This can be easily configured using a string.

In your Go application, you can define a client that takes in various options such as the address of the Redis instance, the password, and the database you wish to use. For example, you can use the default database, denoted by the value 0, if you don't have a password set.

To validate that your setup works, you can start by creating a new directory for your project and creating a new file called main.go. Then, you can use the go run main.go command to run your application locally.

You might like: Run Golang File

Authentication

The Redis client supports multiple ways to provide authentication credentials, and it has a clear priority order for these options.

One of the available options is to use a password, which is a simple and straightforward way to authenticate with Redis.

Credit: youtube.com, Session vs Token Authentication in 100 Seconds

In addition to passwords, the Redis client also supports other authentication methods, such as tokens or certificates, which can be used to authenticate with Redis.

The priority order of these options is determined by the Redis client, and it will try each option in turn until it finds one that works.

Authentication credentials can be provided in a variety of ways, including through environment variables, configuration files, or directly in the code.

Data and Value Management

In Redis, you can set timeouts or propagate information using a context, which is accepted by every Redis command.

The most basic Redis command is Get, which returns a *StringCmd type and can be used to retrieve values from the database. If the key doesn't exist, Redis will return a redis.Nil error.

You can set values in Redis using the Set method, which accepts a key, a value, and an optional time.Duration indicating how long the key should live in the cache before expiring.

Credit: youtube.com, Redis Deep Dive w/ a Ex-Meta Senior Manager

Redis can store complex data structures by marshaling them into JSON and storing the JSON strings in the database using the Set method. This allows you to store composite values like structs in Redis.

The returned value of Redis commands, such as Get, usually has an underlying val field that has the same type indicated by the command. These returned types have helper methods that perform type conversions for you.

Error Handling and Troubleshooting

When working with Go and Redis, it's essential to handle errors properly to avoid unexpected behavior in your applications.

Go-redis exports the redis.Nil error, which is returned whenever the Redis Server responds with (nil), allowing you to distinguish between an empty string reply and a nil reply.

You can use redis-cli to check what response Redis returns, and commands like GET, BLPOP, and ZSCORE can also return redis.Nil.

In practice, this means you'll want to check for redis.Nil when using these commands to ensure your application behaves as expected.

Intriguing read: Golang Check Type

Handling Nil Errors

Credit: youtube.com, err != nil Is GOOD? (And Why)

Redis Server responds with (nil) in certain situations, including when the key does not exist.

You can use redis-cli to check what response Redis returns.

The go-redis library exports the redis.Nil error, which you can use to distinguish an empty string reply from a nil reply.

Commands like GET, BLPOP, and ZSCORE can return redis.Nil, so it's essential to handle this error correctly.

In the case of GET, a nil reply indicates that the key does not exist, whereas an empty string reply means the key exists but has no value.

You might enjoy: Golang Create Error

Unstable Resp3 for Commands

Enabling Unstable RESP3 for Commands can be a bit tricky, so it's essential to know the basics first. If you're planning to use RESP3 with your Redis integration, be aware that some response structures aren't final yet, especially for complex structures like search and query results.

You should consider using RESP2 for these capabilities for now, as the RESP3-based API-s are still unstable. However, the developers plan to stabilize them in future versions.

Credit: youtube.com, How Does Error Handling Help Fix Bugs? - Learn To Troubleshoot

To enable Unstable Resp3, you'll need to set an option in your client configuration. This will allow you to use unstable search commands, but keep in mind that Val() and Result() calls won't have any effect on them.

When UnstableResp3 mode is enabled, you'll need to use RawResult() and RawVal() to retrieve a raw data, as the raw response is the only option for unstable search commands.

Take a look at this: Golang Search

Tips and Best Practices

Connecting to Redis can be a bit tricky, so be sure to avoid common mistakes like incorrect password or host settings.

Scalability is key when working with large amounts of data, and Redis offers two main options: Vertical and Horizontal scaling. Vertical scaling involves increasing the power of your existing server, while Horizontal scaling involves adding more servers to your cluster.

Using read-replicas is a great way to improve the performance of your Redis database by offloading reads to a separate server.

Credit: youtube.com, Golang Microservices: Caching with Redis

The way you distribute your keys across a Redis cluster can have a big impact on performance, so it's worth taking the time to understand how Redis decides where to store your data.

If you need to execute bulk operations across a Redis Cluster, you can use the Redis Cluster protocol to send multiple commands at once.

Sharded Pub/Sub is a feature that allows you to distribute messages across multiple Redis nodes, which can be really useful for large-scale applications.

Introduction and Overview

Redis is an open-source, in-memory data structure store that can be used as a cache, message broker, or a persistent key-value database.

It's a powerful tool that can be leveraged for efficient data storage and retrieval.

In this article, we'll explore how to integrate Redis with a Golang application.

Introduction

Redis is an open-source, in-memory data structure store that can be used as a cache, message broker, or a persistent key-value database. This versatility makes it a popular choice for developers.

Woman using laptop on sofa, surrounded by programming books, learning coding.
Credit: pexels.com, Woman using laptop on sofa, surrounded by programming books, learning coding.

Redis is primarily used as a key-value store, where the key can be a string or even a binary. The value can be a variety of data structures such as strings, hashes, lists, sets, etc.

One of the key benefits of Redis is its ability to be highly available. This can be achieved by setting up primary-replica replication, or taking it a step further with Redis Cluster.

Redis can also be configured to persist to disk, ensuring that data is not lost in the event of a failure. However, there are solutions like Amazon MemoryDB that can take it a notch further with distributed transactional logs.

Here are some of the key points to consider when using Redis:

  • Data structure server
  • Not just a cache, but also a messaging platform
  • High availability options
  • Persistence options

I Love and!

I love Redis and Go! Since its release in 2009, Redis has been topping the charts since 2013, according to db-engines trends statistics.

Redis has won the hearts and minds of the developer community, being voted most loved database for 5 years in a row on Stack Overflow's annual survey.

Code on Screen
Credit: pexels.com, Code on Screen

Go has become the language of the Cloud, powering many cloud-native projects, including Docker, Kubernetes, and Prometheus.

75% of CNCF projects are written in Go, and it's also used in many databases, such as Influxdb, etcd, Vitess, and TiDB.

Go caters to a wide variety of general purpose use cases, including web apps and APIs, data processing pipelines, infrastructure as code, SRE/DevOps solutions, and command line apps.

Go has maintained its top 5 spot in language popularity since 2018, and it's the only statically-typed language after Rust in the 2022 data.

Thomas Goodwin

Lead Writer

Thomas Goodwin is a seasoned writer with a passion for exploring the intersection of technology and business. With a keen eye for detail and a knack for simplifying complex concepts, he has established himself as a trusted voice in the tech industry. Thomas's writing portfolio spans a range of topics, including Azure Virtual Desktop and Cloud Computing Costs.

Love What You Read? Stay Updated!

Join our community for insights, tips, and more.