X-Git-Url: https://git.saurik.com/redis.git/blobdiff_plain/924aa408b99837036b679bd3895f836af6bc763f..49128f0b9da725de992e427fa341a837bcc2991b:/doc/MultiExecCommand.html diff --git a/doc/MultiExecCommand.html b/doc/MultiExecCommand.html new file mode 100644 index 00000000..e0a41983 --- /dev/null +++ b/doc/MultiExecCommand.html @@ -0,0 +1,96 @@ + + + +
+ + + +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:+
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.+
Calling DISCARD instead will flush the transaction queue and will exitthe transaction.+
The following is an example using the Ruby client:
+?> r.multi +=> "OK" +>> r.incr "foo" +=> "QUEUED" +>> r.incr "bar" +=> "QUEUED" +>> r.incr "bar" +=> "QUEUED" +>> r.exec +=> [1, 1, 2] ++
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.+
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.+
This is more clear if at protocol level: in the following example one commandwill fail when executed even if the syntax is right:
+Trying 127.0.0.1... +Connected to localhost. +Escape character is '^]'. +MULTI ++OK +SET a 3 +abc ++QUEUED +LPOP a ++QUEUED +EXEC +*2 ++OK +-ERR Operation against a key holding the wrong kind of value ++
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.+
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.+
Another example, again using the write protocol with telnet, shows howsyntax errors are reported ASAP instead:
+MULTI ++OK +INCR a b c +-ERR wrong number of arguments for 'incr' command ++
This time due to the syntax error the "bad" INCR command is not queuedat all.+
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:
+?> r.set("foo",1) +=> true +>> r.multi +=> "OK" +>> r.incr("foo") +=> "QUEUED" +>> r.discard +=> "OK" +>> r.get("foo") +=> "1" +
+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. ++