Redis Basics

Redis is an in-memory key-value store.

Redis use case

  • Cache data
  • User session

Installation

Download, extract and compile Redis

1
2
3
4
$ wget http://download.redis.io/releases/redis-5.0.3.tar.gz
$ tar xzf redis-5.0.3.tar.gz
$ cd redis-5.0.3
$ make

The binaries are located at the src directory. To start the server:

1
./src/redis-server &

default port is 6379

To start a Redis CLI

1
./src/redis-cli

To test Redis using CLI

1
2
3
4
5
127.0.0.1:6379> set foo bar
OK
127.0.0.1:6379> get foo
"bar"
127.0.0.1:6379> exit

There is also a Docker Image for Redis you can use.

1
docker run -d --rm -p 6379:6379 --name redis-demo redis

You can then get into the container using bash or sh command.

1
docker exec -it redis-demo bash

Redis container has installed redis-cli you can use to interact with redis.

1
# redis-cli

Redis Keys

You can use any binary sequence as a key, meaning you can use string or even JPEG file. Maximum key size is 512M.

Try to stick with a schema for the key. convention is to use colon(:). For Instance “user:100”, “user:100:password”. for multi-words key, use dots. comment:1234:replay.to”

KEYS

KEYS command Returns all keys matching pattern.

show keys matching pattern

1
2
3
> KEYS *name*
1) "lastname"
2) "firstname"

show all keys

1
2
3
4
> KEYS *
1) "lastname"
2) "age"
3) "firstname"

DEL

Use DEL Command to delete a key

1
> DEL mykey

TYPE

Use TYPE Command to check the type of a key

1
> TYPE firstname

The return type can be “string”, “hash”, “list”, “set” or “zset”.

Data Types

String

The simpliest data structure

1
2
3
4
> SET site:about "this is my site"
OK
> GET site:about
"this is my site"

Hash

data type to store objects.

1
2
3
4
5
6
7
8
> HSET books:helloredis title "hello redis"
(integer) 1

> HSET books:helloredis price 33.4
(integer) 1

> HGET books:helloredis title
"hello redis"

HGETALL Command Returns all fields and values of the hash stored at key. In the return value, every field name is followed by its value, so the length of the reply is twice the size of the hash.

1
2
3
> HGETALL books:helloredis
1) "title"
2) "hello redis"

List

An array of values.

1
2
3
4
5
6
> LPUSH books:helloredis:authors 'alice' 'bob'
(integer) 2

> LRANGE books:helloredis:authors 0 10
1) "bob"
2) "alice"

Set

unordered collection of values

1
2
3
4
5
6
> SADD books:helloredis:tags nosql in-memory
(integer) 2

> SMEMBERS books:helloredis:tags
1) "in-memory"
2) "nosql"

You can use SETEX command to set timeout in seconds

1
2
3
4
> SETEX mykey 10 "Hello"
OK
redis:6379> TTL mykey
(integer) 5

Sorted Set

values are associated with a score. score is used for sorting.

1
2
3
4
5
6
7
8
9
10
11
12
13
> ZADD tags 1 redis
(integer) 1

> ZADD tags 3 nosql
(integer) 1

> ZADD tags 2 it
(integer) 1

> ZRANGE tags 0 10
1) "redis"
2) "it"
3) "nosql"

Pub/Sub

Redis supports Pub/Sub. senders(publishers) can send messages to specific receivers(subscribers).

To subscribe to a channel

1
> subscribe foo

To publish a message to foo

1
2
> publish foo "Hello Redis"
(integer) 1

Other Commands

Monitor

MONITOR is a debugging command that streams back every command processed by the Redis server. It can help in understanding what is happening to the database.

1
2
3
4
5
6
7
> monitor
1339518083.107412 [0 127.0.0.1:60866] "keys" "*"
1339518087.877697 [0 127.0.0.1:60866] "dbsize"
1339518090.420270 [0 127.0.0.1:60866] "set" "x" "6"
1339518096.506257 [0 127.0.0.1:60866] "get" "x"
1339518099.363765 [0 127.0.0.1:60866] "del" "x"
1339518100.544926 [0 127.0.0.1:60866] "get" "x"

Reference