X-Git-Url: https://git.saurik.com/redis.git/blobdiff_plain/a4460ef43f9625e6c09cdc327614d1b47c043489..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 @@ + + + + + + + +
+ + + +
+
+ +MultiExecCommand: Contents
  MULTI
  COMMAND_1 ...
  COMMAND_2 ...
  COMMAND_N ...
  EXEC or DISCARD
    Usage
    The DISCARD command
    Return value +
+ +

MultiExecCommand

+ +
+ +
+ +
+ #sidebar GenericCommandsSidebar

MULTI

+

COMMAND_1 ...

+

COMMAND_2 ...

+

COMMAND_N ...

+

EXEC or DISCARD

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:
+
  • All the commands in a transaction are serialized and executed sequentially. It can never happen that a request issued by another client is served in the middle of the execution of a Redis transaction. This guarantees that the commands are executed as a single atomic operation.
  • 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.
+

Usage

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.
+

The DISCARD command

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"
+

Return value

Multi bulk reply, specifically:

+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.
+
+
+ +
+
+ + +