Redis Command Execution Process, redis command process
This problem is simple and simple. The client sends a command request, the server reads the command request, and then the command executor searches for the command implementation, executes the Preparatory operation, calls the command to implement the function, and executes the subsequent work.
But what we want to know is nothing simple. Next we will analyze in detail the whole process of Redis command execution.
Send command request
However, when a user enters a command request through the client, the client first converts the format of the command request entered by the user to the Protocol format, then, the formatted command is sent to the server through the socket connecting to the server (the server is connected to the client through a set of characters or other servers)
READ command request
However, when the client and the server socket are readable due to the writing of the client (you need to check the content of the Redis file event. I will not repeat it here ),
Server MEETINGFirstRead the protocol-format command request from the socket and save it to the input buffer in the client state (the size of the input buffer can be dynamically reduced or expanded based on the input content, but it cannot exceed 1 GB. Otherwise, the server will close the client)
ThenThe server analyzes the command requests in the input buffer, extracts the command parameters contained in the command request, and the number of parameters, then, save the parameters and the number of parameters to the argv and argc attributes in the client state respectively (the argv attribute is an array, and each item in the array is a string object, where argv [0] is the command to be executed, and other subsequent items are the passed command parameters. The argc attribute is responsible for recording the length of the argv array)
Finally, call the command executor to execute the commands specified by the client.
Command Execution
After the server analyzes the values of argv and argc attributes, the server finds the corresponding command implementation function in the Command table based on the value of arg [0, the Redis command indicates that the key of a dictionary (another application of the dictionary) is the name of the command, and the value corresponds to a redisCommand structure, saves statistical information such as the command implementation function, command flag, number of parameters that should be specified for the command, total number of command executions, and total consumption time.
After finding the implementation function is not a real execution command, you still need to perform some checks,
Check whether the command name entered by the user can find the corresponding command implementation,
Check whether the number of parameters entered by the user meets the requirements (the redisCommand structure has an arity attribute. If it is-3, the number of input parameters must be greater than or equal to 3)
Then, check whether the client has passed authentication. If the authenticated attribute in the client status is 1, otherwise it is 0 (Auth command)
Check the memory usage, whether the stop-writes-on-bgsave-error is enabled when bgsave is executed last time, and so on.
After the check is passed, the implementation function of the command is called, that is, the real execution command. After the execution is completed, the execution result is saved to the output buffer in the client status. There are two output buffers, one is a fixed size of 16 KB, which is used to save replies with a small length, and the other is a variable size output buffer to save replies with a large length. [Note: the server limits the size of the client output buffer in two modes: hard and soft. If the size exceeds the hard limit, the server immediately closes the client, if the system exceeds the soft limit, but there is no super-hard limit, the record exceeds the time limit. If the record exceeds the maximum duration and exceeds the set time, the client will be closed. On the contrary, the client will not be closed]
Finally, perform subsequent work, including updating slow query log records, updating some statistics in the redisCommand structure, and some persistent operations and replication operations.
Finally, the result is sent to the client, which is still a socket.
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.