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