Redis is an in-memory data structure store, used as a distributed, in-memory key–value database. Here is an easy handbook for your reference to quickly start it.


You can configure Redis Server by below configuration file.

sudo vi /etc/redis.conf

Redis CLI arguments:

# Connect to a Redis server (e.g. Default is DB 0)
$ redis-cli [-n db_no]

# Get the version of Redis Client
$ redis-cli -v

# Enable keyspace events notification feature (It consumes lots of CPU resources)
$ redis-cli config set notify-keyspace-events KEA

# Monitor all events notification by CLI
#   K     Keyspace events, published with __keyspace@<db>__ prefix.
#   E     Keyevent events, published with __keyevent@<db>__ prefix.
#   g     Generic commands (non-type specific) like DEL, EXPIRE, RENAME, ...
#   $     String commands
#   l     List commands
#   s     Set commands
#   h     Hash commands
#   z     Sorted set commands
#   x     Expired events (events generated every time a key expires)
#   e     Evicted events (events generated when a key is evicted for maxmemory)
#   A     Alias for g$lshzxe, so that the "AKE" string means all the events.
$ redis-cli --csv psubscribe '__key*__:*'
Reading messages... (press Ctrl-C to quit)

# Show statistic of clients
$ redis-cli --stat

# Show keys, the same as `keys *` but it supports Regular Expression
$ redis-cli --scan

# Scanning the entire keyspace to find biggest keys as well as
# average sizes per key type.  You can use -i 0.1 to sleep 0.1 sec
# per 100 SCAN commands (not usually needed).
$ redis-cli --bigkeys

# Monitor all received redis commands
$ redis-cli monitor

Redis commands:

# Test heart beat> ping

# Flush current database (i.e. Delete all keys of current database)> flushdb

# Flush all databases (i.e. Delete all keys of all database)> flushall

# Get all information of Redis server> info

# Get the information of server> info server

# Get all configurations> config get *

# Get the configuraion of dir> config get dir

# Get all keys> keys *

# Set expiration for a key> expire <key> 3

# Delete a specific key> del <key>

# Get the the amount of keys> dbsize

# Exit Redis Client> exit

Data Operations

String> set name foo
OK> get name


# Set a new hash (M for multi)> hmset king username foo password 12345 age 18

# Get the value of a specific key> hget king username

# Set a key-value pair (Auto create the key if it doesn't exist)> hset king alias "bar"
(integer) 1

# Get all key-value> hgetall king
1) "username"
2) "foo"
3) "password"
4) "12345"
5) "age"
6) "18"
7) "alias"
8) "bar

# Delete a key-value pair> hdel king alias
(integer) 1


# Push an element to the end of list> lpush users foo
(integer) 1> lpush users bar
(integer) 2> lpush users baz
(integer) 3

# Get the length of list> llen users
(integer) 3

# Get a range of elements of list> lrange users 0 10
1) "foo"
2) "bar"
3) "baz"


# Add an element to users set> sadd users foo
(integer) 1> sadd users bar
(integer) 1> sadd users baz
(integer) 1> sadd users bar
(integer) 0

# Is a member of the set> sismember users bar
(integer) 1

# Get amount of a set> scard users
(integer) 3

# List members of a set> smembers users
1) "foo"
2) "bar"
3) "baz"

# Remove one or more elements> srem users bar baz
(integer) 1

Sorted Set

# Add an element to users sorted set> zadd users 1 foo
(integer) 1> zadd users 2 bar
(integer) 1> zadd users 3 baz
(integer) 1> zadd users 4 baz
(integer) 0

# Get score of an member --> O(1)> zscore users bar
(integer) 2

# Check if it's a member of the sorted set> zscore users zoo

# Get amount of a sorted set> scount users
(integer) 3

# List members of a sorted set> zrange users 0 10
1) "foo"
2) "bar"
3) "baz"

# List members of a sorted set with scores> zrange users 0 10 withscores
1) "foo"
2) "1"
3) "bar"
4) "2"
5) "baz"
6) "4"

# Remove one or more elements> zrem users bar baz
(integer) 1