[Redis study notes (14)] publish and subscribe, transactions, Lua scripts, slow query logs

This article is published by the official account [Developing Pigeon]! Welcome to follow! ! !


Old Rules-Sister Town House:

One. Publish and Subscribe

(I. Overview

       Redis publish and subscribe functions are composed of PUBLISH, SUBSCRIBE, PSUBSCRIBE and other commands. Through the subscription command, the client can subscribe to one or more channels. Whenever another client sends a message to the subscribed channel, all subscribers of the channel will receive the message. In addition to subscribing to the channel, the client can also subscribe to the mode through the PSUBSCRIBE command. The mode is matched to the channel according to the pattern matching rules. As long as a message is sent to the channel, subscribers of the pattern matching the channel can receive the message.

(2) Channel subscription and unsubscription

       Redis saves the subscription relationship of all channels in the pubsub_channels dictionary in the server state. The key of this dictionary is a subscribed channel, and the value of the key is a linked list that stores all the clients subscribed to this channel.

(3) Subscription and unsubscription of the model

       Similarly, the subscription relationship of the pattern is also stored in the pubsub_Patterns linked list of the server state. Each node is a pubsub_Pattern structure, which stores the subscribed pattern and the clients that subscribe to the pattern.

(4) Send message

       When a Redis client executes a PUBLISH <channel> <message>command to send a message to the channel channel, the server will send the message to all subscribers of the channel, and if there are multiple patterns matching the channel, the subscribers of the pattern will also receive the message.

(5) View subscription news

       The PUBSUB command is a new command in Redis 2.8. The client can use this command to view channel or mode information, such as the number of subscribers.

1. PUBSUB CHANNELS [pattern]

       Return all channels that the server is currently subscribed to. If a pattern parameter is added, the condition of matching the pattern pattern must be met.


2. PUBSUB NUMSUB

       Accepts any number of channels as input parameters and returns the number of subscribers to these channels.

3. PUBSUB NUMPAT

       Returns the number of currently subscribed modes of the server.


two. Affairs

(I. Overview

       Redis implements transactions through MULTI, EXEC, WATCH, and other commands. The transaction packages one or more command requests, and then executes these commands in order.

(Two) the realization of affairs

1. The transaction begins

       The MULTI command marks the beginning of a transaction. This command can switch the client of the command from a non-transactional state to a transactional state. This is done by turning on the REDIS_MULTI flag in the flags attribute of the client state.

2. Order to join the team

       When a client is in a non-transactional state, the command sent by the client will be executed immediately; and after the client turns into a transactional state, if the command sent by the client is one of EXEC, DISCARD, WATCH, MULTI, Then the server executes this command immediately, otherwise it will not execute it, but puts the command into a queue and returns a QUEUED reply to the client.

3. Transaction Queue

       Each Redis client has its own transaction status. The transaction status includes a transaction queue and counter. The queue stores the function pointers, parameters, and the number of parameters of the queued commands.

4. Execution of business

       When a client in a transaction state sends an EXEC command to the server, it will be executed immediately. The server will traverse the client's transaction queue, execute all the commands stored in the queue, and return the execution results to the client.

(Three) WATCH order

1 Overview

       The WATCH command is an optimistic lock. It can monitor any number of database keys before the EXEC command is executed, and when the EXEC command is executed, it checks whether at least one of the monitored keys has been modified, and if it is, reject it. Execute the transaction and return an empty reply representing the failure of the transaction to the client, which ensures the security of the transaction.

2. Realization

       Each Redis database stores a watched_keys dictionary. The key of this dictionary is a database key monitored by the WATCH command, and the value of the dictionary is a linked list that records all the clients that monitor the database key. Through this dictionary, the server can know which database keys are being monitored and which clients are monitoring these database keys.

3. Triggering of the monitoring mechanism

       All commands that modify the database will call a function to check the watched_keys dictionary after execution to see if there is a client that is monitoring the database key that has just been modified by the command. If there is, it will monitor all the database keys. The REDIS_DIRTY_CAS flag of the client is turned on, which indicates that the transaction security of the client has been breached.

4. Determine whether the transaction is executable

       When the server receives an EXEC command from a client, the server decides whether to execute the transaction according to the REDIS_DIRTY_CAS identifier of the client.

(4) ACID of the transaction

1. Atomicity

       Multiple operations in a transaction are executed as a whole, but Redis does not support transaction rollback. Even if an error occurs during the execution of a command, the entire transaction will continue to be executed until all commands are executed.

2. Consistency

       Transaction consistency means that if the database is consistent before the transaction is executed, then after the transaction is executed, no matter whether the transaction will be executed successfully, the database should also be consistent. Consistency means that the data conforms to the definition and requirements of the database itself, and does not contain illegal or invalid error data. Therefore, Redis ensures transaction consistency through error detection and simple design.

(1) Enrollment error

       If a transaction does not exist or the command format is incorrect in the enqueue command, the transaction is refused to be executed, so the consistency will not be affected.

(2) Execution error

       An error occurs during the execution of the transaction, and the server will not interrupt the execution of the transaction, so the consistency will not be affected.

(3) The server is down

       If the Redis server is down during the execution of a transaction, judge according to the persistence mode used by the server:

       If there is no persistence, the database will be blank after restart, so it must be consistent;

       If there is RDB persistence, use RDB to restore data, which must be consistent;

       If there is AOF persistence, use AOF to restore the data, which must be consistent;


3. Isolation

       Redis uses a single thread to execute transactions and is isolated.

4. Durability

       According to the persistence mode of Redis, for example, in RDB mode, because the BGSAVE command is executed asynchronously, transaction data cannot be guaranteed to be saved to the hard disk in the first time; in AOF mode, the durability is determined according to the value of appendfsync option.

       Note that you can also add a SAVE command at the end of each transaction to ensure the durability of the transaction, but the efficiency is not high.


three. Lua script

(I. Overview

       Redis introduced support for Lua scripts from version 2.6. By embedding the Lua environment in the server, the Redis client can use Lua scripts to directly execute multiple Redis commands on the server side atomically.

(Two) Lua environment collaboration components

1. Pseudo client

       Because the execution of Redis commands must have a corresponding client status, in order to execute the Redis commands contained in the Lua script, the Redis server creates a pseudo client specifically for the Lua environment, which is responsible for processing the Redis commands. The Lua script uses the redis.call function to execute a Redis command and sends it to the pseudo client. The pseudo client passes the command to the command executor, and then returns the result to the Lua environment.

2. lua_scripts dictionary

       The key of this dictionary is the SHA1 checksum of a Lua script, and the value of the dictionary is the Lua script corresponding to the SHA1 checksum. Redis will load all Lua scripts that have been executed by the EVAL command and all the scripts that have been executed by the SCRIPT LOAD command. The imported Lua scripts are saved in this dictionary.

(Three) EVAL command

       According to the Lua script executed by the client, define a Lua function corresponding to the script in the Lua environment. The name of the Lua function is composed of the f_ prefix plus the SHA1 checksum of the script, and then save the given script to the lua_scripts dictionary , And finally execute the function just defined.


(4) EVALSHA command

       This command calls the function corresponding to the script according to the SHA1 checksum of the script. The requirement is that the script must have been executed by the EVAL command or loaded by the SCRIPT LOAD command.


(5) Script management commands

1. SCRIPT FLUSH

       Clear all information related to Lua scripts in the server, release and rebuild the lua_scripts dictionary, and rebuild the new Lua environment.


2. SCRIPT EXISTS

       According to the input SHA1 checksum, check whether the corresponding script is in the server.


3. SCRIPT LOAD

       Load the script command. The command first creates the corresponding function for the script in the Lua environment, and then saves the script to the lua_scripts dictionary, but does not execute it.


four. Slow query log

(I. Overview

       Redis's slow query log is used to record command requests whose execution time exceeds a given period of time. Users can monitor and optimize the query speed through the log generated by this function.


(2) Arrangement

       The server configuration has two configurations related to slow query:


1. slowlog-log-slower-than

       The number of subtle command requests that execute the execution event will be recorded in the log.


2. slowlog-max-len

       Execute the maximum number of slow query logs saved by the server, and use FIFO to save the logs.

(3) Check and delete

1. SLOWLOG GET

       Check the slow query log.

2. SLOWLOG LEN

       View the number of logs.


3. SLOWLOG RESET

       Clear all slow query logs.

Guess you like

Origin blog.csdn.net/Mrwxxxx/article/details/114412044