Introduction to Redis Transactions

Source: Internet
Author: User
Tags redis

Transactions in Redis are essentially collections of a set of commands. All commands in a transaction are serialized and executed serially in sequence without being inserted by another command.
Redis supports transactions with the following five commands:
1. MULTI: Mark the beginning of a transaction block
2. EXEC: Execute the commands in all the transaction blocks
3. DISCARD: Discard the commands in all the transaction blocks
4. Watch: Monitors one or more keys, and if these keys are changed by other commands before the transaction is executed, the transaction is interrupted
5. Unwatch: Suppress watch command for all key monitoring

Example:
  
  
  
  
  
  
  
  
  
Now, when we add a command with a syntax error to the queue, exec will find that not only the wrong command will not execute, but the correct command will not execute:
  
  
  
However, when we add a command that is syntactically correct, but runs with errors, for example, to perform a self-increment operation on a value that is not a numeric type, we find that when Exec executes, only the error command will not execute, and the other commands will execute as usual:
  
  
  
So it can be said that Redis is part of the support transaction.
  
The Redis Watch command can monitor one or more keys, and once any of them has been modified, the subsequent transactions will not be executed, for example:
  
  
  
  
  
  
  
Note: Since the function of the Watch command is only when the monitored key has been modified and the transaction is canceled, there is no guarantee that other clients will not modify the value of the monitor, so when the EXEC command fails, the entire transaction needs to be manually re-executed:
  
  
  
After executing the EXEC command, the keys monitored with the watch command are deselected, and you can also suppress the monitoring by using the Unwatch command before executing the multi command.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.