Redis transactions, pipelines

1.Redis transaction

1. Concept

Multiple commands can be executed at one time, which is essentially a collection of commands. All commands in a transaction will be serialized and executed serially in order without being inserted by other commands. No blocking is allowed.

2. The difference between Redis transactions and database transactions

3. Commonly used commands

4. Transaction execution status 

Normal execution 

That is, there is no abnormality in the entire process

MULTI //开启事务

命令 //入队,将多个命令入队到事务中,接到这些命令并不会立即执行而是放到等待执行的事务队列里面

......

EXEC //执行事务块内的命令

After starting a transaction, a series of commands entered will be submitted to a transaction queue in order . When EXEC is entered, all commands in the queue will be executed.

abandon transaction

MULTI 开启事务

......

DISCARD 放弃事务

Do not execute all

If a syntax error occurs during the command input stage , Redis will directly return an error and all commands will not be executed.

Correct execution, incorrect execution not

The syntax is correct when entering the command. There is a command error after EXEC is executed , but it will not be rolled back. The correct one will still be executed, and the incorrect one will not be executed.

watch monitoring 

Redis uses Watch to provide optimistic locking. After using Watch to monitor a Key, when submitting modifications, it will be detected whether the data has been changed during the period. If it has been changed, the execution will fail . Add some knowledge about pessimistic locking and optimistic locking:

  • Pessimistic lock : As the name suggests, it is very pessimistic. Every time you go to get the data, you think that others will modify it, so you will lock it every time you get the data. In this way, if others want to get the data, they will block until they get the lock.
    • Advantages: Simple and reliable, suitable for scenarios with frequent write operations. Pessimistic locking can effectively handle concurrent write operations and ensure the correctness of data.
    • Disadvantages: Impact on system performance. Due to the need for locking and unlocking operations, additional overhead will be incurred and the waiting time of other transactions may increase.
  • Optimistic lock : As the name suggests, it is very optimistic. Every time you go to get the data, you think that others will not modify it, so you will not lock it. However, when updating, you will judge whether others have updated the data during this period. The commit version must be greater than the current version of record to perform an update
    • Advantages: Suitable for scenarios with more reading and less writing. Optimistic locking can improve concurrency performance when there are many concurrent read operations and few write operations.
    • Disadvantages: Requires conflict handling and concurrency safety. Since there is no lock, the occurrence of conflicts may cause some operations to fail and require retry or rollback.

test:

Once EXEC is executed, the monitoring locks added before will be canceled. 

2.Redis pipeline

1.Introduction

Redis is a TCP service based on the client-server model and request/response protocol. A request follows these steps:

  1. The client sends commands to the server in four steps (send command → command queue → command execution → return result), and listens for Socket return, usually in blocking mode waiting for the server to respond.
  2. The server processes the command and returns the results to the client. The above two steps are called: Round Trip Time (RTT for short, the time it takes for a data packet to travel to and from both ends)

If you need to execute a large number of commands at the same time, you have to wait for the response of the previous command before executing it. Not only does this add RTT (Round Time Trip), but it also frequently calls system IO and sends network requests, and requires multiple redis calls. read() and write() system methods. The system methods will transfer data from user mode to kernel mode, which will have a greater impact on the process context and the performance will not be very good.

2. Concept

The pipeline can send multiple commands to the server at one time. After the server completes the processing, it returns the results at once through a response. This reduces the round-trip delay time by reducing the number of communications between the client and redis. The principle of pipeline implementation is queue, and the first-in-first-out feature ensures the order of data.

3. The difference between Pipeline and mset/mget native commands

  • The mset and mget commands are atomic; the pipeline is non-atomic. If an exception occurs during the execution of the instruction, subsequent instructions will continue to be executed.
  • The mset and mget batch commands can only execute one command at a time; the pipeline supports batch execution of different commands.
  • mset and mget are implemented on the server side; pipeline needs to be completed by both the server and the client.

4.The difference between Pipeline and transaction

  • Transactions are atomic, pipes are not atomic
  • The pipeline sends multiple commands to the server at one time. Transactions are sent one by one. The transaction will only be executed after receiving the exec command.

Guess you like

Origin blog.csdn.net/qq_62767608/article/details/132051079