First, Redis operating principle

The Redis server is single-threaded for command processing, but the I/O layer serves multiple clients concurrently, and the conversion of concurrency to an internal single thread is achieved by a multiplexing framework. The redis command is sent to the executive manager in the following four processes

  1. Send the command
  2. The command line
  3. Command execution
  4. Results back

Ii. Redis executes the agreement

Capture packets for redis requests and view the captured packets

1. Run the packet capture command

2. View the captured packets

The Redis protocol is located above the TCP layer, that is, the client and the Redis instance maintain a duplex connection, the interaction is serialized protocol data

RESP agreement

The Redis server communicates with the client through the Redis Serialization Protocol (RESP).

RESP uses TCP connection. Data is transmitted through TCP, and then the corresponding information is parsed according to the parsing rules to complete the interaction.

We can test this by first running a serverSocket listening on 6379 to receive requests from redis clients. To achieve the following

1. Establish a socket connection to listen on port 6379 for receiving requests and output

/** */ ** * <p> SimulatedRedis </p> ** @author DK * @version V1.0 */ public class SimulatedRedis {public static void main(String[] args) throws IOException { ServerSocket server = new ServerSocket(6379); Socket rec = server.accept(); byte[] result = new byte[2048]; rec.getInputStream().read(result); System.out.println(new String(result)); }}Copy the code

2. Use the Jedis client to request local port 6379

/** */ ** * <p> redis</p> ** @author DK * @version V1.0 */ public class SimulatedRedisClient {public static void Main (String[] args) throws IOException {Jedis Jedis = new Jedis("127.0.0.1", 6379); jedis.set("user:2", "9999"); jedis.close(); }}Copy the code

A printout

The discovery is consistent with the captured packet

Each field indicates the following meanings:

*3 indicates several sets of data

$3 indicates that the set length is 3

The Set command

$6 indicates that the key length is 6

User:2   key

$1 indicates that the length of value is 1

 2       value

The advantages of Redis using resP protocol are easy to implement, fast parsing, human readable, and transmission at the TCP layer, which can reduce unnecessary information transmission

Simulate resP protocol for redis writing

Public class RespRedis {public static void main(String[]); /** */ ** * <p> </p> ** @author DK * @version V1.0 */ Args) {SocketAddress addr = new InetSocketAddress("10.1.253.188", 6379); Socket sk = new Socket(); try { sk.connect(addr); OutputStream out = sk.getOutputStream(); StringBuffer sb = new StringBuffer(); sb.append("*3\r\n"); sb.append("$3\r\n"); sb.append("SET\r\n"); sb.append("$6\r\n"); sb.append("user:0\r\n"); sb.append("$5\r\n"); sb.append("11111\r\n"); System.out.println(sb.toString()); byte[] b = sb.toString().getBytes(); out.write(b); out.flush(); } catch (IOException e) { e.printStackTrace(); }}}Copy the code

3. Redis performance test

Redis slow query analysis

The same with mysql: When the execution time exceeds the maximum value, the time consuming command will be recorded

Life cycle of the redis command: the time of the third step is only counted in the slow query

Redis slow query extremum Settings

Two modes are available. The default time is 10ms

1. Command mode

Config set slowlog-log-slower-than 10000 //10 ms

If you want to persist the configuration to redis.conf after using config sets, run the config rewrite command

2. Modify the configuration file

Conf: find slowlog-log-log-slower than 10000 and save it

Note: slowlog-log-slower-than =0 Record all command-1 commands are not recorded

Principles of Slow Query

Slow-max-len Specifies the maximum number of records to be stored in the queue.

For example, when slow-max-len is set to 10, when the 11th slow query command is inserted, the first command in the queue

It’s going to be out of the column, and the 11th is going to be in the slow query queue, and you can config set it dynamically,

You can also modify redis.conf to complete the configuration

Slow Query Command

Get the command of slow query in the queue: slowlog get

Slowlog len // Only one slow query above, return 1;

1) to clear the list of slow queries (reset) : slowlog reset // Return 0 to clear the list;

2) for the online slow-max-len configuration: the value of slow-max-len can be increased online. Redis will truncate the record of slow query when storing long command, which will not occupy a large amount of memory. The value of slow-max-len can be set to more than 1000 online

3), recommendation for online slowlog-log-log-slower than configuration: default is 10 ms, adjusted according to the amount of Redis concurrency, recommended for high concurrency ratio is 1 ms

4) Slow query is a first-in, first-out queue, and access log records are lost when they are out of the column. It is necessary to execute slowlog get regularly and store the results in other devices (such as mysql).

1、redis-benchmark -h 127.0.0.1 -p 6379 -c 100 -n 10000

100 concurrent connections, 10000 requests, check server performance

\