Redis Advanced Step

Source: Internet
Author: User

I. Transactions in Redis

In relational database transactions are essential to a core function, life is also visible everywhere, for example, we go to bank transfer, the first need to be a account of the money, and then deposited to the B account, the two steps must be in the same transaction, or all executed, or do not execute, otherwise the money disappeared in thin air, for who also can not accept.

In the same way, Redis also provides us with a transaction, in which a set of commands in the same transaction is sent to Redis, and then Redis executes sequentially.

1, the syntax of the transaction:

Multi Command 1 command 2 ... exec

Explain the following syntax: First tell Redis through the Multi command: "The command I sent you below is the same thing, you don't do it, you can store it first." Redis replied: "Okay." Then we send the bank Transfer command 1 and command 2, then Redis will comply with the contract will not execute the command, but instead return queued, indicating that the two commands are stored in the queue waiting to execute the transaction. Finally we send the EXEC command, and Redis begins to execute the command in the wait queue in turn, completing the operation of a transaction.

Redis guarantees that all commands in a transaction are either fully executed or not executed. If the client is disconnected before the exec is sent, Redis empties the waiting transaction queue, and all commands are not executed. Once exec is sent, it doesn't matter if the client is disconnected even during execution, because Redis already stores the commands.

Below we simulate the business of bank transfer, from bank a transfer 5000 to bank B, in the middle of the attempt to modify the balance of bank A, then see if the transfer is successful and guarantee the correct amount?

    • First, banks A and B were initialized to 10000 yuan respectively.
127.0.0.1:6379> SetBanka10000OK127.0.0.1:6379>get Banka "10000"127.0.0.1:6379> Setbankb10000OK127.0.0.1:6379>Get bankb "10000"
    • To transfer business operations:
127.0.0.1:6379>Multiok127.0.0.1:6379>Decrby banka theQUEUED127.0.0.1:6379>Incrby bankb theQUEUED127.0.0.1:6379> 
    • Reopen a session, simulate the bank A to modify the balance, such as 10000 yuan, then the money was taken, the balance of 0
127.0. 0.1:6379>10000(integer0127.0.  0.1:6379>  get Banka "0"
    • Perform a transaction in the first session
127.0. 0.1:6379>exec1) (integer-   # This assumes that the balance can be overdrawn, haha. Become ﹣5000, instead of the original thought of 5000, if the actual business, then it is impossible to transfer. The transaction guarantees that the balance is correct 2) (integer15000

2. Transaction error Handling

What does Redis do if a command goes wrong while executing a transaction? In Redis, you need to analyze the cause of the command error, and different reasons will have different ways of handling it.

1) syntax error

A syntax error means that the command does not exist or the number of arguments is incorrect. for this type of error, the Redis (version after 2.6.5 ) is handled with a direct return error, all without execution, even if there is a correct command in it.

127.0.0.1:6379>Multiok127.0.0.1:6379> Set Keyvaluequeued127.0.0.1:6379> Set Key(Error) ERR wrong Number  ofArguments for 'Set'Command127.0.0.1:6379>IiigetKey(Error) ERR unknown command'Iiiget'127.0.0.1:6379> exec(Error) ExecabortTransactionDiscarded because ofprevious errors.127.0.0.1:6379>GetKey(Nil) # There is a syntax error in the transaction, even if a command is correct and will not be executed 127.0.0.1:6379> 

2) Run Error

A run-time error is when the command executes, such as using the hash command to manipulate the key of the collection type. This type of error is not discovered by Redis before it is run, so if there is such a bad command in the transaction, the other correct commands will still be executed, even after the error command. Care should be taken to avoid such errors.

127.0.0.1:6379>Multiok127.0.0.1:6379> Set Key 1QUEUED127.0.0.1:6379>SaddKey 2QUEUED127.0.0.1:6379> Set Key 3QUEUED127.0.0.1:6379> exec1) OK2) (error) Wrongtype operation against aKeyHolding the wrong kind ofvalue3) OK127.0.0.1:6379>GetKey"3"

Sadd Key 2 is visible, but set key 3 is still executed.

Transactions in Redis do not have a rollback mechanism like a relational database, and for that matter, developers have to clean up the mess themselves.

In order to ensure that there is no command and data type mismatch, the pre-planning database (such as the key name specification) is particularly important.

3. Watch command

Watch command can monitor one and more keys, once the value of the monitored key is modified, block after the execution of a transaction (that is, when Exec returns nil, but then watch monitoring will also expire), remember the above transfer? When in the transfer transaction process, Banka was taken away 10000, the balance becomes 0, then the operation of the transfer should prompt the balance is insufficient, cannot transfer. You can use the Watch command to block the execution of a transfer transaction. Below to optimize the above transfer business:

127.0. 0.1:6379>  watch Banka  #监控银行A账号OK
127.0.0.1:6379> Decrby Banka 10000
(integer) 0
127.0. 0.1:6379> Multi
OK127.0.0.1:6379>Decrby banka theQUEUED127.0.0.1:6379>Incrby bankb theQUEUED127.0.0.1:6379> exec(nil)127.0.0.1:6379>get Banka "0"127.0.0.1:6379>Get bankb "10000"127.0.0.1:6379>

Second, the survival time

Redis Advanced Step

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.