2 <!DOCTYPE HTML PUBLIC
"-//W3C//DTD HTML 4.01//EN">
5 <link type=
"text/css" rel=
"stylesheet" href=
"style.css" />
12 <img style=
"border:none" alt=
"Redis Documentation" src=
"redis.png">
16 <div id=
"pagecontent">
18 <!-- This is a (PRE) block. Make sure it's left aligned or your toc title will be off. -->
19 <b>MultiExecCommand: Contents
</b><br> <a href=
"#MULTI">MULTI
</a><br> <a href=
"#COMMAND_1 ...">COMMAND_1 ...
</a><br> <a href=
"#COMMAND_2 ...">COMMAND_2 ...
</a><br> <a href=
"#COMMAND_N ...">COMMAND_N ...
</a><br> <a href=
"#EXEC or DISCARD">EXEC or DISCARD
</a><br> <a href=
"#Usage">Usage
</a><br> <a href=
"#The DISCARD command">The DISCARD command
</a><br> <a href=
"#Return value">Return value
</a>
22 <h1 class=
"wikiname">MultiExecCommand
</h1>
29 #sidebar
<a href=
"GenericCommandsSidebar.html">GenericCommandsSidebar
</a><h1><a name=
"MULTI">MULTI
</a></h1>
30 <h1><a name=
"COMMAND_1 ...">COMMAND_1 ...
</a></h1>
31 <h1><a name=
"COMMAND_2 ...">COMMAND_2 ...
</a></h1>
32 <h1><a name=
"COMMAND_N ...">COMMAND_N ...
</a></h1>
33 <h1><a name=
"EXEC or DISCARD">EXEC or DISCARD
</a></h1><blockquote>MULTI, EXEC and DISCARD commands are the fundation of Redis Transactions.A Redis Transaction allows to execute a group of Redis commands in a singlestep, with two important guarantees:
</blockquote>
34 <ul><li> All the commands in a transaction are serialized and executed sequentially. It can never happen that a request issued by another client is served
<b>in the middle
</b> of the execution of a Redis transaction. This guarantees that the commands are executed as a single atomic operation.
</li><li> Either all of the commands or none are processed. The EXEC command triggers the execution of all the commands in the transaction, so if a client loses the connection to the server in the context of a transaction before calling the MULTI command none of the operations are performed, instead if the EXEC command is called, all the operations are performed. An exception to this rule is when the Append Only File is enabled: every command that is part of a Redis transaction will log in the AOF as long as the operation is completed, so if the Redis server crashes or is killed by the system administrator in some hard way it is possible that only a partial number of operations are registered.
</li></ul>
35 <h2><a name=
"Usage">Usage
</a></h2><blockquote>A Redis transaction is entered using the MULTI command. The command alwaysreplies with OK. At this point the user can issue multiple commands. Insteadto execute this commands Redis will
"queue
" them. All the commands areexecuted once EXEC is called.
</blockquote>
36 <blockquote>Calling DISCARD instead will flush the transaction queue and will exitthe transaction.
</blockquote>
37 <blockquote>The following is an example using the Ruby client:
</blockquote><pre class=
"codeblock python" name=
"code">
40 >> r.incr
"foo
"
41 =
> "QUEUED
"
42 >> r.incr
"bar
"
43 =
> "QUEUED
"
44 >> r.incr
"bar
"
45 =
> "QUEUED
"
49 <blockquote>As it is possible to see from the session above, MULTI returns an
"array
" ofreplies, where every element is the reply of a single command in thetransaction, in the same order the commands were queued.
</blockquote>
50 <blockquote>When a Redis connection is in the context of a MULTI request, all the commandswill reply with a simple string
"QUEUED
" if they are correct from thepoint of view of the syntax and arity (number of arguments) of the commaand.Some command is still allowed to fail during execution time.
</blockquote>
51 <blockquote>This is more clear if at protocol level: in the following example one commandwill fail when executed even if the syntax is right:
</blockquote><pre class=
"codeblock python python" name=
"code">
53 Connected to localhost.
54 Escape character is '^]'.
65 -ERR Operation against a key holding the wrong kind of value
67 <blockquote>MULTI returned a two elements bulk reply in witch one of this is a +OKcode and one is a -ERR reply. It's up to the client lib to find a sensibleway to provide the error to the user.
</blockquote>
68 <blockquote>IMPORTANT: even when a command will raise an error, all the other commandsin the queue will be processed. Redis will NOT stop the processing ofcommands once an error is found.
</blockquote>
69 <blockquote>Another example, again using the write protocol with telnet, shows howsyntax errors are reported ASAP instead:
</blockquote><pre class=
"codeblock python python python" name=
"code">
73 -ERR wrong number of arguments for 'incr' command
75 <blockquote>This time due to the syntax error the
"bad
" INCR command is not queuedat all.
</blockquote>
76 <h2><a name=
"The DISCARD command">The DISCARD command
</a></h2><blockquote>DISCARD can be used in order to abort a transaction. No command will beexecuted, and the state of the client is again the normal one, outsideof a transaction. Example using the Ruby client:
</blockquote><pre class=
"codeblock python python python python" name=
"code">
77 ?
> r.set(
"foo
",
1)
81 >> r.incr(
"foo
")
82 =
> "QUEUED
"
85 >> r.get(
"foo
")
87 </pre><h2><a name=
"Return value">Return value
</a></h2><a href=
"ReplyTypes.html">Multi bulk reply
</a>, specifically:
<br/><br/><pre class=
"codeblock python python python python python" name=
"code">
88 The result of a MULTI/EXEC command is a multi bulk reply where every element is the return value of every command in the atomic transaction.