]> git.saurik.com Git - redis.git/blame - doc/MultiExecCommand.html
Consistency test improved
[redis.git] / doc / MultiExecCommand.html
CommitLineData
aed57a31 1
2<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
3<html>
4 <head>
5 <link type="text/css" rel="stylesheet" href="style.css" />
6 </head>
7 <body>
8 <div id="page">
9
10 <div id='header'>
11 <a href="index.html">
12 <img style="border:none" alt="Redis Documentation" src="redis.png">
13 </a>
14 </div>
15
16 <div id="pagecontent">
17 <div class="index">
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>&nbsp;&nbsp;<a href="#MULTI">MULTI</a><br>&nbsp;&nbsp;<a href="#COMMAND_1 ...">COMMAND_1 ...</a><br>&nbsp;&nbsp;<a href="#COMMAND_2 ...">COMMAND_2 ...</a><br>&nbsp;&nbsp;<a href="#COMMAND_N ...">COMMAND_N ...</a><br>&nbsp;&nbsp;<a href="#EXEC or DISCARD">EXEC or DISCARD</a><br>&nbsp;&nbsp;&nbsp;&nbsp;<a href="#Usage">Usage</a><br>&nbsp;&nbsp;&nbsp;&nbsp;<a href="#The DISCARD command">The DISCARD command</a><br>&nbsp;&nbsp;&nbsp;&nbsp;<a href="#Return value">Return value</a>
20 </div>
21
22 <h1 class="wikiname">MultiExecCommand</h1>
23
24 <div class="summary">
25
26 </div>
27
28 <div class="narrow">
29 &iuml;&raquo;&iquest;#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 &quot;queue&quot; 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">
38?&gt; r.multi
39=&gt; &quot;OK&quot;
40&gt;&gt; r.incr &quot;foo&quot;
41=&gt; &quot;QUEUED&quot;
42&gt;&gt; r.incr &quot;bar&quot;
43=&gt; &quot;QUEUED&quot;
44&gt;&gt; r.incr &quot;bar&quot;
45=&gt; &quot;QUEUED&quot;
46&gt;&gt; r.exec
47=&gt; [1, 1, 2]
48</pre>
49<blockquote>As it is possible to see from the session above, MULTI returns an &quot;array&quot; 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 &quot;QUEUED&quot; 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">
52Trying 127.0.0.1...
53Connected to localhost.
54Escape character is '^]'.
55MULTI
56+OK
57SET a 3
58abc
59+QUEUED
60LPOP a
61+QUEUED
62EXEC
63*2
64+OK
65-ERR Operation against a key holding the wrong kind of value
66</pre>
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">
70MULTI
71+OK
72INCR a b c
73-ERR wrong number of arguments for 'incr' command
74</pre>
75<blockquote>This time due to the syntax error the &quot;bad&quot; 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?&gt; r.set(&quot;foo&quot;,1)
78=&gt; true
79&gt;&gt; r.multi
80=&gt; &quot;OK&quot;
81&gt;&gt; r.incr(&quot;foo&quot;)
82=&gt; &quot;QUEUED&quot;
83&gt;&gt; r.discard
84=&gt; &quot;OK&quot;
85&gt;&gt; r.get(&quot;foo&quot;)
86=&gt; &quot;1&quot;
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">
88The 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.
89</pre>
90 </div>
91
92 </div>
93 </div>
94 </body>
95</html>
96