Redis command Execution whole process

Source: Internet
Author: User

The problem is simple and simple, it is simply that the client sends a command request, the server reads the command request, then the command executor finds the command implementation, performs the preparatory operation, invokes the command implementation function, and performs the follow-up work.
But what we want to know is not simple. Let's take a detailed look at the entire process of redis command execution.

Send command request

However, when a user enters a command request through the client, the client first converts the command request entered by the user into the protocol format, The formatted command is then sent to the server via a socket that is connected to the server (the server communicates through the nested word and the client or other server connections)

Read Command request

However, the client and server-side sockets are readable when the client writes (this requires reviewing the contents of the Redis file event, which is not mentioned here),
The server will first read the command request in the protocol format in the socket and save it to the client state input buffer (where the size of the input buffer can be reduced or expanded dynamically depending on the input, but the maximum cannot exceed 1GB or the server will shut down the client)
The server then parses the command request in the input buffer, extracts the command arguments contained in the command request, and then saves the parameters and the number of arguments to the argv and ARGC properties of the client state (the ARGV property is an array, Each item in the array is a string object, where Argv[0] is the command to execute, and then the other items are passed to the command arguments, and the ARGC property is responsible for recording the length of the argv array)
Finally, the command executor is invoked to execute the command specified by the client.

Command execution

When the server parses the values of the argv and ARGC properties, the server finds the corresponding command implementation function in the command table based on the value of arg[0], and the Redis command represents a dictionary (another application of the dictionary) key is the name of the command, Value corresponds to a rediscommand structure that holds the command's implementation function, the command's flag, the number of arguments the command should specify, the total number of times the command executes, and the total consumption time.
After finding the implementation function is not really executing the command, there are some checks to be done,
Check whether the command name entered by the user can find the corresponding command implementation,
Then check whether the number of parameters entered by the user conforms to the requirements (there is a arity attribute in the Rediscommand structure, if 3 means that the number of arguments entered must be greater than or equal to 3)
Then also check whether the client passed authentication, if the value of the authenticated property in the client state passed authentication is 1, otherwise 0 (auth command)
There is memory consumption, the last time the execution of Bgsave error, whether open Stop-writes-on-bgsave-error and so on check.
Check all through the implementation of the call command, that is, the actual execution of the command, after the execution of the results will be executed to save the client state output buffer, the output buffer has two, a fixed size of 16Kb, used to save a small length of the reply, there is a variable size output buffer, Used to hold a larger reply "Note: The server restricts the size of the client output buffer by two modes, one is the hard limit, one is the soft limit, if the size of the hard limit is exceeded, the server immediately shuts down the client, if the soft limit is exceeded, but there is no super-hard limit, then the record exceeds the time, If the duration exceeds the set time, the client is turned off, instead of shutting down the
Finally, follow-up work is performed, including updating slow query logging, updating some of the statistics in the Rediscommand structure, and some persistence operations, copy operations, and so on.
The final end is to send the results to the client, which is still a socket.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Redis command Execution whole process

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.