Redis transaction with additional features of redis

Source: Internet
Author: User

I. Transactions

Redis's transaction function allows you to Package Multiple commands and then execute all the commands in one order. During the transaction execution process, the server does not interrupt the transaction and changes to execute other command requests. Only when all the commands in the transaction package are
After the command is executed, the server will process other command requests.

Transaction example:

Now, let's assume that the setex command does not exist in redis, And the SET command does not support the ex seconds parameter. If we want to implement a setex command by ourselves, we may use the following code:

def SETEX(key, seconds, value):SET key valueEXPIRE key seconds

In general, this self-made setex command can achieve the effect of setting the key-Value Pair and setting the production time, but this self-made setex has a defect: if the server crashes after the set command is successfully executed and the data is saved, the expire command cannot be executed.

ThenAlthough we have set a key, we have not set an expiration time for the key.If we do not find out, the key that should have been regularly deleted will remain in the database and occupy memory, and even cause subsequent program errors.

 

II,Transaction command

To avoid the above situation, we need to use the redis transaction function. Through transactions, we canLet redis execute multiple commands at a time, and make sure that the commands in the transaction are either executed in all or none of them..

Multi: Start a new transaction.

Discard: discard the transaction

Exec: Execute all commands in the transaction

 

The transaction commands are described as follows:

Multi

Start a transaction.
After this command is executed, all the commands sent by the client for the database or database key will not be executed immediately, but will be put into a transaction queue, and the return of queued indicates that the command is in the queue.

Redis> multi # Start a transaction okredis> set MSG "Hello World" # Put This set command into the transaction queue queuedredis> expire MSG 10086 # Put This set command into the transaction queue queued

 

Discard

Cancel the transaction and discard all commands in the transaction queue.

Redis> multiokredis> set MSG "Hello World" queuedredis> expire MSG 10086 queuedredis> discard # The transaction has been canceled okredis> set MSG "Hello World" OK

 

Exec

Execute the transaction. Execute all the commands in the transaction queue according to the order in which the commands are queued to the transaction queue. The return value of a command is a list that contains the return values of all executed commands in the transaction queue.

Redis> multiokredis> set MSG "Hello World" queuedredis> expire MSG 10086 queuedredis> exec1) OK # Return Value of the SET command 2) (integer) 1 # Return Value of the expire command

 

Use transactions to ensure operation security

Previously defined custom setex with security defects:

Def setex (Key, seconds, value): set key valueexpire key seconds # If the server crashes after the set command is executed, expire cannot be executed

The custom setex definition implemented by transactions has no security defects. The server ensures that either two commands are executed, or both commands are not executed:

def SETEX(key, seconds, value):MULTISET key valueEXPIRE key secondsEXEC

 

Iii. Differences between pipelines and transactions

Assembly line: ensure that multiple commands are combinedSend;

Affairs: Make sure that multiple commands are togetherRun;

(Pipeline_start) set MSG "Hello World" # these two commands will be sent together to the server expire MSG 10086 (pipeline_end)
Multiset MSG "Hello World" # these two commands will be executed by the server together by expire MSG 10086 Exec

 

Redis transaction with additional features of redis

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.