redis combat -redis Affairs

redis combat -redis Affairs

  1. Description

 

   redis transaction isolation separate actions: all command sequence of a transaction are performed sequentially. Affairs in the implementation process, not other commands sent by the client's request interrupted.

 

  redis no transaction isolation level concept: queue command will not be executed before the actual not submitted because the transaction commits before any actual instruction will not be executed in the query does not exist "in the transaction sees affairs update, query outside a transaction can not be seen. "

  redis transaction does not guarantee atomicity: redis same transaction if there is a failure to perform a run command, the subsequent commands will still be executed, there is no rollback.

 

  

  2. Command

 

  Multi, Exec, Discard and Watch Redis commands related matters.
  Multi: mark the start of a transaction block. Multi command to enable a transaction, it always returns OK. After Multi execution, the client can continue to send to the server any number of commands that will not be executed immediately, but will be placed in a queue, when the Exec command is invoked, all queues commands will be executed. On the other hand, by calling Discard, the client can empty transaction queue, and give up enforcement branch.

  Exec: execute all commands within a transaction block. Reply command is an array, each element in the array reply transaction execution order generated. Among them, the same elements of the order and the order in reply command sent. When the client is in state affairs, all incoming command returns a reply content Queued state (status reply), these are queued commands will be executed when the Exec command is invoked.

  Discard: Cancel the transaction, giving up all the commands in a transaction block. When performing Discard command, the transaction will be abandoned, the transaction queue is cleared, and the client can withdraw from the transaction state.

  Watch: monitoring a number of Key or, if this or these other changes are Key command execution before the transaction, then the transaction will be interrupted.

  UnWatch: Cancel Watch command to monitor all of Key's.

 

 

  

  3. Example

  3.1. Examples of normal execution

  First, we empty the contents of the database, view the contents to see the database is empty. Multi then open a business, set up two Key, Value value, Exec enforcement branch. When we see the implementation of the transaction, and returns the results of two commands. By querying the database to save two normal data content.

   

  3.2. Example abandon Affairs

  Next, let us try to abandon the transaction example, first we empty the contents of the database, view the contents to see the database is empty. Multi then open a business, set up two Key, Value value at this time, we execute the command Discard to abandon the transaction. By looking at the database in the air, we can see that the database is empty.

  

  3.3 all guilt examples

  All guilt refers to what is it? That is where there is a compile-time error command, the entire series of commands will not be executed.

  

  3.4. Head creditors example of injustice

  Injustice head creditors means what is it? That is where there is a problem with running a command in the series of commands will be executed no problem, a problem of command will not be executed successfully.

  

  3.5. Watch monitoring

  Watch command can provide a check-and-set (CAS) acts as a Redis transaction. Watch the keys will be monitored, and whether they will find the key has been altered. If at least one of the monitored Exec key is modified before execution, then the entire transaction will be canceled, Exec returns nil-reply to indicate that the transaction has failed.

  Here we have a credit card account balances and debt as an example:

  First initial account balance of 100, the debt is zero. 20 consumption, account balances by 20, 20 increase in debt. First Watch to monitor account balances, and then open the transaction on the account balances and debt operations.

  When no stopper tampering, normal results of account balances 80, 20 debt.

   

  When the Watch to monitor account balances, account balances have Gasser tampering, such as top-100 to the account, the account balances changed to 180, and then execute a series of commands, executing a transaction, the result would be failure to properly update operation.

   

  

  4. Summary

  我们发现 Redis 对于事务,部分支持,不能像SQL Server等关系数据库的强一致性。Watch指令,类似乐观锁,事务提交时,如果Key的值已被别的客户端改变,比如某个List已被别的客户端Push/Pop过了,整个事务队列都不会被执行。通过Watch命令在事务执行之前监控多个Keys,倘若在Watch之后有任何Key的值发生了改变,Exec命令执行的事务都将被放弃,同时返回Nullmulti-bulk应答以通知调用者事务执行失败。

 

  至此Redis事务介绍完毕,有不当地方,欢迎指正!

 

Guess you like

Origin www.cnblogs.com/henxiao25/p/12640172.html