Redis is a wonderful open source solution for caching high-read configuration/information. It is a key/value pair store and has faster reads thanks to its in-memory storage. An example of a key/value pair store is a media website where a few articles are set fixed on their home page for a few hours.
Instead of letting every reader hit their database to fetch a record, a media house can use Redis to store article content. That is one of many applications of Redis.
Job status is temporary information that becomes irrelevant once the job is finished and status is logged to log storage. Due to this, Redis is the best choice for implementing job status caching. We plan to do the following things:
- Write a job status
- Read a job status
Both actions are performed by our job server but at different times. The status can be in three forms:
- Started
- In Progress
- Done
Redis provides a rich collection of data structures to hold information temporarily. Out of them, we use a simple Key:String for our job status. Job ID can be a key and status is its value. In simple notation, it looks like this:
{ '4ed59a6f-24d8-4179-8432-fe4adcdd4f51': 'In Progress'
We can easily run a Redis instance with Docker. Just run a Redis container and expose port 6379:
> docker run --name some-redis -p 6379:6379 -d redis
The preceding command runs a Redis server on localhost on port 6379. The process will be run as a daemon with the -d option. To check which container is for Redis, you can simply run the following Docker command:
> docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2f0b2b457ed7 redis "docker-entrypoint.s…" 8 minutes ago Up 8 minutes 0.0.0.0:6379->6379/tcp some-redis
c3b2a0a0295d rabbitmq:3 "docker-entrypoint.s…" 6 weeks ago Up 11 hours 4369/tcp, 0.0.0.0:5672->5672/tcp, 5671/tcp, 25672/tcp, 0.0.0.0:15672->15672/tcp rabbitmq-server
redis-cli is a tool that can be used to quickly inspect the Redis server. You don't have to install it separately. Your Redis Docker instance already has it built in. You just have to execute a command against the Redis container, like this:
> docker exec -i -t some-redis redis-cli
127.0.0.1:6379>
You can get all the available keys stored in Redis with the following command:
127.0.0.1:6379> KEYS *
You can also set a key to a value with an expiration date in the CLI, like this:
127.0.0.1:6379> SET topic async
OK
The preceding command sets a key called topic to async. The server returns OK for a successful insert. The CLI is easy to use, but in most cases, you can also access Redis from applications. In the next section, we'll learn how to do this from Go programs.