Analysis of redis event processing process
Foreword:
We know that the redis server is an event-driven program, which needs to handle two types of events, namely file events and time events.
So what is the redis event processing process like? Let’s take a look at the picture below:
The aeMain function schedules and executes file events and time events by calling the aeProcessEvents function. Event-related information is recorded in aeEventLoop. First, obtain the execution time interval n of the shortest time event through the aeSearchNearestTimer function, then call the aeApiPoll function to obtain the monitored socket, and finally execute the event processing functions rfileProc and wfileProc corresponding to the socket, and finally execute the time event function processTimeEvents
File Event
Redis has developed its own network event processor based on the Reactor model. This processor is called a file event handler:
The file event processor uses an IO multiplexer to listen to multiple sockets and associate different event processors with the socket according to the tasks currently performed by the socket
When the monitored socket is ready to perform operations such as connection response (accept), read (read), write (write), close (close), etc., when a file event occurs, these file events The processor will call the event handler previously associated with the socket to process the event
The composition of the file event handler
(Learning video sharing: redis video tutorial)
Handler of file events
Redis has written multiple handlers for file events:
Connection response processor: When the Redis server is initialized, the program will associate this connection response processor with the AE_READABLE event of the service listening suite. When a client uses the connect function to connect to the server listening socket Yes, the socket will generate the AE_READABLE event, trigger the connection response processor to execute, and perform the corresponding socket response operation
Command request processor: When a client connects After the response processor successfully connects to the server, the server will associate the AE_READABLE event of the client socket with the command request processor. When the client sends a command request to the server, the socket will generate an AE_READABLE event and trigger the command request. The processor executes and performs the corresponding socket read operation
Command reply processor: When the server has a command reply that needs to be passed to the client, the server will socket the client The AE_WRITABLE event of the interface is associated with the command reply processor. When the client is ready to receive the command reply from the server, the AE_WRITABLE event will be generated, triggering the execution of the command reply processor and performing the corresponding socket write operation.
A complete client-server connection event
The server listens to the AE_READABLE event of the package word, and generates AE_READABLE when the client sends a connection request event, the server will respond to the client's connection request, associate the AE_READABLE event of the client socket with the command request processor, and the client can send a command request to the server
The client sends a command request to the server. The client socket will generate an AE_READABLE event, triggering the command processor to execute. Executing the command will generate a corresponding command reply. The server will combine the AE_WRITABLE event of the client socket with the command. Reply processor association
When the client attempts to read the command reply, the client socket will generate an AE_WRITABLE event, triggering the execution of the command reply processor. When the command reply processor replies to the command After all is written to the socket, the server will contact the association between the AE_WRITABLE event of the client socket and the command reply handler
Time event
Redis Time events are divided into timed events and periodic events. A time event consists of three attributes:
id: the globally unique ID created by the server for the time and time when: the arrival time of the time and time is recorded (UNIX timestamp with millisecond precision) timeProc: time event processor
All time events of the server are placed in an unordered linked list. Whenever the time event executor runs, it traverses the entire linked list, finds all arriving time events, and calls the corresponding event handler. The Redis server in normal mode only uses one time event, serverCron, and in benchmark mode, the server only uses two time events, so the performance of event execution is not affected.
Related recommendations: redis database tutorial
The above is the detailed content of Analysis of redis event processing process. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Redis cluster mode deploys Redis instances to multiple servers through sharding, improving scalability and availability. The construction steps are as follows: Create odd Redis instances with different ports; Create 3 sentinel instances, monitor Redis instances and failover; configure sentinel configuration files, add monitoring Redis instance information and failover settings; configure Redis instance configuration files, enable cluster mode and specify the cluster information file path; create nodes.conf file, containing information of each Redis instance; start the cluster, execute the create command to create a cluster and specify the number of replicas; log in to the cluster to execute the CLUSTER INFO command to verify the cluster status; make

How to clear Redis data: Use the FLUSHALL command to clear all key values. Use the FLUSHDB command to clear the key value of the currently selected database. Use SELECT to switch databases, and then use FLUSHDB to clear multiple databases. Use the DEL command to delete a specific key. Use the redis-cli tool to clear the data.

To read a queue from Redis, you need to get the queue name, read the elements using the LPOP command, and process the empty queue. The specific steps are as follows: Get the queue name: name it with the prefix of "queue:" such as "queue:my-queue". Use the LPOP command: Eject the element from the head of the queue and return its value, such as LPOP queue:my-queue. Processing empty queues: If the queue is empty, LPOP returns nil, and you can check whether the queue exists before reading the element.

Using the Redis directive requires the following steps: Open the Redis client. Enter the command (verb key value). Provides the required parameters (varies from instruction to instruction). Press Enter to execute the command. Redis returns a response indicating the result of the operation (usually OK or -ERR).

Using Redis to lock operations requires obtaining the lock through the SETNX command, and then using the EXPIRE command to set the expiration time. The specific steps are: (1) Use the SETNX command to try to set a key-value pair; (2) Use the EXPIRE command to set the expiration time for the lock; (3) Use the DEL command to delete the lock when the lock is no longer needed.

On CentOS systems, you can limit the execution time of Lua scripts by modifying Redis configuration files or using Redis commands to prevent malicious scripts from consuming too much resources. Method 1: Modify the Redis configuration file and locate the Redis configuration file: The Redis configuration file is usually located in /etc/redis/redis.conf. Edit configuration file: Open the configuration file using a text editor (such as vi or nano): sudovi/etc/redis/redis.conf Set the Lua script execution time limit: Add or modify the following lines in the configuration file to set the maximum execution time of the Lua script (unit: milliseconds)

Use the Redis command line tool (redis-cli) to manage and operate Redis through the following steps: Connect to the server, specify the address and port. Send commands to the server using the command name and parameters. Use the HELP command to view help information for a specific command. Use the QUIT command to exit the command line tool.

In Debian systems, readdir system calls are used to read directory contents. If its performance is not good, try the following optimization strategy: Simplify the number of directory files: Split large directories into multiple small directories as much as possible, reducing the number of items processed per readdir call. Enable directory content caching: build a cache mechanism, update the cache regularly or when directory content changes, and reduce frequent calls to readdir. Memory caches (such as Memcached or Redis) or local caches (such as files or databases) can be considered. Adopt efficient data structure: If you implement directory traversal by yourself, select more efficient data structures (such as hash tables instead of linear search) to store and access directory information
