introduce
  • Allows the user to wrap multiple commands and then execute all of the wrapped commands in sequence at once
  • During the execution of a transaction, the server will not interrupt the transaction to execute other command requests. Only after all the commands of the transaction are executed, the server will process other command requests
  • Execute multiple commands at once and ensure that either all or none of the commands in the transaction are executed
Transaction command
  • MULTIStart a new transaction After this command is executed, all commands sent by the client for the database or database keys are not executed immediately. Instead, they are placed in a transaction queue and QUEUED is returned to indicate that the command is QUEUED
    127.0.0.1:6379> multi 
    OK
    127.0.0.1:6379> set msg "hello world"
    QUEUED
    127.0.0.1:6379> expire msg 600
    QUEUED
    Copy the code
  • DISCARDCancels the transaction, abandoning all commands in the transaction queue
    127.0.0.1:6379> discard
    OK
    127.0.0.1:6379> set msg "hello world"
    OK
    Copy the code
  • EXECExecute all commands in the transaction queue in the order in which they are enqueued to the transaction queue. The return value of a command is a list of all the commands that are executed in the transaction queue
    127.0.0.1:6379> multi 
    OK
    127.0.0.1:6379>  set msg "hello world"
    QUEUED
    127.0.0.1:6379>  expire msg 600
    QUEUED
    127.0.0.1:6379> exec
    1) OK
    2) (integer) 1
    Copy the code

    Transaction error:

    127.0.0.1:6379> multi 
    OK
    127.0.0.1:6379>  set msg "hello world"
    QUEUED
    127.0.0.1:6379>  expire msg 1x
    QUEUED
    127.0.0.1:6379> exec
    (error) EXECABORT Transaction discarded because of previous errors.
    127.0.0.1:6379> get msg
    (nil)
    Copy the code
The difference between pipeline and transaction
function performance
Assembly line Make sure multiple commands are sent together
The transaction Ensure that multiple commands are executed together