This is the fourth day of my participation in the August More text Challenge. For details, see: August More Text Challenge

Redis configuration

The installation of Redis

Install GCC:

Redis is developed in C language. To install Redis, you need to compile the source code downloaded from the official website. The compilation depends on the GCC environment

Make sure root is logged in first, and Linux is connected to the network

Then go to the ~ directory

[root@xxx ~]#
Copy the code

Enter the installation command

Yum -y install GCC automake autoconf libtool make ==== Check the GCC version GCC -vCopy the code

Ps: /var/run/yum.pid has been locked, pid is XXXX another program is running problem resolved

rm-f/var/run/yum.pid
Copy the code

2. Redis installation

Official download: redis. IO /download

1, install,

Here I install is reids5.0+ version, before the installation of 6.0 error, so still use 5.0, really dare not toss about ~

Wget HTTP: / / https://download.redis.io/releases/redis-5.0.7.tar.gzCopy the code

2. Put Redis in /usr/local and decompress tar -zxvf redis-5.0.7.tar.gz

3. Run the command

Command 1:

CD redis - 5.0.7Copy the code

Command 2:

=== Compile make === success flagCopy the code

If GCC is not installed, an error will be reported

Command 3:

make PREFIX=/usr/local/redis install
Copy the code

== note == : PREFIX must be capitalized. At the same time, we will automatically create the redis directory for us and install the result of this directory

Command 4:

cd /usr/local/redis/bin
Copy the code

Redis configuration

Copy the redis.conf file from the Redis installation package to the Redis installation directory (/usr/local/redis).

[root@xiaojian redis-5.0.7]# cp redis.conf /usr/local/redis
Copy the code
1. By default, Redis does not run as a daemon process. You can modify this configuration item by running yes to enable the daemon process daemonize no 3. The default port is 6379. Why choose 6379 as the default port? Because 6379 is the number corresponding to MERZ on the mobile phone button, and MERZ is the name of Alessia MERZ. Bind 127.0.0.1 8. Bind 127.0.0.1 8. Set the number of databases. The default database is 0. Run the SELECT <dbid> command to specify the database ID. Save <seconds> <changes> the default Redis configuration file provides three conditions: Save 900 1 Save 300 10 Save 60 10000 Indicates 1 change within 900 seconds (15 minutes), 10 changes within 300 seconds (5 minutes), and 10000 changes within 60 seconds, respectively. Specify the name of the local database file. The default value is dump.rdb dbfilename dump.rdb 15. Set a Redis connection password. If a connection password is configured, the client needs to use AUTH <password> to provide the password when connecting to Redis. Requirepass Foobared 16 is disabled by default. Set the maximum number of client connections that Redis can open at the same time. The default value is unlimited. The maximum number of client connections that Redis can open at the same time is the maximum number of file descriptors that Redis can open at the same time. When the number of clients reached the limit, Redis will close new connections and return Max number of clients reached error message maxCLIENTS 128 17. When Redis starts, it loads data into the memory. When the memory reaches the maximum, Redis will try to remove keys that have expired or are about to expire. After this method is processed, the memory still reaches the maximum setting, and the data cannot be written, but the data can still be read. Maxmemory <bytes> // Suggestion: If the memory size is 1 GB, the Value ranges from 256 to 512 18. Specifies whether to log after each update. By default, Redis writes data asynchronously to disk. If it is not enabled, data may be lost for a period of time during a power failure. Because redis synchronizes data files according to the save condition above, some data will only exist in memory for a period of time. The default value is no appendonly no 19. Specify the name of the update log file. The default value is appendonlyCopy the code

Memory maintenance policies in Redis

As an excellent intermediate cache, Redis often stores a large amount of data. Even if cluster deployment is adopted for dynamic capacity expansion, the memory should be organized to maintain system performance. There are two solutions in Redis,

  • One is to set the timeout period for the data;

  • Second, LRU algorithm is used to dynamically delete the unused data. A page replacement algorithm for memory management. For data blocks that are in memory but not in use, called LRU, the operating system moves them out of memory based on which data belong to LRU to make room for loading additional data.

    • 1. Volatile -lru: Delete the least frequently used data from the set timeout period.
    • 2. Allkeys-lru: query allkeys for the most recently used data and delete it. This is the most widely used policy.
    • 3. Volatile -random: Random deletion of data that has timed out.
    • 4. Allkeys-random: queries allkeys and deletes them randomly.
    • 5. Volatile – TTL: Queries all the data whose timeout period is set, sorts the data, and deletes the data that will expire soon.
    • 6. Noeviction: if set to this property, no deletion is performed and an error is returned if memory is spilled. Volatile -lfu: expels the least frequently-used key from allkeys that are configured with an expiration time allkeys-lfu: From all key to expel the least frequently used in key www.jianshu.com/p/c8aeb3eee…

Customize Redis

Change daemonize no to daemonize yes. Daemon, running in the background, cannot remotely connect to the Redis service requirePass set password unless manually kill the process bind 127.0.0.1 and comment it outCopy the code

Redis start

In the redis installation directory

#Server startup
./bin/redis-server ./redis.conf

#Client startup
#The local client is started
	./bin/redis-cli -a password
#The remote client is started-port: indicates the port on which Redis is started. -password: indicates the password set by requirePass in the configuration file
#Test connection successful127.0.0.1:6379 > ping PONGCopy the code

The closing of the Redis

The first closure mode :(power off, abnormal shutdown. Easy to lose data)

#Check out the PID: 
ps -ef | grep -i redis
#Forcibly Shut down a Process
kill -9 PID
Copy the code

The second closing mode (normal closing, data saving)

#The redis service is shutdown on the client
./bin/redis-cli shutdown 
#If you have a password, log in first and then shutdown/bin/reis -cli -a password 127.0.0.1:6379> shutdownCopy the code

Test performance

Redis-sentinel is a stress test tool!

Official performance testing tools,

The serial number options describe The default value
1 -h Specify the server host name 127.0.0.1
2 -p Specify the server port 6379
3 -s Specifying the server socket
4 -c Specifies the number of concurrent connections 50
5 -n Specify number of requests 10000
6 -d Specifies the data size of the SET/GET value in bytes 2
7 -k 1=keep alive 0=reconnect 1
8 -r SET/GET/INCR uses random keys and SADD uses random values
9 -P The request is piped 1
10 -q Forcibly exit Redis. Only query/ SEC values are displayed
11 –csv Output in CSV format
12 -l Build a loop that permanently executes tests
13 -t Run only a comma-separated list of test commands.
14 -I Idle mode. Open only N idle connections and wait.

Test it out:

Test: 50 concurrent connections, 10000 requests
[root@xiaojian bin]# ./redis-benchmark -h localhost -p 6379 -c 50 -n 10000
Copy the code