-
Notifications
You must be signed in to change notification settings - Fork 270
DOC-4340 more detail about connection pools and multiplexing #728
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| --- | ||
| categories: | ||
| - docs | ||
| - develop | ||
| - stack | ||
| - oss | ||
| - rs | ||
| - rc | ||
| - oss | ||
| - kubernetes | ||
| - clients | ||
| description: Manage Redis connections efficiently | ||
| linkTitle: Pooling/multiplexing | ||
| title: Connection pools and multiplexing | ||
| weight: 10 | ||
| --- | ||
|
|
||
| Redis example code generally opens a connection, demonstrates | ||
| a command or feature and then closes. Real-world code typically | ||
andy-stark-redis marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| has short bursts of communication with the server and periods of | ||
| inactivity in between. Opening and closing connections | ||
| involves some overhead and leads to inefficiency if you do | ||
| it frequently. This means that you can improve the performance of production | ||
| code by making as few separate connections as possible. | ||
|
|
||
| Managing connections in your own code can be tricky, so the Redis | ||
| client libraries give you some help. The two basic approaches to | ||
| connection management are called *connection pooling* and *multiplexing*. | ||
| The [`redis-py`]({{< relref "/develop/connect/clients/python/redis-py" >}}), | ||
| [`jedis`]({{< relref "/develop/connect/clients/java/jedis" >}}), and | ||
| [`go-redis`]({{< relref "/develop/connect/clients/go" >}}) clients support | ||
| connection pooling, while | ||
| [`NRedisStack`]({{< relref "/develop/connect/clients/dotnet" >}}) | ||
| supports multiplexing. | ||
| [`Lettuce`]({{< relref "/develop/connect/clients/java/lettuce" >}}) | ||
| supports both approaches. | ||
|
|
||
| ## Connection pooling | ||
|
|
||
| When you initialize a connection pool, the client opens a small number | ||
| of connections and adds them to the pool. | ||
|
|
||
| {{< image filename="/images/dev/connect/pool-and-mux/ConnPoolInit.drawio.svg" >}} | ||
|
|
||
| Each time you "open" a connection | ||
| from the pool, the client actually justs returns one of these existing | ||
andy-stark-redis marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| connections and notes the fact that it is in use. | ||
|
|
||
| {{< image filename="/images/dev/connect/pool-and-mux/ConnPoolInUse.drawio.svg" >}} | ||
|
|
||
| When you later "close" | ||
| the connection, the client puts it back into the pool of available | ||
| connections without actually closing it. | ||
|
|
||
| {{< image filename="/images/dev/connect/pool-and-mux/ConnPoolDiscon.drawio.svg" >}} | ||
|
|
||
| If all connections in the pool are in use but the app needs more then | ||
andy-stark-redis marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| the client can simply open new ones as necessary. In this way, the client | ||
andy-stark-redis marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| eventually finds the right number of connections to satisfy your | ||
| app's demands. | ||
|
|
||
| ## Multiplexing | ||
|
|
||
| Instead of pooling several connections, a multiplexer keeps a | ||
| single connection open and uses it for all traffic between the | ||
| client and the server. The "connections" returned to your code are | ||
| simply to identify where to send the response data from your commands. | ||
andy-stark-redis marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| {{< image filename="/images/dev/connect/pool-and-mux/ConnMux.drawio.svg" >}} | ||
|
|
||
| Note that it is not a problem if the multiplexer receives several commands close | ||
| together in time. When this happens, the multiplexer can often combine the commands into a | ||
| [pipeline]({{< relref "/develop/use/pipelining" >}}), which | ||
| improves efficiency even more. | ||
andy-stark-redis marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| Multiplexing offers high efficiency but works transparently without requiring | ||
| any special code to enable it in your app. The main disadvantage of multiplexing compared to | ||
| connection pooling is that it can't support the blocking "pop" commands (such as | ||
| [`BLPOP`]({{< relref "/commands/blpop" >}})) since these would stall the | ||
| connection for all callers. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.