09-redis Transaction and lock application

Source: Internet
Author: User

Transactional Redis in Redis supports simple transaction Redis vs. mysql transactions-------------------------------------------------------MySQL Redis-------------------------------------------------------Open start transaction muitl---------------------------    ----------------------------Statement Plain SQL Common command-------------------------------------------------------failed Rollback rollback Discard Cancel-------------------------------------------------------successful commit exec- ------------------------------------------------------Note: The difference between rollback and discard if you have successfully executed 2 statements, the 3rd statement has an error. After the rollback, the first 2 statements affect the disappearance. Discard just end this transaction, the effect of the first 2 statements is still in note: In the statement after Mutil, there may be 2 case Error 1: syntax is a problem, this, exec, error, all statements are not executed 2: The syntax itself is correct, but the applicable object has a problem. For example, after the zadd operation of the list object exec, the correct statement is executed and an inappropriate statement is skipped. (If Zadd operations list how to avoid this thing, by the programmer responsible for) thinking: I am buying tickets Ticket-1, money-100 and only 1 tickets, if after I multi, and exec before, The ticket was bought by someone else---that ticket became 0. How can I observe this situation, and no longer submit pessimistic thoughts: the world is full of danger, someone and I must be robbed, to ticket lock, only I can operate. [Pessimistic lock] optimistic idea: not so people and I rob, therefore, I just need to pay attention,--there is no one to change the value of ticket can be [optimistic lock]redis in the transaction, Enabled is optimistic lock, only responsible for monitoring key has not been changed. Specific commands----Watch command example: Redis 127.0.0.1:6379> watch Ticketokredis 127.0.0.1:6379> Multiokredis 127.0.0.1:6379> decr ticketqueuedredis 127.0.0.1:6379> decrby money 100QUEUEDredis 127.0.0.1:6379 > exec (Nil)//return nil, indicating that the monitoring ticket has changed and the transaction is canceled. Redis 127.0.0.1:6379> get Ticket "0" Redis 127.0.0.1:6379> get Mone Y "x" Watch Key1 Key2 ... Keyn effect: Monitor Key1 Key2. Keyn There is no change, if there is a change, then the transaction cancels the Unwatch effect: cancel all watch monitoring

  

09-redis Transaction and lock application

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.