table of Contents
Two, redis execution agreement
Simulate resp protocol for redis write
Redis slow query extreme value setting
1. Redis operating principle
The redis server processes commands in a single thread, but the I/O level provides services to multiple clients concurrently, and the conversion from concurrency to an internal single thread is implemented through a multiplexing framework. The following four processes from sending the redis command to the executive manager
- send command
- Command queue
- Command execution
- Result returned
Two, redis execution agreement
Capture the redis request and view the captured content
1. Execute the capture command
2. View the captured packet content
The redis protocol is located above the TCP layer, that is, the client and the redis instance maintain a duplex connection, and the exchange is serialized protocol data
RESP protocol
Redis server and client communicate through RESP (REdis Serialization Protocol) protocol.
The bottom layer of RESP adopts the TCP connection mode, which transmits data through tcp, and then parses the corresponding information according to the analysis rules to complete the interaction.
We can test, first run a serverSocket monitor 6379 to receive the request information from the redis client. Implementation is as follows
1. Establish a socket connection and monitor port 6379 for receiving requests and outputting
/**
* /**
* <p>模拟redis</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));
}
}
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();
}
}
Printout
Found that the content is consistent with the captured packet
The meaning of each field is:
*3 means there are several sets of data
$ 3 means set length is 3
Set command
$6 means the key length is 6
User:2 key
$1 means value length is 1
2 value
The main features are as follows: From the above examples, it is found that the benefits of using the resp protocol for redis are easy to implement, fast to parse, human readable, and transmitted at the TCP layer, which can reduce unnecessary information transmission
Simulate resp protocol for redis write
/**
* /**
* <p>模拟redis</p>
*
* @author DK
* @version V1.0
*/
public class RespRedis {
public static void main(String[] 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();
}
}
}
Three, redis performance test
Redis slow query analysis
Same as mysql: when the execution time exceeds the maximum value, the time-consuming command will be recorded
redis command lifecycle: Send queued execution returns, only slow query statistics of 3 Ge perform the steps of time
Redis slow query extreme value setting
There are generally two ways, the default time is 10ms
1. Command method
config set slowlog-log-slower-than 10000 //10毫秒
After using config set, if you want to persist the configuration to redis.conf, execute config rewrite
2. Configuration file modification
Redis.conf modification: find slowlog-log-slower-than 10000, modify and save
Note: slowlog-log-slower-than =0 records all commands -1 commands do not record
Slow query principle
Slow query records are also stored in the queue, the maximum number of records stored in slow-max-len,
For example, if you set slow-max-len=10, when the 11th slow query command is inserted, the first command in the queue
Will be listed, and the 11th entry will be listed in the slow query queue, which can be dynamically set by config set,
You can also modify redis.conf to complete the configuration
Slow query command
Get the slow query command in the queue: slowlog get
Get the current length of the slow query list: slowlog len //There is only 1 slow query above, return 1;
1) , to clean up the slow query list (reset): slowlog reset // then check slowlog len returns 0 empty at this time;
2 ) , to recommend online slow-max-len configurations: lines can increase the value of slow-max-len, the record store long slow query truncated when redis command will do, will not take up a lot of memory, can be set online Above 1000
3 ) , for online recommendation slowlog-log-slower-than configuration: Default is 10 ms, is adjusted according to the amount of concurrency redis, recommendations for high concurrency than 1 millisecond
4 ) . Slow query is a first-in-first-out queue. Access log records are lost when dequeuing, and slowlog get needs to be executed periodically to 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, 10,000 requests, check server performance