Intro to Redis — with Docker Compose

What is Redis?
Redis is a high throughput, in-memory key-value store used for fast lookup. It supports common data structures such as strings, lists, sets, hashes, and more (redislabs). Since Redis stores data in-memory, it does not face the same read/write (I/O) limitations and performance issues that databases face with hard disks. Although Redis stores and accesses data in-memory, it can be persisted on the hard disk for resilience; Docker Volumes makes this process easy.
When would we use Redis?
Redis is commonly used in web stacks as the “cache” service accessed by the API. For example, the client (browser) will make a request to the API (server) for some common piece of data, like “Top 10 Product Categories”. Rather than the API going directly to the database, executing a potentially long running query statement (depending on how well your database schema is structured) and awaiting the result, we can populate both the Redis key-value store and the database with identical information.
In this scenario, the database is used as the “true data source” and Redis is used as the lightweight, fast lookup for commonly accessed data — like Top Categories. If the client requested all information about a particular, unpopular Product ID, for instance, we probably should not store all of this data in-memory with Redis and eat up all of our server’s RAM; instead, we would just execute a slower database query to service the request.
How do we access data in Redis?
Fortunately, Redis is a very popular database/store so there is a lot of community support. There are easy-to-use client libraries for most of the popular languages, like Node Redis and scala-redis. These libraries establish a blocking connection (single threaded, synchronous) between the client (your application) and the Redis server via port 6379.
Depending on the data structure being accessed, different Redis commands are used — like HGET , HGETALL , and HSET . The most common and simple use case is setting/getting a value from a given key.
Below, we’re setting a field called field1 in the key myhash to "Hello" and then getting the value of field1 :
HSET myhash field1 "Hello"
HGET myhash field1
> "Hello"
The sections below assume you have Docker (and Docker Compose) installed on either Linux or Docker Desktop for Mac/Windows.
Running Redis with Docker (not Compose)
To get started, we can start up a Redis server immediately with a regular Docker container:
docker run -d -p 6379:6379 --name redis redis:alpine
# -- Explained
-d: run in the background as a daemon process-p 6379:6379: expose the default Redis ports to the host machine for external access to Redis--name: Name of the Docker containerredis:alpine: The Docker image we’re pulling from Dockerhub. We’ll use the lightweight "alpine" version.
The Redis server will be created instantly. Now, we can enter our Docker container by running:
docker exec -it <CONTAINER_ID> /bin/sh

You’ll be dropped in the /data directory, which is where our Redis data is stored inside the container.
To interact with Redis via the command line, we run the following inside of the container:
redis-cli
This will activate the Redis CLI (command line interface). Your command line will look like:

Now you are free to enter any valid Redis command. For example, let’s set/get a simple one-level key/value:
HSET example hello world
HGET example hello

Redis with Docker Compose
Let’s see what this looks like when we add Redis as a service to a Docker Compose stack.
Create your docker-compose.yml file and add the following:
version: '3.2'
services:
redis:
image: "redis:alpine"
command: redis-server
expose:
- "6379"
volumes:
- redis-data:/data
- redis-conf:/usr/local/etc/redis/redis.conf
volumes:
redis-data:
redis-conf:
This service definition is fairly straightforward. We will:
- create a service called
redisbased on theredis:alpineDocker image - on service start, run the command
redis-serverto start the Redis server - set
expose: "6379"to expose the container’s port to the local Docker network. To expose the Redis port to the host and clients outside of the Docker network, useports: 6379:6379instead ofexpose. - create two volumes,
redis-dataandredis-conf. The prior will be responsible for persisting our Redis key/value data on the host’s hard disk and the latter will volumizing the Redis configuration file for viewing on the host.
There are many arguments we can pass to the command and many environment variables to set to configure your Redis server even further, but this is the basic setup that will cover most initial use cases.
Basic Redis Authentication
Depending on your use case, it may be a good idea to require basic authentication for your Redis server. To do this, we simply add --requirepass to our command , like so:
version: '3.2'
services:
redis:
image: "redis:alpine"
command: redis-server --requirepass ${REDIS_PASS:-password123}
expose:
- "6379"
volumes:
- redis-data:/data
- redis-conf:/usr/local/etc/redis/redis.conf
volumes:
redis-data:
redis-conf:
Now, when we enter our Docker container like usual:

This happened because we didn’t provide our password as a named argument (-a) to the redis-cli command. Trying again:

We use the -a argument to provide the password that was specified in the docker-compose.yml file; in this case, it was password123.
Conclusion
Redis is a powerful, fast, and lightweight key-value store used in tech stacks by companies big and small. It has a small learning curve to get started and Docker makes it easy to deploy and connect to a new Redis server in minutes.
Sources
redislabs. “Redis.” Redis, redis.io/documentation. Accessed 29 Nov. 2020.
redislabs. “Command Reference — Redis.” Redis, redis.io/commands. Accessed 29 Nov. 2020.