Redis Source Learning Redis Business NoSQL

Source: Internet
Author: User

Redis transactions provide a mechanism for packaging multiple command requests, and then executing multiple commands in sequence, and during the execution of a transaction, the server does not interrupt the transaction to perform other command requests that are not in the transaction, it will execute all the commands in the transaction before executing the other commands.

How

The multi, discard, exec, watch, unwatch commands are available in Redis to implement the functionality of the transaction.

The Redis transaction begins with the Multi command, followed by the command to execute in the transaction, finally the EXEC command, or the discard command. All commands in the join transaction are executed atomically, and the other commands that do not join the transaction are not interspersed in the middle.

Multi, exec, and discard

The multi command tells the Redis client to start a thing, and then Redis returns an OK, and then all of the command Redis will not execute immediately, returning only the queued result until the EXEC command is encountered to execute all previous commands. Or you encounter a discard command that discards the command to join the transaction before executing.

127.0.0.1:6379> Get Name

(nil)

127.0.0.1:6379> Get Gender

(nil)

127.0.0.1:6379> Multi

Ok

127.0.0.1:6379> Set name Slogen

QUEUED

127.0.0.1:6379> Set gender male

QUEUED

127.0.0.1:6379> exec

1) OK

2) OK

127.0.0.1:6379> Mget Name Gender

1) "Slogen"

2) "Male"

Watch

The watch command is an optimistic lock provided by Redis that can monitor any number of database keys before exec executes, and when the EXEC command executes, detects if at least one of the monitored keys has been modified, and if so, the server will refuse to execute the transaction. and returns an empty reply to the client that failed on behalf of the transaction.

First, execute the following command in CLIENT1:

127.0.0.1:6379> Get Name

(nil)

127.0.0.1:6379> Watch Name

Ok

127.0.0.1:6379> Multi

Ok

127.0.0.1:6379> Set name Slogen

QUEUED

127.0.0.1:6379> Set gender male

QUEUED

127.0.0.1:6379> Get Name

QUEUED

At this point the client has not executed the EXEC command, then under CLIENT2, execute the following command to modify name:

127.0.0.1:6379> Set name Rio

Ok

127.0.0.1:6379> Get Name

"Rio"

Next, execute the EXEC command under CLIENT1:

127.0.0.1:6379> exec

(nil)

127.0.0.1:6379> Get Name

"Rio"

As you can see from the execution results, when the EXEC command is executed in CLIENT1, Redis detects that the name field has been modified by another client, so it refuses to execute all the commands in the transaction and returns nil directly to indicate that the execution failed. The value of the name obtained at this time is also the Rio set in Client2.

Why

Multi

The Redis transaction begins with the Multi command, then it starts from the source code of the Multi command.

When Redis receives a command sent by the client, it executes the Multicommand () method, which is in the Multi.c file.

void Multicommand (client *c) {

1. If the flags are detected, the Client_multi is already contained.

Indicates that the corresponding client is already in the context of the transaction and returns an error

if (C->flags & Client_multi) {

Addreplyerror (c, "MULTI calls can not is nested");

Return

}

2. Open flags ' Client_multi logo

C->flags |= Client_multi;

3. Returns OK to tell the client that the transaction has been successfully opened

Addreply (C,shared.ok);

}

As you can see from the source code, Multicommand () mainly completes the following three things:

Detects if the client sending the Multi command is already in the transaction and returns an error directly if it is. As you can see from here, Redis does not support transaction nesting execution.

Adds a multi_client flag to the flags flag for the corresponding client, indicating that it has entered the transaction.

Returns OK to tell the client that the transaction has been successfully opened.

Redis Source Learning Redis Business NoSQL

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.