In redis, the code about the event-driven framework is concentrated in AE. hae. c. the author also sets the introduction in the header: asimpleevent-drivenprogramminglibrary. this framework is actually very simple. The core is a message queue, and only one thread is responsible for processing it. The scheduling idea here is a simple priority queue, and file operations are preferred.
In redis, the code about the event-driven framework is concentrated in AE. h/AE. c. the author also sets the introduction in the header: a simple event-driven programming library. this framework is actually very simple. The core is a message queue, and only one thread is responsible for processing it. The scheduling idea here is a simple priority queue, and file operations are preferred.
In redis, the code about the event-driven framework is concentrated in AE. h/AE. c. the author also sets the introduction in the header: a simple event-driven programming library. this framework is actually very simple. The core is a message queue, and there is only one thread responsible for processing it. The scheduling idea here is a simple priority queue. The file operation Priority is always higher than the time operation. tasks are not preemptible. for details about the execution process, refer to the following figure: "time -------... <---- 10 MS ----> <---- 10 MS ----> <---- 10 MS ----> <---- 10 MS ----> <-------- 15 MS --------> <------- 12 MS -------> <= 0, it indicates that at least one time event has arrived # If the value is greater than 0, it indicates that no time event has arrived at nearest_te_remaind_ms = t E. when-now_in_ms () if nearest_te_remaind_ms <= 0: # if a time event has arrived # call the non-blocking file event wait function poll (timeout = None) else: # If the time event has not arrived # The maximum blocking time cannot exceed the te arrival time poll (timeout = nearest_te_remaind_ms) # process the ready file event process_file_events () # process the arrival time event process_time_event () "so far, the event part ends. then there is the communication protocol. redis uses non-blocking sockets, so it can process multiple client requests with a single thread. the Protocol for communications between the server and the client is simple enough. for example. /redis-cli communication, when you input get mike, the transmitted data is $3 \ r \ nmike \ r \ n. The first 3 indicates that only one row of data exists. Therefore, in the officially recommended java driver jedis, jedis only enables users to splice strings manually from the original to interfaces. At the same time, encapsulate some exceptions. that's all. the following is the execution content of ** set mike "**: + assume that C1 is now connected to server S. When the command SET mike is executed, the client Concatenates the string: "* 3 \ r \ n $3 \ r \ nSET \ r \ n $4 \ r \ nmike \ r \ n $4 \ r \ nmike \ r \ n ", at the same time, it is written into the socket connected to the server. + When the file event processor of S executes, it will notice that the read event corresponding to C1 is ready, so it reads the Protocol text and queries the cache. + By analyzing the query cache (parse), the server finds the command implementation function corresponding to the SET string in the Command table, and finally locates the t_string.c/setCommand function. In addition, the two command parameters mike and mike will also be saved in the client structure as strings. + Then, the program sends the client, the command to be executed, and the command parameters to the command executor: The executor calls the setCommand function and changes the value of the mike key in the database to mike, then, save the command execution result in the client's reply cache and associate the write event with the client fd to send the result back to the client. + Because of the modification of the mike key, other programs related to the database namespace, such as AOF, REPLICATION, and transaction security checks (have you modified the keys monitored by WATCH ?) It will also be triggered. After these subsequent programs are executed, the command executor will exit and other programs (such as the time event processor) on the server will continue to run. + When the write event corresponding to C1 is ready, the program will write the data stored in the client structure reply cache back to the client. After the client receives the data, it will print the result. Because it is simple enough, we can write arbitrary driver according to the protocol. at the same time, the redis-proxy project is displayed on the internal network. It is set that the PHP client will frequently create redis connections to obtain data. At the same time, because php does not have persistent connections (I understand it now), it is a waste of resources to create and release a large number of connections. Therefore, netty is used as a middleware, maintain persistent connections to redis through java, connect to the connection pool, and connect to the php end by inheriting the high performance of netty to improve the response speed. because the document does not describe the use scenario of this proxy, my understanding should be placed on every php server, if it is only placed on the redis server, this so-called proxy doesn't really mean much. The biggest optimization is to change a network call to a local loop. improves the response speed. combined with the [mogujie.com Deployment Scheme] ( http://gitlab.mogujie.org/dragon/newman/blob/master/Chapter10.md ). We can see that nginx is used to handle a large number of short and concentrated requests. because both parties are single-threaded models, the biggest bottleneck lies in the blocking of big data transmission. at the same time, it cannot reflect the advantages of multi-core CPU. the latter can reflect the multi-core advantages through multiple instances (mogujie.com currently uses the Instance name when requesting cache resources, and it feels that a redis instance is specified). For the former, you must note that the cached content should not be too large During encoding. if needed, you can use string compression to shorten the character length. so far. read the full text>