|
| 1 | +--- |
| 2 | +categories: |
| 3 | +- docs |
| 4 | +- develop |
| 5 | +- stack |
| 6 | +- oss |
| 7 | +- rs |
| 8 | +- rc |
| 9 | +- oss |
| 10 | +- kubernetes |
| 11 | +- clients |
| 12 | +description: Learn how to use Redis pipelines and transactions |
| 13 | +linkTitle: Pipelines/transactions |
| 14 | +title: Pipelines and transactions |
| 15 | +weight: 2 |
| 16 | +--- |
| 17 | + |
| 18 | +Redis lets you send a sequence of commands to the server together in a batch. |
| 19 | +There are two types of batch that you can use: |
| 20 | + |
| 21 | +- **Pipelines** avoid network and processing overhead by sending several commands |
| 22 | + to the server together in a single communication. The server then sends back |
| 23 | + a single communication with all the responses. See the |
| 24 | + [Pipelining]({{< relref "/develop/use/pipelining" >}}) page for more |
| 25 | + information. |
| 26 | +- **Transactions** guarantee that all the included commands will execute |
| 27 | + to completion without being interrupted by commands from other clients. |
| 28 | + See the [Transactions]({{< relref "/develop/interact/transactions" >}}) |
| 29 | + page for more information. |
| 30 | + |
| 31 | +## Execute a pipeline |
| 32 | + |
| 33 | +To execute commands in a pipeline, you first create a pipeline object |
| 34 | +and then add commands to it using methods that resemble the standard |
| 35 | +command methods (for example, `set()` and `get()`). The commands are |
| 36 | +buffered in the pipeline and only execute when you call the `sync()` |
| 37 | +method on the pipeline object. |
| 38 | + |
| 39 | +The return values of the pipeline commands are `Response<Type>` objects, |
| 40 | +where `Type` is the type returned by the standard non-pipelined form |
| 41 | +of the command. You can access the return value from a `Response<>` object |
| 42 | +only after the pipeline has executed using its `get()` method. |
| 43 | + |
| 44 | +<!-- Tested examples will replace the inline ones when they are approved. |
| 45 | +Markup removed to stop warnings. |
| 46 | +--> |
| 47 | +{{< clients-example pipe_trans_tutorial basic_pipe >}} |
| 48 | +{{< /clients-example >}} |
| 49 | + |
| 50 | +## Execute a transaction |
| 51 | + |
| 52 | +{{< clients-example pipe_trans_tutorial basic_trans >}} |
| 53 | +{{< /clients-example >}} |
| 54 | + |
| 55 | +## Watch keys for changes |
| 56 | + |
| 57 | +Redis supports *optimistic locking* to avoid inconsistent updates |
| 58 | +to different keys. The basic idea is to watch for changes to any |
| 59 | +keys that you use in a transaction while you are are processing the |
| 60 | +updates. If the watched keys do change, you must restart the updates |
| 61 | +with the latest data from the keys. See |
| 62 | +[Transactions]({{< relref "/develop/interact/transactions" >}}) |
| 63 | +for more information about optimistic locking. |
| 64 | + |
| 65 | +The example below shows how to repeatedly attempt a transaction with a watched |
| 66 | +key until it succeeds. The code reads a string |
| 67 | +that represents a `PATH` variable for a command shell, then appends a new |
| 68 | +command path to the string before attempting to write it back. If the watched |
| 69 | +key is modified by another client before writing, the transaction aborts |
| 70 | +with a `WatchError` exception, and the loop executes again for another attempt. |
| 71 | +Otherwise, the loop terminates successfully. |
| 72 | + |
| 73 | +<!-- |
| 74 | +clients-example pipe_trans_tutorial trans_watch Python |
| 75 | +/clients-example |
| 76 | +--> |
| 77 | + |
| 78 | +{{< clients-example pipe_trans_tutorial trans_watch >}} |
| 79 | +{{< /clients-example >}} |
0 commit comments