Go and Redis
Find tutorials, examples and technical articles that will help you to develop with Redis and Golang.
#
Getting StartedGolang community has built many client libraries that you can find here. For your first steps with Golang and Redis, this article will show how to use the recommended library: redigo.
- Redigo
- Go-Redis
The redigo
library is located in the https://github.com/gomodule/redigo
that you must import in your application.
redigo
module#
Step 1. Import the #
Step 2. Create a connection pool#
Step 3. Write your application codeFind more information about Golang & Redis connections in the "Redis Connect".
Go-redis is a type-safe, Redis client library for Go with support for features like Pub/Sub, sentinel, and pipelining.It is a Redis client able to support a Redis cluster and is designed to store and update slot info automatically with a cluster change. Below are the attractive features of Go-redis:
- Go-redis has pooling capabilities.(Pools allow you to safely handle go-routines, auto reconnect if any error occurs)
- It supports both standard, OSS cluster AIP, and Sentinel
- Comes with Auto reconnects / Auto-rediscovers cluster slots on error/migration
- Support instrumentations
- Allows for a custom dialer (this is useful for Enterprise)
- Support for Redis Sentinel
The go-redis library is located in the https://github.com/go-redis/redis that you must import in your application. Do check Redis Cache Library for Golang
#
Step 1. Run a Redis serverRedis is an open source, in-memory, key-value data store most commonly used as a primary database, cache, message broker, and queue. Redis delivers sub-millisecond response times, enabling fast and powerful real-time applications in industries such as gaming, fintech, ad-tech, social media, healthcare, and IoT. You can run a Redis database directly over your local mac os or in a container. If you have Docker installed in your sytem, type the following command:
You can connect to Redis server using the redis-cli
command like this:
The above command will make a connection to the Redis server. It will then present a prompt that allows you to run Redis commands.
#
Step 2. Initialise the Go ModuleIn order to connect to the Redis instance and return some data value, first you need to initialize the Go module as shown:
#
Step 3. Install redis/v8#
Step 4. Create a main.go fileLet us create a main.go
file and write the following code to check for your Redis instance connection
#
Step 5. Begin the compilationBy now, the Go application should successfully connect to the Redis instance and return data value (a successful "PONG" response).
#
Redis LaunchpadRedis Launchpad is like an “App Store” for Redis sample apps. You can easily find apps for your preferred frameworks and languages. Check out a few of these apps below, or click here to access the complete list.
#
Technical Articles & WhitepapersRedis and Golang: Designed to Improve Performance Redisqueue - A producer and consumer of a queue that uses Redis streamsA High Performance Recommendation Engine with Redis and Go**