Skip to content

Commit 697d665

Browse files
committed
Merge branch 'main' into DOC-4137
2 parents 639fc50 + 553deca commit 697d665

File tree

58 files changed

+1316
-304
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+1316
-304
lines changed

content/commands/client-caching/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ title: CLIENT CACHING
4343
This command controls the tracking of the keys in the next command executed
4444
by the connection, when tracking is enabled in `OPTIN` or `OPTOUT` mode.
4545
Please check the
46-
[client side caching documentation]({{< relref "/develop/use/client-side-caching" >}}) for
46+
[client side caching documentation]({{< relref "/develop/connect/clients/client-side-caching" >}}) for
4747
background information.
4848

4949
When tracking is enabled Redis, using the [`CLIENT TRACKING`]({{< relref "/commands/client-tracking" >}}) command, it is

content/commands/client-getredir/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ syntax_str: ''
3131
title: CLIENT GETREDIR
3232
---
3333
This command returns the client ID we are redirecting our
34-
[tracking]({{< relref "/develop/use/client-side-caching" >}}) notifications to. We set a client
34+
[tracking]({{< relref "/develop/connect/clients/client-side-caching#tracking" >}}) notifications to. We set a client
3535
to redirect to when using [`CLIENT TRACKING`]({{< relref "/commands/client-tracking" >}}) to enable tracking. However in
3636
order to avoid forcing client libraries implementations to remember the
3737
ID notifications are redirected to, this command exists in order to improve

content/commands/client-tracking/index.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ syntax_str: "[REDIRECT\_client-id] [PREFIX\_prefix [PREFIX prefix ...]] [BCAST]
7575
title: CLIENT TRACKING
7676
---
7777
This command enables the tracking feature of the Redis server, that is used
78-
for [server assisted client side caching]({{< relref "/develop/use/client-side-caching" >}}).
78+
for [server assisted client side caching]({{< relref "/develop/connect/clients/client-side-caching#tracking" >}}).
7979

8080
When tracking is enabled Redis remembers the keys that the connection
8181
requested, in order to send later invalidation messages when such keys are
@@ -85,7 +85,7 @@ when the RESP3 protocol is used) or redirected in a different connection
8585
available where clients participating in this protocol receive every
8686
notification just subscribing to given key prefixes, regardless of the
8787
keys that they requested. Given the complexity of the argument please
88-
refer to [the main client side caching documentation]({{< relref "/develop/use/client-side-caching" >}}) for the details. This manual page is only a reference for the options of this subcommand.
88+
refer to [the main client side caching documentation]({{< relref "/develop/reference/client-side-caching" >}}) for the details. This manual page is only a reference for the options of this subcommand.
8989

9090
In order to enable tracking, use:
9191

content/commands/client-trackinginfo/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ syntax_fmt: CLIENT TRACKINGINFO
2929
syntax_str: ''
3030
title: CLIENT TRACKINGINFO
3131
---
32-
The command returns information about the current client connection's use of the [server assisted client side caching]({{< relref "/develop/use/client-side-caching" >}}) feature.
32+
The command returns information about the current client connection's use of the [server assisted client side caching]({{< relref "/develop/connect/clients/client-side-caching" >}}) feature.
3333

3434
Here's the list of tracking information sections and their respective values:
3535

content/develop/connect/clients/client-side-caching.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ weight: 20
1717

1818
*Client-side caching* reduces network traffic between
1919
a Redis client and the server, which generally improves performance.
20-
See [Client-side caching compatibility with Redis Software and Redis Cloud]({{< relref "operate/rs/references/compatibility/client-side-caching" >}})
21-
for details on Redis versions that support client-side caching.
2220

2321
By default, an [application server](https://en.wikipedia.org/wiki/Application_server)
2422
(which sits between the user app and the database) contacts the
@@ -45,7 +43,7 @@ client-side caching works well in the very common use case where a small subset
4543
is accessed much more frequently than the rest of the data (according
4644
to the [Pareto principle](https://en.wikipedia.org/wiki/Pareto_principle)).
4745

48-
## Updating the cache when the data changes
46+
## Updating the cache when the data changes {#tracking}
4947

5048
All caching systems must implement a scheme to update data in the cache
5149
when the corresponding data changes in the main database. Redis uses an
@@ -63,6 +61,12 @@ will evict the stale data in response. Next time a client reads from
6361
the same key, it will access the database directly and refresh its cache
6462
with the updated data.
6563

64+
{{< note >}}If any connection from a client gets disconnected (including
65+
one from a connection pool), then the client will flush all keys from the
66+
client-side cache. Caching then resumes for subsequent reads from the
67+
connections that are still active.
68+
{{< /note >}}
69+
6670
The sequence diagram below shows how two clients might interact as they
6771
access and update the same key:
6872

@@ -82,7 +86,7 @@ will use cached data, except for the following:
8286
[`HSCAN`]({{< relref "/commands/hscan" >}}),
8387
and [`ZRANDMEMBER`]({{< relref "/commands/zrandmember" >}}). By design, these commands
8488
give different results each time they are called.
85-
- Search and query commands (with the `FT.*` prefix), such as
89+
- Redis Query Engine commands (with the `FT.*` prefix), such as
8690
[`FT.SEARCH`]({{< baseurl >}}/commands/ft.search).
8791

8892
You can use the [`MONITOR`]({{< relref "/commands/monitor" >}}) command to

content/develop/connect/clients/dotnet.md

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ dotnet add package NRedisStack
3737

3838
Connect to localhost on port 6379.
3939

40-
```
40+
```csharp
4141
using NRedisStack;
4242
using NRedisStack.RedisStackCommands;
4343
using StackExchange.Redis;
@@ -70,7 +70,7 @@ Console.WriteLine(String.Join("; ", hashFields));
7070
// name: John; surname: Smith; company: Redis; age: 29
7171
```
7272

73-
To access Redis Stack capabilities, you should use appropriate interface like this:
73+
To access Redis Stack capabilities, use the appropriate interface like this:
7474

7575
```
7676
IBloomCommands bf = db.BF();
@@ -84,7 +84,7 @@ IJsonCommands json = db.JSON();
8484
ITimeSeriesCommands ts = db.TS();
8585
```
8686

87-
### Connect to a Redis cluster
87+
## Connect to a Redis cluster
8888

8989
To connect to a Redis cluster, you just need to specify one or all cluster endpoints in the client configuration:
9090

@@ -106,7 +106,7 @@ db.StringSet("foo", "bar");
106106
Console.WriteLine(db.StringGet("foo")); // prints bar
107107
```
108108

109-
### Connect to your production Redis with TLS
109+
## Connect to your production Redis with TLS
110110

111111
When you deploy your application, use TLS and follow the [Redis security]({{< relref "/operate/oss_and_stack/management/security/" >}}) guidelines.
112112

@@ -169,6 +169,23 @@ conn.StringSet("foo", "bar");
169169
Console.WriteLine(conn.StringGet("foo"));
170170
```
171171

172+
## Multiplexing
173+
174+
Although example code typically works with a single connection,
175+
real-world code often uses multiple connections at the same time.
176+
Opening and closing connections repeatedly is inefficient, so it is best
177+
to manage open connections carefully to avoid this.
178+
179+
Several other
180+
Redis client libraries use *connection pools* to reuse a set of open
181+
connections efficiently. NRedisStack uses a different approach called
182+
*multiplexing*, which sends all client commands and responses over a
183+
single connection. NRedisStack manages multiplexing for you automatically.
184+
This gives high performance without requiring any extra coding.
185+
See
186+
[Connection pools and multiplexing]({{< relref "/develop/connect/clients/pools-and-muxing" >}})
187+
for more information.
188+
172189
## Example: Indexing and querying JSON documents
173190

174191
This example shows how to convert Redis search results to JSON format using `NRedisStack`.

content/develop/connect/clients/java/jedis.md

Lines changed: 76 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -56,46 +56,36 @@ To include `Jedis` as a dependency in your application, edit the dependency file
5656

5757
## Connect
5858

59-
For many applications, it's best to use a connection pool. You can instantiate and use a `Jedis` connection pool like so:
59+
The following code opens a basic connection to a local Redis server:
6060

6161
```java
6262
package org.example;
63-
import redis.clients.jedis.Jedis;
64-
import redis.clients.jedis.JedisPool;
63+
import redis.clients.jedis.UnifiedJedis;
6564

6665
public class Main {
6766
public static void main(String[] args) {
68-
JedisPool pool = new JedisPool("localhost", 6379);
67+
UnifiedJedis jedis = new UnifiedJedis("redis://localhost:6379");
6968

70-
try (Jedis jedis = pool.getResource()) {
71-
// Store & Retrieve a simple string
72-
jedis.set("foo", "bar");
73-
System.out.println(jedis.get("foo")); // prints bar
74-
75-
// Store & Retrieve a HashMap
76-
Map<String, String> hash = new HashMap<>();;
77-
hash.put("name", "John");
78-
hash.put("surname", "Smith");
79-
hash.put("company", "Redis");
80-
hash.put("age", "29");
81-
jedis.hset("user-session:123", hash);
82-
System.out.println(jedis.hgetAll("user-session:123"));
83-
// Prints: {name=John, surname=Smith, company=Redis, age=29}
84-
}
69+
// Code that interacts with Redis...
70+
71+
jedis.close();
8572
}
8673
}
8774
```
8875

89-
Because adding a `try-with-resources` block for each command can be cumbersome, consider using `JedisPooled` as an easier way to pool connections.
76+
After you have connected, you can check the connection by storing and
77+
retrieving a simple string value:
9078

9179
```java
92-
import redis.clients.jedis.JedisPooled;
80+
...
9381

94-
//...
82+
String res1 = jedis.set("bike:1", "Deimos");
83+
System.out.println(res1); // OK
9584

96-
JedisPooled jedis = new JedisPooled("localhost", 6379);
97-
jedis.set("foo", "bar");
98-
System.out.println(jedis.get("foo")); // prints "bar"
85+
String res2 = jedis.get("bike:1");
86+
System.out.println(res2); // Deimos
87+
88+
...
9989
```
10090

10191
### Connect to a Redis cluster
@@ -309,15 +299,65 @@ client.hget("person:2", "name"); // Read from the server
309299
client.hget("person:2", "name"); // Read from the cache
310300
```
311301

312-
## Production usage
302+
The client will also flush the cache automatically
303+
if any connection (including one from a connection pool)
304+
is disconnected.
313305

314-
The following sections explain how to handle situations that may occur
315-
in your production environment.
306+
## Connect with a connection pool
316307

317-
### Configuring a connection pool
308+
For production usage, you should use a connection pool to manage
309+
connections rather than opening and closing connections individually.
310+
A connection pool maintains several open connections and reuses them
311+
efficiently. When you open a connection from a pool, the pool allocates
312+
one of its open connections. When you subsequently close the same connection,
313+
it is not actually closed but simply returned to the pool for reuse.
314+
This avoids the overhead of repeated connecting and disconnecting.
315+
See
316+
[Connection pools and multiplexing]({{< relref "/develop/connect/clients/pools-and-muxing" >}})
317+
for more information.
318+
319+
Use the following code to connect with a connection pool:
320+
321+
```java
322+
package org.example;
323+
import redis.clients.jedis.Jedis;
324+
import redis.clients.jedis.JedisPool;
325+
326+
public class Main {
327+
public static void main(String[] args) {
328+
JedisPool pool = new JedisPool("localhost", 6379);
329+
330+
try (Jedis jedis = pool.getResource()) {
331+
// Store & Retrieve a simple string
332+
jedis.set("foo", "bar");
333+
System.out.println(jedis.get("foo")); // prints bar
334+
335+
// Store & Retrieve a HashMap
336+
Map<String, String> hash = new HashMap<>();;
337+
hash.put("name", "John");
338+
hash.put("surname", "Smith");
339+
hash.put("company", "Redis");
340+
hash.put("age", "29");
341+
jedis.hset("user-session:123", hash);
342+
System.out.println(jedis.hgetAll("user-session:123"));
343+
// Prints: {name=John, surname=Smith, company=Redis, age=29}
344+
}
345+
}
346+
}
347+
```
348+
349+
Because adding a `try-with-resources` block for each command can be cumbersome, consider using `JedisPooled` as an easier way to pool connections. `JedisPooled`, added in Jedis version 4.0.0, provides capabilities similar to `JedisPool` but with a more straightforward API.
350+
351+
```java
352+
import redis.clients.jedis.JedisPooled;
353+
354+
//...
355+
356+
JedisPooled jedis = new JedisPooled("localhost", 6379);
357+
jedis.set("foo", "bar");
358+
System.out.println(jedis.get("foo")); // prints "bar"
359+
```
318360

319-
As mentioned in the previous section, use `JedisPool` or `JedisPooled` to create a connection pool.
320-
`JedisPooled`, added in Jedis version 4.0.0, provides capabilities similar to `JedisPool` but with a more straightforward API.
321361
A connection pool holds a specified number of connections, creates more connections when necessary, and terminates them when they are no longer needed.
322362

323363
Here is a simplified connection lifecycle in a pool:
@@ -363,6 +403,11 @@ poolConfig.setTimeBetweenEvictionRuns(Duration.ofSeconds(1));
363403
JedisPooled jedis = new JedisPooled(poolConfig, "localhost", 6379);
364404
```
365405

406+
## Production usage
407+
408+
The following sections explain how to handle situations that may occur
409+
in your production environment.
410+
366411
### Timeouts
367412

368413
To set a timeout for a connection, use the `JedisPooled` or `JedisPool` constructor with the `timeout` parameter, or use `JedisClientConfig` with the `socketTimeout` and `connectionTimeout` parameters:

content/develop/connect/clients/java/lettuce.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,9 @@ try (RedisClient client = RedisClient.create(redisURI)) {
234234

235235
A typical approach with Lettuce is to create a single `RedisClient` instance and reuse it to establish connections to your Redis server(s).
236236
These connections are multiplexed; that is, multiple commands can be run concurrently over a single or a small set of connections, making explicit pooling less practical.
237+
See
238+
[Connection pools and multiplexing]({{< relref "/develop/connect/clients/pools-and-muxing" >}})
239+
for more information.
237240

238241
Lettuce provides pool config to be used with Lettuce asynchronous connection methods.
239242

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
---
2+
categories:
3+
- docs
4+
- develop
5+
- stack
6+
- oss
7+
- rs
8+
- rc
9+
- oss
10+
- kubernetes
11+
- clients
12+
description: Manage Redis connections efficiently
13+
linkTitle: Pooling/multiplexing
14+
title: Connection pools and multiplexing
15+
weight: 10
16+
---
17+
18+
Redis example code generally opens a connection, demonstrates
19+
a command or feature, and then closes. Real-world code typically
20+
has short bursts of communication with the server and periods of
21+
inactivity in between. Opening and closing connections
22+
involves some overhead and leads to inefficiency if you do
23+
it frequently. This means that you can improve the performance of production
24+
code by making as few separate connections as possible.
25+
26+
Managing connections in your own code can be tricky, so the Redis
27+
client libraries give you some help. The two basic approaches to
28+
connection management are called *connection pooling* and *multiplexing*.
29+
The [`redis-py`]({{< relref "/develop/connect/clients/python/redis-py" >}}),
30+
[`jedis`]({{< relref "/develop/connect/clients/java/jedis" >}}), and
31+
[`go-redis`]({{< relref "/develop/connect/clients/go" >}}) clients support
32+
connection pooling, while
33+
[`NRedisStack`]({{< relref "/develop/connect/clients/dotnet" >}})
34+
supports multiplexing.
35+
[`Lettuce`]({{< relref "/develop/connect/clients/java/lettuce" >}})
36+
supports both approaches.
37+
38+
## Connection pooling
39+
40+
When you initialize a connection pool, the client opens a small number
41+
of connections and adds them to the pool.
42+
43+
{{< image filename="/images/dev/connect/pool-and-mux/ConnPoolInit.drawio.svg" >}}
44+
45+
Each time you "open" a connection
46+
from the pool, the client returns one of these existing
47+
connections and notes the fact that it is in use.
48+
49+
{{< image filename="/images/dev/connect/pool-and-mux/ConnPoolInUse.drawio.svg" >}}
50+
51+
When you later "close"
52+
the connection, the client puts it back into the pool of available
53+
connections without actually closing it.
54+
55+
{{< image filename="/images/dev/connect/pool-and-mux/ConnPoolDiscon.drawio.svg" >}}
56+
57+
If all connections in the pool are in use but the app needs more, then
58+
the client can simply open new connections as necessary. In this way, the client
59+
eventually finds the right number of connections to satisfy your
60+
app's demands.
61+
62+
## Multiplexing
63+
64+
Instead of pooling several connections, a multiplexer keeps a
65+
single connection open and uses it for all traffic between the
66+
client and the server. The "connections" returned to your code are
67+
used to identify where to send the response data from your commands.
68+
69+
{{< image filename="/images/dev/connect/pool-and-mux/ConnMux.drawio.svg" >}}
70+
71+
Note that it is not a problem if the multiplexer receives several commands close
72+
together in time. When this happens, the multiplexer can often combine the commands into a
73+
[pipeline]({{< relref "/develop/use/pipelining" >}}), which
74+
improves efficiency.
75+
76+
Multiplexing offers high efficiency but works transparently without requiring
77+
any special code to enable it in your app. The main disadvantage of multiplexing compared to
78+
connection pooling is that it can't support the blocking "pop" commands (such as
79+
[`BLPOP`]({{< relref "/commands/blpop" >}})) since these would stall the
80+
connection for all callers.

0 commit comments

Comments
 (0)