]> git.saurik.com Git - redis.git/blame - doc/MultiExecCommand.html
Added used CPU statistics in INFO output, obtained via getrusage()
[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. -->
abe18d0e 19<b>MultiExecCommand: Contents</b><br>&nbsp;&nbsp;<a href="#WATCH key1 key2 ... keyN (Redis &gt;">WATCH key1 key2 ... keyN (Redis &gt;</a><br>&nbsp;&nbsp;<a href="#UNWATCH">UNWATCH</a><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="#Check and Set (CAS) transactions using WATCH">Check and Set (CAS) transactions using WATCH</a><br>&nbsp;&nbsp;&nbsp;&nbsp;<a href="#WATCH explained">WATCH explained</a><br>&nbsp;&nbsp;&nbsp;&nbsp;<a href="#WATCH used to implement ZPOP">WATCH used to implement ZPOP</a><br>&nbsp;&nbsp;&nbsp;&nbsp;<a href="#Return value">Return value</a>
aed57a31 20 </div>
21
22 <h1 class="wikiname">MultiExecCommand</h1>
23
24 <div class="summary">
25
26 </div>
27
28 <div class="narrow">
abe18d0e 29 &iuml;&raquo;&iquest;#sidebar <a href="GenericCommandsSidebar.html">GenericCommandsSidebar</a><h1><a name="WATCH key1 key2 ... keyN (Redis &gt;">WATCH key1 key2 ... keyN (Redis &gt;</a></h1> 2.1.0)=
30<h1><a name="UNWATCH">UNWATCH</a></h1>
31<h1><a name="MULTI">MULTI</a></h1>
aed57a31 32<h1><a name="COMMAND_1 ...">COMMAND_1 ...</a></h1>
33<h1><a name="COMMAND_2 ...">COMMAND_2 ...</a></h1>
34<h1><a name="COMMAND_N ...">COMMAND_N ...</a></h1>
abe18d0e 35<h1><a name="EXEC or DISCARD">EXEC or DISCARD</a></h1>MULTI, EXEC, DISCARD and WATCH commands are the fundation of Redis Transactions.
36A Redis Transaction allows the execution of a group of Redis commands in a single
37step, with two important guarantees:<br/><br/><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>
38Since Redis 2.1.0, it's also possible to add a further guarantee to the above two, in the form of optimistic locking of a set of keys in a way very similar to a CAS (check and set) operation. This is documented later in this manual page.<h2><a name="Usage">Usage</a></h2>A Redis transaction is entered using the MULTI command. The command always
39replies with OK. At this point the user can issue multiple commands. Instead
40to execute this commands Redis will &quot;queue&quot; them. All the commands are
41executed once EXEC is called.<br/><br/>Calling DISCARD instead will flush the transaction queue and will exit
42the transaction.<br/><br/>The following is an example using the Ruby client:
43<pre class="codeblock python" name="code">
aed57a31 44?&gt; r.multi
45=&gt; &quot;OK&quot;
46&gt;&gt; r.incr &quot;foo&quot;
47=&gt; &quot;QUEUED&quot;
48&gt;&gt; r.incr &quot;bar&quot;
49=&gt; &quot;QUEUED&quot;
50&gt;&gt; r.incr &quot;bar&quot;
51=&gt; &quot;QUEUED&quot;
52&gt;&gt; r.exec
53=&gt; [1, 1, 2]
54</pre>
abe18d0e 55As it is possible to see from the session above, MULTI returns an &quot;array&quot; of
56replies, where every element is the reply of a single command in the
57transaction, in the same order the commands were queued.<br/><br/>When a Redis connection is in the context of a MULTI request, all the commands
58will reply with a simple string &quot;QUEUED&quot; if they are correct from the
59point of view of the syntax and arity (number of arguments) of the commaand.
60Some command is still allowed to fail during execution time.<br/><br/>This is more clear if at protocol level: in the following example one command
61will fail when executed even if the syntax is right:
62<pre class="codeblock python python" name="code">
aed57a31 63Trying 127.0.0.1...
64Connected to localhost.
65Escape character is '^]'.
66MULTI
67+OK
68SET a 3
69abc
70+QUEUED
71LPOP a
72+QUEUED
73EXEC
74*2
75+OK
76-ERR Operation against a key holding the wrong kind of value
77</pre>
abe18d0e 78MULTI returned a two elements bulk reply in witch one of this is a +OK
79code and one is a -ERR reply. It's up to the client lib to find a sensible
80way to provide the error to the user.<br/><br/><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>
81Another example, again using the write protocol with telnet, shows how
82syntax errors are reported ASAP instead:
83<pre class="codeblock python python python" name="code">
aed57a31 84MULTI
85+OK
86INCR a b c
87-ERR wrong number of arguments for 'incr' command
88</pre>
abe18d0e 89This time due to the syntax error the &quot;bad&quot; INCR command is not queued
90at all.<h2><a name="The DISCARD command">The DISCARD command</a></h2>DISCARD can be used in order to abort a transaction. No command will be
91executed, and the state of the client is again the normal one, outside
92<blockquote>of a transaction. Example using the Ruby client:</blockquote><pre class="codeblock python python python python" name="code">
aed57a31 93?&gt; r.set(&quot;foo&quot;,1)
94=&gt; true
95&gt;&gt; r.multi
96=&gt; &quot;OK&quot;
97&gt;&gt; r.incr(&quot;foo&quot;)
98=&gt; &quot;QUEUED&quot;
99&gt;&gt; r.discard
100=&gt; &quot;OK&quot;
101&gt;&gt; r.get(&quot;foo&quot;)
102=&gt; &quot;1&quot;
abe18d0e 103</pre><h2><a name="Check and Set (CAS) transactions using WATCH">Check and Set (CAS) transactions using WATCH</a></h2>WATCH is used in order to provide a CAS (Check and Set) behavior to
104Redis Transactions.<br/><br/>WATCHed keys are monitored in order to detect changes against this keys.
105If at least a watched key will be modified before the EXEC call, the
106whole transaction will abort, and EXEC will return a nil object
107(A Null Multi Bulk reply) to notify that the transaction failed.<br/><br/>For example imagine we have the need to atomically increment the value
108of a key by 1 (I know we have INCR, let's suppose we don't have it).<br/><br/>The first try may be the following:
109<pre class="codeblock python python python python python" name="code">
110val = GET mykey
111val = val + 1
112SET mykey $val
113</pre>
114This will work reliably only if we have a single client performing the operation in a given time.
115If multiple clients will try to increment the key about at the same time
116there will be a race condition. For instance client A and B will read the
117old value, for instance, 10. The value will be incremented to 11 by both
118the clients, and finally SET as the value of the key. So the final value
119will be &quot;11&quot; instead of &quot;12&quot;.<br/><br/>Thanks to WATCH we are able to model the problem very well:
120<pre class="codeblock python python python python python python" name="code">
121WATCH mykey
122val = GET mykey
123val = val + 1
124MULTI
125SET mykey $val
126EXEC
aed57a31 127</pre>
abe18d0e 128Using the above code, if there are race conditions and another client
129modified the result of <i>val</i> in the time between our call to WATCH and
130our call to EXEC, the transaction will fail.<br/><br/>We'll have just to re-iterate the operation hoping this time we'll not get
131a new race. This form of locking is called <b>optimistic locking</b> and is
132a very powerful form of locking as in many problems there are multiple
133clients accessing a much bigger number of keys, so it's very unlikely that
134there are collisions: usually operations don't need to be performed
135multiple times.<h2><a name="WATCH explained">WATCH explained</a></h2>So what is WATCH really about? It is a command that will make the EXEC
136conditional: we are asking Redis to perform the transaction only if no
137other client modified any of the WATCHed keys. Otherwise the transaction is not
138entered at all. (Note that if you WATCH a volatile key and Redis expires the key after you WATCHed it, EXEC will still work. <a href="http://code.google.com/p/redis/issues/detail?id=270" target="_blank">More</a>.)<br/><br/>WATCH can be called multiple times. Simply all the WATCH calls will
139have the effects to watch for changes starting from the call, up to the
140moment EXEC is called.<br/><br/>When EXEC is called, either if it will fail or succeed, all keys are
141UNWATCHed. Also when a client connection is closed, everything gets
142UNWATCHed.<br/><br/>It is also possible to use the UNWATCH command (without arguments) in order
143to flush all the watched keys. Sometimes this is useful as we
144optimistically lock a few keys, since possibly we need to perform a transaction
145to alter those keys, but after reading the current content of the keys
146we don't want to proceed. When this happens we just call UNWATCH so that
147the connection can already be used freely for new transactions.<h2><a name="WATCH used to implement ZPOP">WATCH used to implement ZPOP</a></h2>A good example to illustrate how WATCH can be used to create new atomic
148operations otherwise not supported by Redis is to implement ZPOP, that is
149a command that pops the element with the lower score from a sorted set
150in an atomic way. This is the simplest implementation:
151<pre class="codeblock python python python python python python python" name="code">
152WATCH zset
153ele = ZRANGE zset 0 0
154MULTI
155ZREM zset ele
156EXEC
157</pre>
158If EXEC fails (returns a nil value) we just re-iterate the operation.<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 python python python" name="code">
159The 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.
160</pre>If a MULTI/EXEC transaction is aborted because of WATCH detected modified keys, a <a href="ReplyTypes.html">Null Multi Bulk reply</a> is returned.
aed57a31 161 </div>
162
163 </div>
164 </div>
165 </body>
166</html>
167