This article was originally published in my Medium blog with the title "Redis the Proper Way". When I decided to re-publish it on
dev.to
, I found the title to be misleading. There are a lot you should do with redis to make it the "proper way" that I have touched upon. Thus, now the article is titled more appropriately.
When I first started familiarizing myself with redis, I thought about it as a big key value store. Sort of like a fancy hashmap. That said, according to redis official website, it is “in-memory data structure store”. The difference is, data structure store can have keys represented as another data structure. Some of the possibilities are strings, hashes, sets, and lists. If you think about it, this kind of store gives almost infinite abilities to design a broker, cache, or even database.
At my previous work, we were using redis to store a state of a load balancer, managing a pool of video streaming servers. That said, this tutorial aims to be helpful for any redis applications. I will cover steps needed to setup redis on your linux machine, and configure it the proper way. So, let’s start!
First of all, I assume that you already installed make and gcc. If you are using any firewall solution, please allow traffic on port which redis is going to be running on! After you checkout all of the prerequisites, we can proceed.
Installation
Download Redis source code, and build it:
wget http://download.redis.io/redis-stable.tar.gz
tar xvzf redis-stable.tar.gz
cd redis-stable
make
It is highly possible that you see the following error:
zmalloc.h:50:10: fatal error: jemalloc/jemalloc.h: No such file or directory
#include <jemalloc/jemalloc.h>
In this case you have to run:
make distclean
make
Once build completed, copy the executables into /usr/local/bin
, or any other location you store your executables.
sudo cp src/redis-server /usr/local/bin/
sudo cp src/redis-cli /usr/local/bin/
Congratulations, you successfully installed redis server, and cli tooling on your vm! We can end it here, but there are quite a few step to make the configuration complete.
Configuration
Next step is to create init script, so redis is restarted automatically every time your server reboots. Example of the script could be found in the source code, so we can just copy it, and later modify.
In this guide, I will use a port number to easier identify which redis-server instance the file is related to. You don’t have to do it, but it is a common convention.
sudo cp utils/redis_init_script /etc/init.d/redis_5000
Once done, open the file with your favorite text editor, and set proper REDISPORT
value, in my case 5000
.
Now, create directories to store Redis config files, and persistent data:
sudo mkdir /etc/redis
sudo mkdir /var/redis/5000 -p
Copy configuration file, from source foler to your newly created directory
sudo cp redis.conf /etc/redis/5000.conf
Make the following changes to your configuration file:
- Set
daemonize
toyes
- Set the
pidfile
to/var/run/redis_5000.pid
- Change the
port
to5000
- Set the
logfile
to/var/log/redis_5000.log
- Set the
dir
to/var/redis/5000
- Set
requirepass
to some secure password. I will set mine toSUPER_SECRET_PASSWORD
(Hint: secure password can be generated by runningopenssl rand -hex 32
)
Don’t forget to link your init script:
sudo update-rc.d redis_5000 defaults
Finally, you can start your Redis:
sudo /etc/init.d/redis_5000 start
To test your installation and configuration, you can use Redis cli client:
redis-cli -p 5000
It will open a new connection. At first you will have to authenticate by running AUTH SUPER_SECRET_PASSWORD
. Now, you enter PING
, and you should get back PONG
!
Top comments (0)