Redis database detailing and parameter tuning

Keywords: Linux Redis Database Oracle MySQL

Redis cluster configuration example:
1. The difference between relational and non-relational databases:

Redis database is a non-relational database, not the same type as oracle, mysql, sql server and other relational databases. NoSQL is the general name of non-relational databases. The mainstream NOSQL databases include redis, MongBD and so on. NoSQL's storage mode, storage structure and usage scenarios are completely different. NoSQL database is considered as the next generation of database products because of its advantages of non-relational, distributed, open source and horizontal expansion.

NoSQL can solve the three problems of relational database:

  1. High concurrent read and write requirements for database.
  2. Requirements for efficient storage and access of massive data.
  3. Requirements for high scalability and availability of databases.

Relational database and non-relational database have their own characteristics and application scenarios. The precise combination of them will bring new ideas to the development of Web 2.0 database. Let relational databases focus on relationships, while non-relational databases focus on storage.
.
2. Introduction of redis database:

Redis is an open source, C-language-based, network-enabled, memory-based and persistent log type, key-value (key-value pair) database, which is an indispensable part of the current distributed architecture.

Redis server is a single process model, that is, multiple Redis processes can be started simultaneously on a single server, while the actual processing speed of Redis depends entirely on the execution efficiency of the main process. If only one Redis process is running on the server, when multiple client s access at the same time, the processing capacity of the server will decrease to a certain extent; if multiple Redis processes are opened on the same server, Redis will cause great pressure on the CPU of the server while improving the concurrent processing capacity. That is to say, in the actual production environment, we need to decide how many Redis processes to open according to the actual needs. If the requirement of high concurrency is higher, it may be considered to open multiple processes on the same server; if CPU resources are tight, single process can be used.

Redis has the following points:

  1. With a very high speed of reading and writing, the maximum speed of data reading can reach 110,000 times/s, and the highest speed of data writing can reach 81,000 times/s.
  2. It supports rich data types, not only simple key-value data types, but also data type operations such as strings, lists, hashes, sets and ordered sets.
  3. Supporting data persistence, data in memory can be saved in disk, reboot can be loaded again for use.
  4. Atomicity. All operations of Redis are atomic.
  5. Support data backup, that is, master-salve mode of data backup.

.
Redis is a database running in memory. Caching is one of the most frequently used scenarios. In addition, the common application scenarios of Redis include the operation of acquiring the latest N data, ranking application, counter application, storage relationship, real-time analysis system, log recording, etc.
.
3. Redis installation and deployment:

You can download the source packages from Redis or from this: https://pan.baidu.com/s/1rGl8OwfYuYAB-aEXcZ7wRw (Extraction code: zg3w) Download directly.

[root@redis ~]# tar zxf redis-5.0.5.tar.gz -C /usr/src/          #Unpacking
[root@redis ~]# cd /usr/src/redis-5.0.5/                  #Switch to the unzipped directory
[root@redis redis-5.0.5]# make && make install    #No need for. / configure to check and configure the environment, just make installation.
[root@redis redis-5.0.5]# cd /usr/src/redis-5.0.5/utils/    #Setting up the relevant configuration file
[root@redis utils]# ./install_server.sh       #Execute the script, and the relevant configuration file is generated.
#Next is to specify the storage directory of various configuration files, and confirm by return all the way.
[root@redis utils]# cd /etc/init.d/             #OPTIMIZATION OF CONTROL SERVICE START-UP AND STOP
[root@redis init.d]# mv redis_6379 redis
[root@redis init.d]# chkconfig --add redis         #Add as a system service.
[root@redis init.d]# systemctl restart redis
[root@redis init.d]# netstat -anpt | grep redis   # redis defaults to listen on 6379 and cluster port 16379
tcp        0      0 127.0.0.1:6379     0.0.0.0:*    LISTEN      7098/redis-server 1 
#Now that redis is installed, the cluster is not configured, so port 16379 is not listening.
[root@redis init.d]# vim /etc/redis/6379.conf             #View redis configuration file
bind 127.0.0.1 192.168.1.1             #Host address monitored
appendonly yes  #Change to "yes" to log and write data synchronously after each update operation
port 6379                                        #Monitor port
daemonize yes                               #Enabling Daemons
pidfile /var/run/redis_6379.pid        #Specify the PID file
loglevel notice                                #log level
logfile /var/log/redis_6379.log        #Specify log files

The above is part of the configuration parameters. There are many more configuration parameters in the main configuration, which can be referred to as follows:

4. Redis Command Tools and Common Commands:

Redis software provides many command tools. When installing Redis, the included software tools will be installed into the system at the same time and can be directly used in the system. The functions of these command tools are as follows:

  1. redis-server: Tool for starting Redis.
  2. redis-benchmark: Used to detect Redis's efficiency on the machine.
  3. redis-check-aof: Fix AOF persistent files.
  4. redis-check-rdb: Fix RDB persistent files.
  5. redis-cli: Redis command-line tool.

.
The following are the use of redis-cli and redis-benchmark tools:

1. redis-cli command-line tool:
.
The redis-cli command can connect to the specified database, specify the remote host with'-h','-p specifies the port number of the service', specify the password with'-a'if the password is set, and omit the'-a' option if the connection password is not set. As follows:

[root@redis redis]# redis-cli -h 192.168.1.1 -p 6379      
#If the port number defaults to listen on 6379, the - p option can also be omitted.
192.168.1.1:6379>
//In a database operating environment, help commands can be used to obtain help of command type. There are three ways to get command help.

help @<group>: Obtain<group>List of commands in.
help <command>: Get help from a command.
help <tab> : Get a list of topics that might help.
//Examples are as follows:

192.168.1.1:6379> help @list         #View all commands related to list data types

  BLPOP key [key ...] timeout
  summary: ck until one is available
  since: 2.0.0

  BRPOP key [key ...] timeout
  summary: block until one is available
  since: 2.0.0
             ...........................
192.168.1.1:6379> help set       #View the command help for the set command.

  SET key value [expiration EX seconds|PX milliseconds] [NX|XX]
  summary: Set the string value of a key
  since: 1.0.0
  group: string

2. redis-benchmark stress testing tool:
.
redis-benchmark is an official Redis performance testing tool, which can effectively test the performance of Redis services. Common options for this tool are as follows:

[root@redis redis]# redis-benchmark -h 192.168.1.1 -p 6379 -c 100 -n 100000
#Send 100 concurrent connections and 100000 requests to the Eds server with the specified IP address and port to test performance.

           .........................
====== MSET (10 keys) ======
  100000 requests completed in 1.74 seconds
  100 parallel clients
  3 bytes payload
  keep alive: 1

23.94% <= 1 milliseconds
98.02% <= 2 milliseconds
99.64% <= 3 milliseconds
99.76% <= 4 milliseconds
99.82% <= 5 milliseconds
99.89% <= 6 milliseconds
99.90% <= 7 milliseconds
99.94% <= 8 milliseconds
99.97% <= 9 milliseconds
100.00% <= 10 milliseconds
100.00% <= 10 milliseconds
57603.69 requests per second

#The test results show that milliseconds are milliseconds, and the shorter the time, the better the performance.
[root@redis redis]# redis-benchmark -h 192.168.1.1 -p 6379 -q -d 100
#Test the performance of accessing 100B packets to the specified port 6379 of the specified Redis server.
[root@redis redis]# redis-benchmark -t set,lpush -n 100000 -q
#Test the performance of Redis service on the local machine during set and lpush operations.
SET: 67659.00 requests per second
LPUSH: 64516.13 requests per second

Common commands in Redis database:
.
Redis database uses the form of key-value (key-value pair) data storage. The commands used are set and get.
.

  • Set: Stores data in the form of set key value.
  • Get: Get the data, the command format is get key.
[root@localhost ~]# redis-cli          #Connect to local database
127.0.0.1:6379 > set test zhangsan             #insert data
OK
127.0.0.1:6379> get test         #Query Key
"zhangsan"
127.0.0.1:6379> keys t*               #Query data starting with "t"
1) "test"
127.0.0.1:6379> keys t?         #Queries begin with "t" and then contain data of any one bit
(empty list or set)                 #No, no qualified data.
127.0.0.1:6379> keys t??         #The query starts with "t" and then contains an arbitrary two-digit data
(empty list or set)
127.0.0.1:6379> exists test          #Determine whether the "test" key exists
(integer) 1                                              #Represents the existence of the test key
127.0.0.1:6379> exists yy                    #Judging whether the "yy" key exists
(integer) 0
127.0.0.1:6379> keys *        #Query all keys in the current library
1) "counter:__rand_int__"
2) "mylist"
3) "key:__rand_int__"
4) "test"
5) "myset:__rand_int__" 
127.0.0.1:6379> del test                         #Delete the test key
(integer) 1
127.0.0.1:6379> set www 23
OK
127.0.0.1:6379> type www            #Get the value type corresponding to the key "www"
string

The rename command can rename an existing key, and another rename command is renamenx. The difference between the two is that the target key value changed by the former will be changed whether it exists or not, directly covering the data of the target key value; the renamenx command of the latter will check whether the target key value exists when it changes, and if it exists, it will abandon the change.

127.0.0.1:6379> get www
"23"
127.0.0.1:6379> set qq 123            #Insert a piece of data
OK
127.0.0.1:6379> renamenx www qq                #Change "www" to "qq"
(integer) 0                                                         #"qq" exists, abandon change
127.0.0.1:6379> rename www qq             #Use the rename command to make changes
OK
127.0.0.1:6379> get qq               #Direct coverage of target data
"23"
127.0.0.1:6379> dbsize                  #View the number of key s in the current database
(integer) 5

Common commands for multiple databases:

Redis supports multiple databases. Redis includes 16 databases by default without any changes. The database names are named in turn using numbers 0-15. The data in each library is independent, that is to say, the data in 0 libraries can not be found in 10 libraries.

127.0.0.1:6379> select 10        #Switch to Library No. 10
OK
127.0.0.1:6379[10]> select 15          #Switch to Library No. 15
OK
127.0.0.1:6379[15]> select 0              #Switch to Library No. 0
OK
127.0.0.1:6379> move liuyi 10    #Move the data in the library to 10 Libraries
(integer) 1
127.0.0.1:6379> select 10            #Switch to Library No. 10
OK
127.0.0.1:6379[10]> get liuyi                 #View the moving Library
"23"
#No "liuyi" data can be found in Library 0.

Clear the data in the database:

Redis database data deletion is mainly divided into two parts: emptying the data in the current database, using the flushdb command; emptying all the data in the library, using the flushall command. Database clearing operation is dangerous, careful use in production environment!!!

Posted by chooseodie on Fri, 20 Sep 2019 04:11:29 -0700