MYSQL transaction: the purpose is to ensure data integrity and security
redis-transaction: The purpose is to ensure the batch execution of redis statements
redis transaction:
During the execution of the transaction, Redis will not provide any services for requests from other clients
In a redis transaction, if a command fails to execute, the rest of the commands will still be executed
【Order】
1. multi : Open the transaction to mark the start of the transaction, and the commands executed after that will be stored in the command queue. These commands will only be executed by the courtyard when EXEC is executed, similar to the relational database: begin transaction (enable queue)
2. exec : commit a transaction, similar to that in relational databases: commit (execute batched statements)
3. discard : transaction rollback, similar to rollback in relational databases (do not execute batch statements)
Actual operation
Execute multi after emptying the database content
You can see that set username zhangsan entered the queue and was not executed
get username is still not executed and added to the queue
At this point the content of the queue is: (multi:)
set username zhangsan
get username
We enter exec to see that the commands are executed in sequence
The following commands test discard, multi to start batch transactions, one set and one get enter the queue, after executing discard, it is equivalent to these two statements are not executed directly
The following statement tests the redis transaction, and continues to execute in the case of a statement reporting an error
Since username is a string, incr username will definitely report an error
But we can see that the statement set username lisi below the error is executed
So it can be seen that the data of redis is not to ensure the security of data, but to ensure the batch execution of commands