REDIS
REDIS is an in-memory database.
It is not a replacement for a traditional RDBMS like MySQL, Oracle & PostgreSQL.
REDIS works as a key value store – we will learn more about what this means from the exercises below.
REDIS is typically used in cases where we need fast data indexing, data access or multiple simultaneous data reads / writes (it handles concurrency very well).
REDIS is well documented here:
http://redis.io/documentation
Command Reference:
http://redis.io/commands
Install Redis on Ubuntu:
1 2 3 4 5 6 7 8 9 10 11 12 |
wget http://download.redis.io/redis-stable.tar.gz tar xvzf redis-stable.tar.gz cd redis-stable make sudo make install make test sudo mkdir /etc/redis sudo mkdir /var/redis sudo cp utils/redis_init_script /etc/init.d/redis_6379 sudo cp redis.conf /etc/redis/6379.conf sudo mkdir /var/redis/6379 sudo vi /etc/redis/6379.conf |
In the configuration file make the following settings:
Set daemonize to yes (by default it is set to no).
Set the pidfile to /var/run/redis_6379.pid (modify the port if needed).
Change the port accordingly. In our example it is not needed as the default port is already 6379.
Set your preferred loglevel.
Set the logfile to /var/log/redis_6379.log
Set the dir to /var/redis/6379 (very important step!)
1 2 |
sudo update-rc.d redis_6379 defaults redis-cli save |
check the file if it was created in: /var/redis/6379/dump.rdb
Now lets play with Redis remotely from a GUI client Ubuntu-Desktop:
On Ubuntu VM Configure Redis for remote connection
1 |
sudo vi /etc/redis/6379.conf |
Add the following line:
(This allows us to connect to the Redis server from a remote machine)
1 |
bind 0.0.0.0 |
Restart Redis
1 2 |
sudo service redis_6379 stop sudo service redis_6379 start |
Check Ubuntu VM IP address.
1 |
ifconfig |
Install Redis Desktop
Download and install Redis Desktop on your MAC or PC:
http://redisdesktop.com/download
Configure Redis connection in Redis Desktop using this ip address
“Connect to Redis Server”
Enter IP address from Ubuntu
Test Connection
If this doesn’t work try setting your virtual machine network settings to Bridged mode in VMWare / VirtualBox
Try these exercises in the Redis Desktop application to learn about Redis
You can open Redis Desktop, connect to the Ubuntu Redis server and run the commands in the console.
String
Redis string is a sequence of bytes. Strings in Redis are binary safe, meaning they have a known length not determined by any special terminating characters, so you can store anything up to 512 megabytes in one string.
1 2 |
SET kaisha_name "Avinton" GET kaisha_name |
Substring:
1 |
GETRANGE kaisha_name 2 3 |
SET
SET new value and GET old value
1 |
GETSET kaisha_name "Avinton Japan KK" |
MGET
Get multiple values at the same time
1 2 |
SET project z MGET project kaisha_name |
SETEX
Set a key with expiry timer
1 |
SETEX expired_key 10 expire |
Expiry TTL
To check how much life it has left before it dies:
1 |
TTL expired_key |
Hash HMSET
A Redis hash is a collection of key value pairs. Redis Hashes are maps between string fields and string values, so they are used to represent objects
1 2 |
HMSET kaisha:1 kaisha_name avinton kaisha_type KK employees 70 HGETALL kaisha:1 |
Lists LPUSH LRANGE
Redis Lists are simply lists of strings, sorted by insertion order. You can add elements to a Redis List on the head or on the tail.
1 2 3 4 5 6 |
lpush training linux lpush training postgresq lpush training redis lpush training memory lpush training cpu lpush training gpu |
1 |
lrange training 2 3 |
Sets SADD SMEMBERS
Redis Sets are an unordered collection of Strings. In redis you can add, remove, and test for existence of members in O(1) time complexity.
1 2 3 4 5 |
sadd avinton gibo-san sadd avinton misaki-san sadd avinton fukushima-san sadd avinton sato-san sadd avinton takahara-san |
1 |
smembers avinton |
Sorted Sets ZADD ZRANGEBYSCORE
Redis Sorted Sets are, similarly to Redis Sets, non repeating collections of Strings. The difference is that every member of a Sorted Set is associated with score, that is used in order to take the sorted set ordered, from the smallest to the greatest score. While members are unique, scores may be repeated.
1 2 3 4 5 |
zadd avintonSS 0 redis zadd avintonSS 1 postgresql zadd avintonSS 2 linux zadd avintonSS 2 python zadd avintonSS 1 cpu |
1 |
ZRANGEBYSCORE avintonSS 2 2 |
Conclusion
Above we have seen some examples of how to use REDIS.
This gives us an idea about REDIS’ flexibility despite being a “simple” key value store.
We will choose from these different types (string, sets, lists, sorted sets and Hashes etc based on the data we want to store.