Redis design and implementation Learning notes-server

Source: Internet
Author: User
Tags server memory redis server

The Redis server is responsible for establishing connections with multiple clients, processing client requests, and saving individual database state. By using an event handler implemented by I/O multiplexing technology, the Redis server uses single-threaded single processes to process client command requests. Redis records the various states of the service side through the redisserver structure.

Command request Execution procedure

1. The client sends a command request and the client translates the command request into the protocol format.
2. The server reads the command request, caches the command request in the client input buffer, analyzes the command in the input buffer, saves the parameters and the number of arguments to the argv property and the Argc property of the client state, and then invokes the command executor to execute the specified command.
3. The command executor looks for the command specified by the parameter in the command table according to the argv[0] parameter and saves the found command to the client state cmd property
4. Perform preparatory operations:

    •   Checks if the client-side cmd pointer is pointing to NULL, and if it returns an error,
    •   The Arity property of the Rediscommand structure pointed to by the client-side CMD property, Check that the number of arguments given by the command request is correct, and if the number of arguments incorrectly returns an error
    •   Checks if the client is authenticated, the unauthenticated client can only execute the AUTH command. Returns an error if a non-authenticated client executes a command other than Auth;
    •   If the server has the MaxMemory switch feature turned on, check the server's memory footprint and, if necessary, memory reclamation This allows the following commands to execute smoothly;
    •   If the server has an error executing the bgsave command one time, and the server has the Stop-writes-on-bgsave-error feature turned on, and the server is going to execute a write command, The server will then refuse to execute this command and return an error to the client;
    •   When the client is currently subscribing to a channel with the subscribe command, or is subscribing to the mode with the Psubscribe command, the server will only execute   The client sends the subscribe, Psubscribe, unsubscribe, punsubscribe four commands, the other commands will be rejected;          
    •   If the server is loading data, the command sent by the client must have a 1 ID (such as info, SHUTDOWN, PUBLISH)   will not be executed by the server, the other commands will be rejected by the server;
    •   If the server times out and goes into a blocking state because the Lua script is executing, the server will only execute the shutdown nosave command and the script Kill command sent by the client, and the other commands will be rejected by the server;
    •   If the client is executing a transaction, Then the server only executes the exec, DISCARD, MULTI, watch four commands that the client sends, and the other commands are put into the transaction queue;
    •   If the server has the monitor feature turned on, The server then sends information such as commands and parameters to the monitor that it will execute.
When the above preparatory operation is completed, the server can begin to actually execute the command.
5, invoke the command implementation function, Rediscommand has a proc property, it is a function pointer, by invoking it to perform the final command implementation.
6. Follow-up work:
    • If the server turns on slow log query logging, the slow query log module checks to see if a new slow query log needs to be added for the command request that has just been executed.
    • The milliseconds property of the rediscommand structure of the executed command is updated according to the time spent executing the command, and its calls counter value is increased by 1;
    • If the server turns on the AOF persistence feature, then the AOF persistence module writes the command request just executed to the AOF buffer;
    • If there are other servers replicating the current server from the server, then the server propagates the command just executed to all slave servers;
7, the command reply is sent to the client, the command implementation function will save the command reply to the client's output buffer, and the client socket associated with the command reply processor, when the client socket becomes writable state, the server will execute the command reply processor, Sends a reply to a command that is saved in the client buffer to a client. When the command has been sent, the reply processor empties the client output buffer. (In the doubt, if the client does not attempt to read the command reply, how does the server handle the client's output buffer?) Was it washed out in the process of the next command? )。

Servercron function

The server executes the Servercron function once every 100 milliseconds, typically, when the function is the only scheduled task of the Redis server, and its main responsibilities are as follows:
1, update the server time cache, Redisserver has Unixtime and mstime two properties, because 100 milliseconds to call once, so the time of the two properties are not accurate, if you want to obtain accurate system time, you must perform system call acquisition.
2, update the LRU clock, redisserver in the Lruclock attribute to save the server's LRU clock, it is also a server time cache, when the server to calculate the idle time of a database key, obtained by the lruclock of the server with the LRU property of Redisobject. Servercron defaults to update lruclock every 10 seconds, so it is also an estimate.
3, the update server executes the number of commands per second.
4, update the server memory peak, recorded in the Redisserver stat_peak_memory attribute.
5, processing sigterm signal, when starting the server, Redis will be the server process Sigterm signal associated processor Sigtermhandler function, the signal processor is responsible for the server to Sigterm signal, open the server state Shutdown_ ASAP identifies that each time the Servercron function is run, the program checks the Shutdown_asap property of the server state and determines whether to shut down the server based on the value of the property, and the server makes an RDB persistence operation before shutting down the server. This is why the server intercepts the sigterm signal.
6, the management of client resources, Servercron function each execution will call the Clientscron function, Clientscron function will be a certain number of clients to do two checks:

    • If the connection between the client and the server has timed out (no interaction for a long time), then the program frees the client and closes the connection.
    • If the size of the input buffer exceeds a certain length after the last execution of a command request, the program frees the client's current input buffer and re-creates a default-sized input buffer, preventing the client's input buffers from consuming too much memory.
7, manage the database resources, call the Databasecron function, check the server part of the database, delete the expiration key in it, and, if necessary, shrink the dictionary.
8, the execution of delayed bgrewriteaof, the server during the execution of Bgsave, if the client requests the BGREWRITEAOF command to the server, then the server will delay the BGREWRITEAOF command until the completion of bgsave execution, The aof_rewrite_scheduled record in Redisserver (a value of 1 indicates that a BGREWRITEAOF command has been delayed).
9. Check the running state of persistent operation, the server records the child process ID of Bgsave and bgrewriteaof command through Redisserver rdb_child_pid and Aof_child_pid property. This two property can also be used to check if there is currently a bgsave or bgrewriteaof command executing, each time the Servercron executes, the value of the two property is checked, as long as there is a value not 1, the program executes the WAIT3 function once, Check if the child process has a signal to send to the server:
    • If a signal is reached, indicating that the new Rdb file has been generated or that the AoF file rewrite is complete, the server needs to do the following, such as replacing the existing Rdb file with a new Rdb file, or replacing the existing AoF file with the rewritten aof file
    • If no signal arrives, then the persistence operation is not completed and no action is made.
If the value of Rdb_child_pid and aof_child_pid two attributes is-1, then the server is not persisting, in which case the program performs three checks:
    • Check to see if there are bgrewriteaof being delayed, and if so, execute a new bgrewriteaof.
    • Check to see if the auto-save condition is met, and if the server does not perform other persistence operations, the server starts a new bgsave.
    • Check to see if the AOF override condition is met, and if the server does not perform other persistence operations, execute a new bgrewriteaof command.
10. Writes the contents of the AOF buffer to the aof file, if aof persistence is turned on, and the aof buffer remains to be written, then Servercron calls the relevant function to write the contents of the AOF buffer to the aof file
11. Close the asynchronous client, and the server shuts down those clients whose output buffers are out of bounds.
12. Increase the value of the Cronloops counter, which records how many times the Servercron function was executed, and this property is used to "execute a specified code once per n Servercron function execution" function.

Redis design and implementation Learning notes-server

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.