3301. Redis - Tutorial

Tutorial for Redis

1. Redis

Redis is an open source, BSD licensed, advanced key-value store. It is often referred to as a data structure server, since the keys can contain strings, hashes, lists, sets and sorted sets. Redis is written in C.

Redis has three main peculiarities that sets it apart.

  • Redis holds its database entirely in the memory, using the disk only for persistence.
  • Redis has a relatively rich set of data types when compared to many key-value data stores.
  • Redis can replicate data to any number of slaves.

2. Install Redis on Ubuntu

2.1 Install Redis

$sudo apt-get update
$sudo apt-get install redis-server

Open redis prompt.

$ redis-cli

Test connection.

redis> ping

Syntax for remote server.

$ redis-cli -h host -p port -a password

Example for connecting to remote redis server.

$redis-cli -h -p 6379 -a "mypass"
redis> PING  

2.2 Connect Redis installed in Ubuntu Virtual Machine

First, find the ip address of Ubuntu. Settings->Network, click the setting icon, see the ip address, eg. ‘’.

Edit file /etc/redis/redis.conf in Ubuntu. Change bind from ‘ ::1’ to ‘ ::1’. Or directly comment out.

bind ::1

Restart redis server.

sudo systemctl restart redis

Check if redis can be access with non-local ip address.

$ redis-cli -h>ping

2.3 Redis Desktop Manager

Go to https://redisdesktop.com/download download and install Redis Desktop Manager.

2.4 Configuration




redis> CONFIG SET loglevel "notice"
redis> CONFIG GET loglevel  
1) "loglevel"
2) "notice"

3. Date Type

3.1 Strings

Redis string is a sequence of bytes.

redis> SET name "johnny"
redis> GET name

3.2 Hashes

Redis hash is a collection of key value pairs. Redis Hashes are maps between string fields and string values. Hence, they are used to represent objects.

redis> HMSET user:1 username johnny password 123456 email csgeek@mail.com
redis> HGETALL user:1  
1) "username"
2) "johnny"
3) "password"
4) "123456"
5) "email"
6) "csgeek@mail.com"

3.3 Lists

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.

redis> lpush database redis
(integer) 1
redis> lpush database mongodb
(integer) 2
redis> lpush database mysql
(integer) 3
redis> lrange database 0 10  

1) "mysql"
2) "mongodb"
3) "redis"

3.4 Sets

Redis Sets are an unordered collection of strings.

redis> sadd database redis
(integer) 1
redis> sadd database mongodb
(integer) 1
redis> sadd database mysql
(integer) 1
redis> sadd database mysql
(integer) 0
redis> smembers database  

1) "mysql"
2) "mongodb"
3) "redis"
  • mysql is added twice, however due to unique property of the set, it is added only once.

3.5 Sorted Sets

Redis Sorted Sets are similar to Redis Sets, non-repeating collections of Strings. The difference is, every member of a Sorted Set is associated with a score, that is used in order to take the sorted set ordered, from the smallest to the greatest score. While members are unique, the scores may be repeated.

redis> zadd database 0 redis
(integer) 1
redis> zadd database 0 mongodb
(integer) 1
redis> zadd database 0 mysql
(integer) 1
redis> zadd database 0 mysql
(integer) 0
redis> ZRANGEBYSCORE tutoriallist 0 1000  

1) "redis"
2) "mongodb"
3) "mysql"

4. References