Overview
During my time studying the Data Storage and Big Data Processing course at university, I learned about Redis. This is a NoSQL database. Unlike other databases, Redis is an in-memory storage system with key-value storage structure. Besides data storage, Redis is also used for caching, message broker, and queue functionality. Redis supports many basic data structures such as strings, hash, list, set, etc. Since data is stored in RAM, it can be lost during failures, so Redis has support for backup to hard disk for recovery during incidents. With such capabilities, Redis has applications in real-time analytics or IoT problems.
Installing Redis
Installing Redis is very simple, you can refer to https://redis.io/download.
After installation, open terminal and run redis-cli which will connect to the default host localhost and default port 6379.
Data Types in Redis
1. Strings
strings is the most basic data type in Redis, stored in binary format. We have two operations: SET and GET. Note that SET will replace any existing value stored in the key, even if the key’s value is not a string.
1
2
3
4
127.0.0.1:6379> set myname viethoang
OK
127.0.0.1:6379> get myname
"viethoang"
2. List
The list data type is simply a list of strings arranged in insertion order. We can add to the left or right end with LPUSH or RPUSH operations. The key will be deleted if the list becomes empty after LPOP or RPOP operations.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
127.0.0.1:6379> KEYS *
1) "myname"
2) "users"
127.0.0.1:6379> lpush users viethoang2
(integer) 2
127.0.0.1:6379> rpush users viethoang3
(integer) 3
127.0.0.1:6379> lrange users 0 10
1) "viethoang2"
2) "viethoang1"
3) "viethoang3"
127.0.0.1:6379> lpop users
"viethoang2"
127.0.0.1:6379> rpop users
"viethoang3"
127.0.0.1:6379> lpop users
"viethoang1"
3. Set
The set data type is an unordered collection of strings. Operations for adding elements, reading, deleting individual elements, and checking element existence are O(1). It also supports set operations like intersection/union/difference.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
127.0.0.1:6379> sadd comics one-piece
(integer) 1
127.0.0.1:6379> sadd comics conan
(integer) 1
127.0.0.1:6379> sadd comics naruto
(integer) 1
127.0.0.1:6379> smembers comics
1) "naruto"
2) "one-piece"
3) "conan"
127.0.0.1:6379> spop comics
"naruto"
127.0.0.1:6379> smembers comics
1) "one-piece"
2) "conan"
4. Hashes
hash is a hash table storage type for key-value pairs. For example, hashes are commonly used to store user information (fields like name, age, address, etc.). Hash also supports add, read, delete operations, and reading all values inside.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
127.0.0.1:6379> hmset character-1 name viethoang school hust age 21
OK
127.0.0.1:6379> hgetall character-1
1) "name"
2) "viethoang"
3) "school"
4) "hust"
5) "age"
6) "21"
127.0.0.1:6379> hget character-1 age
"21"
127.0.0.1:6379> hget character-1 school
"hust"
127.0.0.1:6379> hget character-1 name
"viethoang"
5. Sorted set (zset)
sorted set is a collection of unique strings sorted by a score/weight.
1
2
3
4
5
6
7
8
9
10
127.0.0.1:6379> zadd myzset 10 element_1
(integer) 1
127.0.0.1:6379> zadd myzset 2 element_2
(integer) 1
127.0.0.1:6379> zadd myzset 6 element_3
(integer) 1
127.0.0.1:6379> zrangebyscore myzset 0 100
1) "element_2"
2) "element_3"
3) "element_1"
Benefits of Using Redis
Query Performance
Since data is stored in RAM, Redis read and write operations average less than 1/1000 second and can support millions of operations per second, unlike other storage systems that require round-trips to disk.
Data Durability
Redis uses primary-replica architecture so data can be replicated to multiple different servers. To ensure data is not lost, Redis can support backup to disk (point-in-time backups).
Some Applications of Redis
Caching
Session store
Real time analytics
Redis can act as a queue similar to Apache Kafka. This helps separate input and output data streams without data loss.
