A transaction is a set of commands

Run the multi command to inform the server

My next set of commands are bundled together and must be executed or not executed (multi-exec).

As you can see, no matter what the command is, until exec is encountered, queued is returned, which means that Redis has queued the set of commands first and does not execute them immediately. The commands in the queue are executed only after exec is received.

You can verify that

time The client 1 Client 2
T1 multi
T2 set age 1
T3 get age
T4 get age
T5 exec
T6 get age



When will a transaction not be executed?

Before starting a transaction, watch a key. If the key is changed by another client before the transaction is executed, the transaction will not be executed.

Same command as above, but watch age first

conclusion

  • Start the -execute transaction through the multi-exec command group
  • The set of commands that make up a transaction are first queued and executed sequentially after the exec command
  • If watch has a key and the key is changed by another client, the transaction will not be executed