NoSQL——Redis configuration and optimization

Table of contents

1. Relational databases and non-relational databases

1.1 Relational database

1.2 Non-relational database

1.3 Differences between relational databases and non-relational databases

1.3.1 Background of non-relational databases 

2. Introduction to Redis

2.1 redis advantages:

3. Redis installation and deployment  

4. Redis command tool 

4.1 redis-cli command line tool

4.2 redis-benchmark testing tool

4.3 Common commands for Redis database

4.4 Common Redis multi-database commands

5. Redis High Availability


1. Relational databases and non-relational databases

1.1 Relational database

  • A relational database is a structured database created on the basis of a relational model ( two-dimensional tabular model ) and is generally oriented towards records.
  • SQL statements (Standard Data Query Language) are a language based on relational databases and are used to retrieve and operate data in relational databases.
  • Mainstream relational databases include Oracle, MySQL, SQL Server, Microsoft Access, DB2, PostgreSQL , etc.
  • When using the above database, you must first build a database, create a table, and design the table structure, and then store the data according to the table structure. If the data does not match the table structure, the storage will fail.

1.2 Non-relational database

  • NoSQL (NoSQL = Not Only SQL), which means "not just SQL" , is the general term for non-relational databases.
  • Databases other than mainstream relational databases are considered non-relational.
  • There is no need to build databases and tables in advance to define the data storage table structure . Each record can have different data types and number of fields (such as text, pictures, videos, music, etc. in WeChat group chats).
  • Mainstream NoSQL databases include Redis, MongBD, Hbase, Memcached , etc.

1.3 Differences between relational databases and non-relational databases

  • Data storage is different
  1. The main difference between relational and non-relational databases is the way the data is stored . Relational data is naturally tabular and therefore stored in rows and columns of data tables. Data tables can be stored in association with each other and can be easily retrieved.
  2. In contrast, non-relational data does not fit into the rows and columns of a data table, but is grouped together in large chunks. Non-relational data is typically stored in datasets, such as documents, key-value pairs, or graph structures . Your data and its characteristics are the primary influencing factors in choosing how to store and retrieve your data.
  • Different ways to expand
  1. The biggest difference between SQL and NoSQL databases may be in the way of expansion. To support the growing demand, of course, expansion is required.
  2. To support more concurrency, SQL databases scale vertically , which means increasing processing power and using faster computers so that the same data set can be processed faster. Because the data is stored in relational tables, operational performance bottlenecks can involve a lot to overcome. Although the SQL database has a lot of room for expansion, it will eventually reach the upper limit of vertical expansion tables, which requires improving computer performance .
  3. NoSQL databases scale horizontally . Because non-relational data storage is naturally distributed, NoSQL databases can be expanded by adding more common database servers (nodes) to the resource pool to share the load .
  •  Different support for transactional
  1. If data operations require high transactionality or complex data queries require control of execution plans, then a traditional SQL database is your best choice in terms of performance and stability. SQL database supports fine-grained control over transaction atomicity and easy rollback of transactions .
     
  2. Although NoSQL databases can also use transaction operations, they cannot be compared with relational databases in terms of stability, so their real shining value lies in the scalability of operations and the processing of large data volumes.
1.3.1 Background of non-relational databases 

Non-relational databases are mainly used to deal with the three high ( high availability, high concurrency, high storage ) problems of Web2.0 purely dynamic website types .

(1) High performance - the need for high concurrent reading and writing of the database
(2) Huge Storage - the need for efficient storage and access of massive data
(3) High Scalability && High Availability - the need for high scalability and high availability of the database

 Relational databases and non-relational databases have their own characteristics and application scenarios. The close combination of the two will bring new ideas to the development of Web2.0 databases. Let relational databases focus on relationships, and non-relational databases focus on storage. For example, in a MySQL database environment where reading and writing are separated, frequently accessed data can be stored in a non-relational database to improve access speed.

2. Introduction to Redis

  • Redis (remote dictionary server) is an open source NoSQL database written in C language.
  • Redis runs based on memory and supports persistence. It adopts key-value (key-value pair) storage form and is an indispensable part of the current distributed architecture.
  • The Redis server program is a single-process model, that is, multiple Redis processes can be started simultaneously on one server. The actual processing speed of Redis completely depends on the execution efficiency of the main process. If only one Redis process is running on the server, when multiple clients access it at the same time, the server's processing capability will decrease to a certain extent; if multiple Redis processes are opened on the same server, Redis will improve concurrent processing capabilities. At the same time, it will put a lot of pressure on the server's CPU. That is: in an actual production environment, you need to decide how many Redis processes to start based on actual needs. If you have higher requirements for high concurrency, you may consider starting multiple processes on the same server. If CPU resources are tight, use a single process.

2.1 redis advantages:

(1) Extremely high data reading and writing speed: the data reading speed can reach up to 110,000 times/s, and the data writing speed can reach up to 81,000 times/s.
(2) Supports rich data types: supports key-value, Strings, Lists, Hashes, Sets and Sorted Sets and other data type operations.
(3) Support data persistence: the data in the memory can be saved on the disk and can be loaded again for use when restarting.
(4) Atomicity: All Redis operations are atomic.
(5) Support data backup: data backup in master-salve mode.

Redis is a memory-based database, and caching is one of its most common application scenarios. In addition, common application scenarios of Redis also include operations to obtain the latest N data, ranking applications, counter applications, storage relationships, real-time analysis systems, and logging.

ps: Why is Redis so fast?
1. Redis is a pure memory structure , which avoids time-consuming operations such as disk I/O.
2. The core module of Redis command processing is single-threaded, which reduces lock competition, the cost of frequently creating and destroying threads, and reduces the consumption of thread context switching.
3. The I/O multiplexing mechanism is adopted, which greatly improves concurrency efficiency.

Note: The newly added multi-threading in Redis 6.0 only uses multi-linearity for processing network requests, while data read and write commands are still processed by a single thread.
 

3. Redis installation and deployment  

systemctl stop firewalld
setenforce 0

yum install -y gcc gcc-c++ make

tar zxvf redis-5.0.7.tar.gz -C /opt/

cd /opt/redis-5.0.7/
make
make PREFIX=/usr/local/redis install
#由于Redis源码包中直接提供了 Makefile 文件,所以在解压完软件包后,不用先执行 ./configure 进行配置,可直接执行 make 与 make install 命令进行安装。

#执行软件包提供的 install_server.sh 脚本文件设置 Redis 服务所需要的相关配置文件
cd /opt/redis-5.0.7/utils
./install_server.sh
......					#一直回车
Please select the redis executable path [/usr/local/bin/redis-server] /usr/local/redis/bin/redis-server  	#需要手动修改为 /usr/local/redis/bin/redis-server ,注意要一次性正确输入
----------------------------------------------------------------------------------------------------------
Selected config:
Port           : 6379								#默认侦听端口为6379
Config file    : /etc/redis/6379.conf				#配置文件路径
Log file       : /var/log/redis_6379.log			#日志文件路径
Data dir       : /var/lib/redis/6379				#数据文件路径
Executable     : /usr/local/redis/bin/redis-server	#可执行文件路径
Cli Executable : /usr/local/bin/redis-cli			#客户端命令工具
----------------------------------------------------------------------------------------------------------

#把redis的可执行程序文件放入路径环境变量的目录中便于系统识别
ln -s /usr/local/redis/bin/* /usr/local/bin/

#当 install_server.sh 脚本运行完毕,Redis 服务就已经启动,默认监听端口为 6379
netstat -natp | grep redis

#Redis 服务控制
/etc/init.d/redis_6379 stop				#停止
/etc/init.d/redis_6379 start			#启动
/etc/init.d/redis_6379 restart			#重启
/etc/init.d/redis_6379 status			#状态

#修改配置 /etc/redis/6379.conf 参数
vim /etc/redis/6379.conf
bind 127.0.0.1 192.168.10.23				#70行,添加 监听的主机地址
port 6379									#93行,Redis默认的监听端口
daemonize yes								#137行,启用守护进程
pidfile /var/run/redis_6379.pid				#159行,指定 PID 文件
loglevel notice								#167行,日志级别
logfile /var/log/redis_6379.log				#172行,指定日志文件


/etc/init.d/redis_6379 restart

4. Redis command tool 

  • redis-server: Tool for starting Redis
  • redis-benchmark: used to detect the operating efficiency of Redis on the local machine
  • redis-check-aof: Repair AOF persistence files
  • redis-check-rdb: Repair RDB persistence files
  • redis-cli: Redis command line tool

4.1 redis-cli command line tool

语法:redis-cli -h host -p port -a password
-h :指定远程主机
-p :指定 Redis 服务的端口号
-a :指定密码,未设置数据库密码可以省略-a 选项
若不添加任何选项表示,则使用 127.0.0.1:6379 连接本机上的 Redis 数据库

例:
redis-cli -h 192.168.181.103 -p 6379

4.2 redis-benchmark testing tool

redis-benchmark 是官方自带的 Redis 性能测试工具,可以有效的测试 Redis 服务的性能。
基本的测试语法:redis-benchmark [选项] [选项值]。
-h :指定服务器主机名。
-p :指定服务器端口。
-s :指定服务器 socket
-c :指定并发连接数。 
-n :指定请求数。
-d :以字节的形式指定 SET/GET 值的数据大小。
-k :1=keep alive 0=reconnect 。
-r :SET/GET/INCR 使用随机 key, SADD 使用随机值。
-P :通过管道传输<numreq>请求。
-q :强制退出 redis。仅显示 query/sec 值。
--csv :以 CSV 格式输出。
-l :生成循环,永久执行测试。
-t :仅运行以逗号分隔的测试命令列表。
-I :Idle 模式。仅打开 N 个 idle 连接并等待。

#向 IP 地址为 192.168.181.103、端口为 6379 的 Redis 服务器发送 100 个并发连接与 100000 个请求测试性能
redis-benchmark -h 192.168.181.103 -p 6379 -c 100 -n 100000

#测试存取大小为 100 字节的数据包的性能
redis-benchmark -h 192.168.181.103 -p 6379 -q -d 100

#测试本机上 Redis 服务在进行 set 与 lpush 操作时的性能
redis-benchmark -t set,lpush -n 100000 -q

4.3 Common commands for Redis database

set:存放数据,命令格式为 set key value
get:获取数据,命令格式为 get key

127.0.0.1:6379> set teacher zhangsan
OK
127.0.0.1:6379> get teacher
"zhangsan"

# keys 命令可以取符合规则的键值列表,通常情况可以结合*、?等选项来使用。
127.0.0.1:6379> set k1 1
127.0.0.1:6379> set k2 2
127.0.0.1:6379> set k3 3
127.0.0.1:6379> set v1 4
127.0.0.1:6379> set v5 5
127.0.0.1:6379> set v22 5

127.0.0.1:6379> KEYS *				#查看当前数据库中所有键

127.0.0.1:6379> KEYS v*				#查看当前数据库中以 v 开头的数据

127.0.0.1:6379> KEYS v?				#查看当前数据库中以 v 开头后面包含任意一位的数据

127.0.0.1:6379> KEYS v??				#查看当前数据库中以 v 开头 v 开头后面包含任意两位的数据

# exists 命令可以判断键值是否存在。
127.0.0.1:6379> exists teacher		#判断 teacher 键是否存在
(integer) 1							# 1 表示 teacher 键是存在
127.0.0.1:6379> exists tea
(integer) 0							# 0 表示 tea 键不存在

# del 命令可以删除当前数据库的指定 key。
127.0.0.1:6379> keys *
127.0.0.1:6379> del v5
127.0.0.1:6379> get v5

# type 命令可以获取 key 对应的 value 值类型。
127.0.0.1:6379> type k1
string

# rename 命令是对已有 key 进行重命名。(覆盖)
命令格式:rename 源key 目标key
使用rename命令进行重命名时,无论目标key是否存在都进行重命名,且源key的值会覆盖目标key的值。在实际使用过程中,建议先用 exists 命令查看目标 key 是否存在,然后再决定是否执行 rename 命令,以避免覆盖重要数据。

127.0.0.1:6379> keys v*
1) "v1"
2) "v22"
127.0.0.1:6379> rename v22 v2
OK
127.0.0.1:6379> keys v*
1) "v1"
2) "v2"
127.0.0.1:6379> get v1
"4"
127.0.0.1:6379> get v2
"5"
127.0.0.1:6379> rename v1 v2
OK
127.0.0.1:6379> get v1
(nil)
127.0.0.1:6379> get v2
"4"

# renamenx 命令的作用是对已有 key 进行重命名,并检测新名是否存在,如果目标 key 存在则不进行重命名。(不覆盖)
命令格式:renamenx 源key 目标key
127.0.0.1:6379> keys *
127.0.0.1:6379> get teacher
"zhangsan"
127.0.0.1:6379> get v2
"4"
127.0.0.1:6379> renamenx v2 teacher
(integer) 0
127.0.0.1:6379> keys *
127.0.0.1:6379> get teacher
"zhangsan"
127.0.0.1:6379> get v2
"4"

# dbsize 命令的作用是查看当前数据库中 key 的数目。
127.0.0.1:6379> dbsize


#使用config set requirepass yourpassword命令设置密码
127.0.0.1:6379> config set requirepass 123456

#使用config get requirepass命令查看密码(一旦设置密码,必须先验证通过密码,否则所有操作不可用)
127.0.0.1:6379> auth 123456
127.0.0.1:6379> config get requirepass

4.4 Common Redis multi-database commands

Redis supports multiple databases. Redis contains 16 databases by default. The database names are named sequentially with numbers 0-15.
Multiple databases are independent of each other and do not interfere with each other.

#多数据库间切换
命令格式:select 序号
使用 redis-cli 连接 Redis 数据库后,默认使用的是序号为 0 的数据库。

127.0.0.1:6379> select 10			#切换至序号为 10 的数据库

127.0.0.1:6379[10]> select 15		#切换至序号为 15 的数据库

127.0.0.1:6379[15]> select 0			#切换至序号为 0 的数据库

#多数据库间移动数据
格式:move 键值 序号

127.0.0.1:6379> set k1 100
OK
127.0.0.1:6379> get k1
"100"
127.0.0.1:6379> select 1
OK
127.0.0.1:6379[1]> get k1
(nil)
127.0.0.1:6379[1]> select 0			#切换至目标数据库 0
OK
127.0.0.1:6379> get k1				#查看目标数据是否存在
"100"
127.0.0.1:6379> move k1 1			#将数据库 0 中 k1 移动到数据库 1 中
(integer) 1
127.0.0.1:6379> select 1				#切换至目标数据库 1
OK
127.0.0.1:6379[1]> get k1			#查看被移动数据
"100"
127.0.0.1:6379[1]> select 0
OK
127.0.0.1:6379> get k1				#在数据库 0 中无法查看到 k1 的值
(nil)

#清除数据库内数据
FLUSHDB :清空当前数据库数据
FLUSHALL :清空所有数据库的数据,慎用!

5. Redis High Availability

In web servers, high availability refers to the time the server can be accessed normally, and the measurement standard is how long it can provide normal services (99.9%, 99.99%, 99.999%, etc.).
However, in the context of Redis, the meaning of high availability seems to be broader. In addition to ensuring the provision of normal services (such as master-slave separation, fast disaster recovery technology), it is also necessary to consider the expansion of data capacity, data security and no loss, etc.

 

In Redis, the technologies to achieve high availability mainly include persistence, master-slave replication, sentry and Cluster clusters. The following describes their functions and what problems they solve.


●Persistence: Persistence is the simplest high-availability method (sometimes not even classified as a high-availability method). Its main function is data backup, that is, storing data on the hard disk to ensure that the data will not be lost due to process exit.
●Master-slave replication: Master-slave replication is the basis of high-availability Redis. Both Sentinel and Cluster achieve high availability based on master-slave replication. Master-slave replication mainly implements multi-machine backup of data, as well as load balancing and simple fault recovery for read operations. Disadvantages: Failure recovery cannot be automated; write operations cannot be load balanced; storage capacity is limited by a single machine.
●Sentinel: Based on master-slave replication, Sentinel implements automated fault recovery. Disadvantages: Write operations cannot be load balanced; storage capacity is limited by a single machine.
●Cluster cluster: Through clustering, Redis solves the problem that write operations cannot be load balanced and storage capacity is limited by a single machine, achieving a relatively complete high-availability solution.

 

Guess you like

Origin blog.csdn.net/Sp_Tizzy/article/details/131942446