Redis is an open source, in-memory data structure store which is commonly used as a database, cache or a message broker. Since data is stored and retrieved in memory, operations are very fast with a time complexity of O(1).
Data is stored in Key Value pairs. To access a value, you need to specify the key, the value will be null if it doesn’t exist. Redis has many commands and can be run in multiple modes like master-slave mode to replicate data easily between two Redis servers.
Installation
Installing redis is simple, below is the installation procedure to get Redis installed on an ubuntu 16.04 system:
First Update the apt packages:
sudo apt update
Next install build-essentials and tcl
sudo apt install build-essential tcl8.5 -y
Next get the latest release from Redis
sudo wget http://download.redis.io/releases/redis-stable.tar.gz
untar it
sudo tar xzf redis-stable.tar.gz
Next cd into the new directory created
cd redis-stable
Next Make and install
sudo make && sudo make install
Now Redis has been installed on your server, Now you want to run the Redis server as a background deamon, to do this, we need to run the install_server.sh script found in the utils directory
cd utils && sudo ./install_server.sh
Next to start the redis background deamon, use the following command
sudo service redis start
Next you can access the redis cli by using the command
redis-cli
the above command will show you a prompt with a port number on which redis is running, now you can use redis commands directly in the prompt.
set name redis
sets the key (name) with value (redis)
get name
will return the value of the key (name)
del name
Will delete the key value pair with key(name).
You can find all the commands on the Redis website
By default redis listens for connections from anywhere, to force redis to accept connections only from localhost, we have to bind it to localhost. To do this, we edit the config file.
sudo nano /etc/redis/redis.config
In the file, uncomment or add the following line
bind 127.0.0.1
Now that the Redis server has been set up, you can use an redis clients to connect to the server, here we will use a nodejs app to connect to the redis server and to read and write data.
You will need to first install the redis client via npm
npm install redis --save
Next you create a client object
var redis = require("redis"); client = redis.createClient();
Now you can use the client object to perform redis operations like the one you performed in the redis-cli
client.set("string key", "string val");
client.get("string key");
client.del("string key");
Redis as a Cache
Apart from using Redis as an in-memory database, you can also use redis as a cache, Redis has a maxmemory directive which specifies the max memory that redis can use for caching, when memory used exceeds that of maxmemory, Redis removes keys from using one of the the following eviction policies:
- noeviction: no keys are evicted when the maxmemory is reached, commands that use more memory fail with errors when redis tries to use any more memory
- allkeys-lru:Keys that are used the least are evicted first.
- volatile-lru: Remove least recently used keys first which have an expire set.
- allkeys-random: Remove keys randomly.
- volatile-random: Remove keys with an expire set randomly.
- volatile-ttl: Remove keys which have expire set and have a short time to live(TTL) first.