Posts Redis 101 (Part I)
Post
Cancel

Redis 101 (Part I)

Overview

During my studies in Big Data Storage and Processing at school, I learned about Redis. This is a NoSQL database. Unlike other databases, this is an in-memory storage type with key-value storage. In addition to storing data, Redis is also used for caching, message broker, and queue. Redis also supports many basic data structures such as strings, hash, list, set,… Since storage on RAM will be lost when encountering issues, Redis has support for backing up to hard disk to recover when encountering problems. With such capabilities, Redis will have 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, it will connect to the default host localhost and default port 6379.

Data Types in Redis

1. Strings

strings is the most basic data type of 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 that key’s value is not strings.

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 string values arranged in the order they were inserted. We can add to the left or right end with operations LPUSH or RPUSH. The key will be deleted if the list is 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 string values. Operations to add elements, read, delete individual elements, and check element presence are O(1). Additionally, it supports intersection/union/difference operations.

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, hash is commonly used to store users (fields like name, age, address,…). Hash also supports operations to add, read, delete, and read 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 weight score.

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

Because it stores data in RAM, Redis performance for read/write operations averages less than 1/1000 of a second and can support millions of operations per second, unlike other storage systems that require round-trips to disk.

Data Reliability

Redis uses a primary-replica architecture so data can be replicated to multiple different servers. To ensure data is not lost, Redis can support backing up 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 will help separate data input and output without loss.

https://raw.githubusercontent.com/demanejar/image-collection/refs/heads/main/Redis101/redis-realtime-ana.png

This post is licensed under CC BY 4.0 by the author.