Skip to content

Commit ceacfff

Browse files
DOC-4560 added Jedis page and testable examples
1 parent 13bc6ac commit ceacfff

File tree

1 file changed

+86
-0
lines changed

1 file changed

+86
-0
lines changed
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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 main difference with the pipeline commands is that they return
40+
`Response<Type>` objects, where `Type` is the return type of the
41+
standard command method. A `Response` object contains a valid result
42+
only after the pipeline has finished executing. You can access the
43+
result using the `Response` object's `get()` method.
44+
45+
{{< clients-example pipe_trans_tutorial basic_pipe Java-Sync >}}
46+
{{< /clients-example >}}
47+
48+
## Execute a transaction
49+
50+
A transaction works in a similar way to a pipeline. Create a
51+
transaction object with the `multi()`, call command methods
52+
on that object, and then call the transaction object's
53+
`exec()` method to execute it. You can access the results
54+
from commands in the transaction using `Response` objects, as
55+
you would with a pipeline. However, the `exec()` method also
56+
returns a `List<Object>` value that contains all the result
57+
values in the order the commands were executed (see
58+
[Watch keys for changes](#watch-keys-for-changes) below for
59+
an example that uses the results list).
60+
61+
{{< clients-example pipe_trans_tutorial basic_trans Java-Sync >}}
62+
{{< /clients-example >}}
63+
64+
## Watch keys for changes
65+
66+
Redis supports *optimistic locking* to avoid inconsistent updates
67+
to different keys. The basic idea is to watch for changes to any
68+
keys that you use in a transaction while you are are processing the
69+
updates. If the watched keys do change, you must restart the updates
70+
with the latest data from the keys. See
71+
[Transactions]({{< relref "/develop/interact/transactions" >}})
72+
for more information about optimistic locking.
73+
74+
The code below reads a string
75+
that represents a `PATH` variable for a command shell, then appends a new
76+
command path to the string before attempting to write it back. If the watched
77+
key is modified by another client before writing, the transaction aborts.
78+
Note that you should call read-only commands for the watched keys synchronously on
79+
the usual client object (called `jedis` in our examples) but you still call commands
80+
for the transaction on the transaction object.
81+
82+
For production usage, you would generally call code like the following in
83+
a loop to retry it until it succeeds or else report or log the failure.
84+
85+
{{< clients-example pipe_trans_tutorial trans_watch Java-Sync >}}
86+
{{< /clients-example >}}

0 commit comments

Comments
 (0)