Redis is an open-source (BSD licensed), in-memory data structure store, used as a database, cache, and message broker. It supports data structures such as strings, hashes, lists, sets, sorted sets with range queries, bitmaps, hyperloglogs, geospatial indexes with radius queries and streams.
https://redis.io/
Redis is an open-source database, which is essentially a combination of maps. It is easily implemented ourselves in Go, but if you don’t want to reinvent the wheel every time you code, this article is for you. However, I will start by explaining how Redis works, and how to use it.
What is Redis?
Redis stands for the Remote Dictionary Server. It is an in-memory data structure project implementing a distributed, in-memory key-value database with optional durability. Redis supports different kinds of abstract data structures, such as strings, lists, maps, sets, sorted sets, and other lesser-used ones like HyperLogLog (which, if you’re interested, is an algorithm that counts the number of unique elements in a multiset).
It is interesting to me, because I had only worked on RDBMS like SQL up until this point, and while the redis is also a data storage, it doesn’t use the disk storage but instead the RAM. I list additional differences in the table below:
Redis vs RDBMS – Differences
Redis | RDBMS |
---|---|
Advantages: | |
Redis stores everything in primary memory(RAM, Cache) | RDBMS stores everything in secondary memory (HDDs, SSDs) |
superfast read-write speeds | much slower, especially with increase in size |
generally used to store small binary files like logs in the magnitude of a few thousand kilobytes. | can store large amounts of data which has less frequently usage. |
Disadvantage: | |
smaller in perspective of data storage capability and quite expensive | abundant, cheap and readily available |
no query language and no support for a relational algebra. You cannot submit ad-hoc queries to redis. Few preset commands are available. | Fully functional query languages, and ad hoc queries supported for non-programmers. |
only offers basic security (in term of access rights) at instance level. | RDBMS can provide fine grained per-object access control lists for role management. |
Now that we understand that, let’s move on to installation.
Installation of Redis Cache Go Client
Go to redis.io, click on “Clients” in the top menubar of the page, and there you will see many programming language clients. We shall click on Go, and then we’ll be redirected to a list.
We can use any of these, but we’ll focus on only one – redigo, and the rest of the clients also work the same way.
Redigo Package
We can install Redigo using the “go get” command:
go get github.com/gomodule/redigo/redis
The Go distribution is Redigo’s only dependency. So no extra software is required.
Let’s open up a new file main.go, and import in our libraries:
package main
import (
"fmt"
"log"
"github.com/gomodule/redigo/redis"
)
Opening Redis Server Connection in Go
We can then dial a connection to any port using the redis.Dial() command.
c, err := redis.Dial("tcp", ":6379")
if err != nil {
log.Fatal(err) // handle error
}
defer c.Close()
Adding data to Redis Cache – HMSET Command
We can interact with the connection, like if we wanted to add a key:value pair as our data cache:
_, err = conn.Do(
"HMSET",
"book:1",
"title",
"Sherlock Holmes",
"creator",
"AC Doyle",
"category",
"detective",
"price of book",
59.99,
)
The HMSET command is one of many Redis commands, and you can check out the full list of commands here. The HMSET command sets the specified fields to their respective values in the hash stored at key
. This command overwrites any specified fields already existing in the hash. If key
does not exist, a new key holding a hash is created.
Fetching the Data from Redis Cache – HGET Command
Our data is stored in the Redis cache, so we can fetch the data using redis.String() command.
title, err := redis.String(conn.Do("HGET", "book:1", "title"))
if err != nil {
log.Fatal(err) // handle error
}
fmt.Println("Book Title:", title)
HGET command returns the value associated with field
in the hash stored at key
.