Skip to content

Commit cfb110c

Browse files
mgravellNickCraver
andauthored
Protocol support: RESP3 (#2396)
Overall changes: - [x] introduce `Resp2Type` and `Resp3Type` shims (`Resp2Type` has reduced types); existing code using `[Obsolete] Type` uses `Resp2Type` for minimal code impact - [x] mark existing `Type` as `[Obsolete]`, and proxy to `Resp2Type` for compat - [x] deal with null handling differences - [x] deal with `Boolean`, which works very differently (`t`/`f` instead of `1`/`0`) - [x] deal with `[+|-]{inf|nan}` when parsing doubles (explicitly called out in the RESP3 spec) - [x] parse new tokens - [x] `HELLO` handshake - [x] core message and result handling - [x] validation and fallback - [x] prove all return types (see: redis/redis-specifications#15) - [x] <strike>streamed RESP3</strike> omitting; not implemented by server; can revisit - [x] deal with pub/sub differences - [x] check routing of publish etc - [x] check re-wire of subscription if failed - [x] check receive notifications - [x] connection management (i.e. not to spin up in resp3) - [x] connection fallback spinup (i.e. if we were trying resp3 but failed) - [x] other - [x] [undocumented RESP3 delta](redis/redis-doc#2513) - [x] run core tests in both RESP2 and RESP3 - [x] compensate for tests that expect separate subscription connections Co-authored-by: Nick Craver <[email protected]> Co-authored-by: Nick Craver <[email protected]>
1 parent 20f41f6 commit cfb110c

File tree

96 files changed

+2530
-773
lines changed

Some content is hidden

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

96 files changed

+2530
-773
lines changed

Directory.Packages.props

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
<!-- For binding redirect testing, main package gets this transitively -->
2424
<PackageVersion Include="System.IO.Pipelines" Version="5.0.1" />
2525
<PackageVersion Include="System.Runtime.Caching" Version="5.0.0" />
26-
<PackageVersion Include="xunit" Version="2.4.2-pre.12" />
27-
<PackageVersion Include="xunit.runner.visualstudio" Version="2.4.3" />
26+
<PackageVersion Include="xunit" Version="2.5.0" />
27+
<PackageVersion Include="xunit.runner.visualstudio" Version="2.5.0" />
2828
</ItemGroup>
2929
</Project>

StackExchange.Redis.sln

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "docs", "docs", "{153A10E4-E
119119
docs\Profiling_v2.md = docs\Profiling_v2.md
120120
docs\PubSubOrder.md = docs\PubSubOrder.md
121121
docs\ReleaseNotes.md = docs\ReleaseNotes.md
122+
docs\Resp3.md = docs\Resp3.md
122123
docs\Scripting.md = docs\Scripting.md
123124
docs\Server.md = docs\Server.md
124125
docs\Testing.md = docs\Testing.md

docs/Configuration.md

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Configuration
1+
# Configuration
22
===
33

44
When connecting to Redis version 6 or above with an ACL configured, your ACL user needs to at least have permissions to run the ECHO command. We run this command to verify that we have a valid connection to the Redis service.
@@ -15,7 +15,7 @@ The `configuration` here can be either:
1515

1616
The latter is *basically* a tokenized form of the former.
1717

18-
Basic Configuration Strings
18+
## Basic Configuration Strings
1919
-
2020

2121
The *simplest* configuration example is just the host name:
@@ -66,7 +66,7 @@ Microsoft Azure Redis example with password
6666
var conn = ConnectionMultiplexer.Connect("contoso5.redis.cache.windows.net,ssl=true,password=...");
6767
```
6868

69-
Configuration Options
69+
## Configuration Options
7070
---
7171

7272
The `ConfigurationOptions` object has a wide range of properties, all of which are fully documented in intellisense. Some of the more common options to use include:
@@ -98,6 +98,7 @@ The `ConfigurationOptions` object has a wide range of properties, all of which a
9898
| version={string} | `DefaultVersion` | (`4.0` in Azure, else `2.0`) | Redis version level (useful when the server does not make this available) |
9999
| tunnel={string} | `Tunnel` | `null` | Tunnel for connections (use `http:{proxy url}` for "connect"-based proxy server) |
100100
| setlib={bool} | `SetClientLibrary` | `true` | Whether to attempt to use `CLIENT SETINFO` to set the library name/version on the connection |
101+
| protocol={string} | `Protocol` | `null` | Redis protocol to use; see section below |
101102

102103
Additional code-only options:
103104
- LoggerFactory (`ILoggerFactory`) - Default: `null`
@@ -123,16 +124,17 @@ Additional code-only options:
123124
Tokens in the configuration string are comma-separated; any without an `=` sign are assumed to be redis server endpoints. Endpoints without an explicit port will use 6379 if ssl is not enabled, and 6380 if ssl is enabled.
124125
Tokens starting with `$` are taken to represent command maps, for example: `$config=cfg`.
125126

126-
Obsolete Configuration Options
127+
## Obsolete Configuration Options
127128
---
129+
128130
These options are parsed in connection strings for backwards compatibility (meaning they do not error as invalid), but no longer have any effect.
129131

130132
| Configuration string | `ConfigurationOptions` | Previous Default | Previous Meaning |
131133
| ---------------------- | ---------------------- | ---------------------------- | --------------------------------------------------------------------------------------------------------- |
132134
| responseTimeout={int} | `ResponseTimeout` | `SyncTimeout` | Time (ms) to decide whether the socket is unhealthy |
133135
| writeBuffer={int} | `WriteBuffer` | `4096` | Size of the output buffer |
134136

135-
Automatic and Manual Configuration
137+
## Automatic and Manual Configuration
136138
---
137139

138140
In many common scenarios, StackExchange.Redis will automatically configure a lot of settings, including the server type and version, connection timeouts, and primary/replica relationships. Sometimes, though, the commands for this have been disabled on the redis server. In this case, it is useful to provide more information:
@@ -161,7 +163,8 @@ Which is equivalent to the command string:
161163
```config
162164
redis0:6379,redis1:6380,keepAlive=180,version=2.8.8,$CLIENT=,$CLUSTER=,$CONFIG=,$ECHO=,$INFO=,$PING=
163165
```
164-
Renaming Commands
166+
167+
## Renaming Commands
165168
---
166169

167170
A slightly unusual feature of redis is that you can disable and/or rename individual commands. As per the previous example, this is done via the `CommandMap`, but instead of passing a `HashSet<string>` to `Create()` (to indicate the available or unavailable commands), you pass a `Dictionary<string,string>`. All commands not mentioned in the dictionary are assumed to be enabled and not renamed. A `null` or blank value records that the command is disabled. For example:
@@ -184,8 +187,9 @@ The above is equivalent to (in the connection string):
184187
$INFO=,$SELECT=use
185188
```
186189

187-
Redis Server Permissions
190+
## Redis Server Permissions
188191
---
192+
189193
If the user you're connecting to Redis with is limited, it still needs to have certain commands enabled for the StackExchange.Redis to succeed in connecting. The client uses:
190194
- `AUTH` to authenticate
191195
- `CLIENT` to set the client name
@@ -205,7 +209,7 @@ For example, a common _very_ minimal configuration ACL on the server (non-cluste
205209

206210
Note that if you choose to disable access to the above commands, it needs to be done via the `CommandMap` and not only the ACL on the server (otherwise we'll attempt the command and fail the handshake). Also, if any of the these commands are disabled, some functionality may be diminished or broken.
207211

208-
twemproxy
212+
## twemproxy
209213
---
210214

211215
[twemproxy](https://github.com/twitter/twemproxy) is a tool that allows multiple redis instances to be used as though it were a single server, with inbuilt sharding and fault tolerance (much like redis cluster, but implemented separately). The feature-set available to Twemproxy is reduced. To avoid having to configure this manually, the `Proxy` option can be used:
@@ -218,8 +222,9 @@ var options = new ConfigurationOptions
218222
};
219223
```
220224

221-
envoyproxy
225+
##envoyproxy
222226
---
227+
223228
[Envoyproxy](https://github.com/envoyproxy/envoy) is a tool that allows to front a redis cluster with a set of proxies, with inbuilt discovery and fault tolerance. The feature-set available to Envoyproxy is reduced. To avoid having to configure this manually, the `Proxy` option can be used:
224229
```csharp
225230
var options = new ConfigurationOptions+{
@@ -229,7 +234,7 @@ var options = new ConfigurationOptions+{
229234
```
230235

231236

232-
Tiebreakers and Configuration Change Announcements
237+
## Tiebreakers and Configuration Change Announcements
233238
---
234239

235240
Normally StackExchange.Redis will resolve primary/replica nodes automatically. However, if you are not using a management tool such as redis-sentinel or redis cluster, there is a chance that occasionally you will get multiple primary nodes (for example, while resetting a node for maintenance it may reappear on the network as a primary). To help with this, StackExchange.Redis can use the notion of a *tie-breaker* - which is only used when multiple primaries are detected (not including redis cluster, where multiple primaries are *expected*). For compatibility with BookSleeve, this defaults to the key named `"__Booksleeve_TieBreak"` (always in database 0). This is used as a crude voting mechanism to help determine the *preferred* primary, so that work is routed correctly.
@@ -240,8 +245,9 @@ Both options can be customized or disabled (set to `""`), via the `.Configuratio
240245

241246
These settings are also used by the `IServer.MakeMaster()` method, which can set the tie-breaker in the database and broadcast the configuration change message. The configuration message can also be used separately to primary/replica changes simply to request all nodes to refresh their configurations, via the `ConnectionMultiplexer.PublishReconfigure` method.
242247

243-
ReconnectRetryPolicy
248+
## ReconnectRetryPolicy
244249
---
250+
245251
StackExchange.Redis automatically tries to reconnect in the background when the connection is lost for any reason. It keeps retrying until the connection has been restored. It would use ReconnectRetryPolicy to decide how long it should wait between the retries.
246252
ReconnectRetryPolicy can be exponential (default), linear or a custom retry policy.
247253

@@ -266,3 +272,16 @@ config.ReconnectRetryPolicy = new LinearRetry(5000);
266272
//5 5000
267273
//6 5000
268274
```
275+
276+
## Redis protocol
277+
278+
Without specific configuration, StackExchange.Redis will use the RESP2 protocol; this means that pub/sub requires a separate connection to the server. RESP3 is a newer protocol
279+
(usually, but not always, available on v6 servers and above) which allows (among other changes) pub/sub messages to be communicated on the *same* connection - which can be very
280+
desirable in servers with a large number of clients. The protocol handshake needs to happen very early in the connection, so *by default* the library does not attempt a RESP3 connection
281+
unless it has reason to expect it to work.
282+
283+
The library determines whether to use RESP3 by:
284+
- The `HELLO` command has been disabled: RESP2 is used
285+
- A protocol *other than* `resp3` or `3` is specified: RESP2 is used
286+
- A protocol of `resp3` or `3` is specified: RESP3 is attempted (with fallback if it fails)
287+
- In all other scenarios: RESP2 is used

docs/ReleaseNotes.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Current package versions:
88

99
## Unreleased
1010

11+
- Adds: RESP3 support ([#2396 by mgravell](https://github.com/StackExchange/StackExchange.Redis/pull/2396)) - see https://stackexchange.github.io/StackExchange.Redis/Resp3
1112
- Fix [#2507](https://github.com/StackExchange/StackExchange.Redis/issues/2507): Pub/sub with multi-item payloads should be usable ([#2508 by mgravell](https://github.com/StackExchange/StackExchange.Redis/pull/2508))
1213
- Add: connection-id tracking (internal only, no public API) ([#2508 by mgravell](https://github.com/StackExchange/StackExchange.Redis/pull/2508))
1314
- Add: `ConfigurationOptions.LoggerFactory` for logging to an `ILoggerFactory` (e.g. `ILogger`) all connection and error events ([#2051 by NickCraver](https://github.com/StackExchange/StackExchange.Redis/pull/2051))

docs/Resp3.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# RESP3 and StackExchange.Redis
2+
3+
RESP2 and RESP3 are evolutions of the Redis protocol, with RESP3 existing from Redis server version 6 onwards (v7.2+ for Redis Enterprise). The main differences are:
4+
5+
1. RESP3 can carry out-of-band / "push" messages on a single connection, where-as RESP2 requires a separate connection for these messages
6+
2. RESP3 can (when appropriate) convey additional semantic meaning about returned payloads inside the same result structure
7+
3. Some commands (see [this topic](https://github.com/redis/redis-doc/issues/2511)) return different result structures in RESP3 mode; for example a flat interleaved array might become a jagged array
8+
9+
For most people, #1 is the main reason to consider RESP3, as in high-usage servers - this can halve the number of connections required.
10+
This is particularly useful in hosted environments where the number of inbound connections to the server is capped as part of a service plan.
11+
Alternatively, where users are currently choosing to disable the out-of-band connection to achieve this, they may now be able to re-enable this
12+
(for example, to receive server maintenance notifications) *without* incurring any additional connection overhead.
13+
14+
Because of the significance of #3 (and to avoid breaking your code), the library does not currently default to RESP3 mode. This must be enabled explicitly
15+
via `ConfigurationOptions.Protocol` or by adding `,protocol=resp3` (or `,protocol=3`) to the configuration string.
16+
17+
---
18+
19+
#3 is a critical one - the library *should* already handle all documented commands that have revised results in RESP3, but if you're using
20+
`Execute[Async]` to issue ad-hoc commands, you may need to update your processing code to compensate for this, ideally using detection to handle
21+
*either* format so that the same code works in both REP2 and RESP3. Since the impacted commands are handled internally by the library, in reality
22+
this should not usually present a difficulty.
23+
24+
The minor (#2) and major (#3) differences to results are only visible to your code when using:
25+
26+
- Lua scripts invoked via the `ScriptEvaluate[Async](...)` or related APIs, that either:
27+
- Uses the `redis.setresp(3)` API and returns a value from `redis.[p]call(...)`
28+
- Returns a value that satisfies the [LUA to RESP3 type conversion rules](https://redis.io/docs/manual/programmability/lua-api/#lua-to-resp3-type-conversion)
29+
- Ad-hoc commands (in particular: *modules*) that are invoked via the `Execute[Async](string command, ...)` API
30+
31+
...both which return `RedisResult`. **If you are not using these APIs, you should not need to do anything additional.**
32+
33+
Historically, you could use the `RedisResult.Type` property to query the type of data returned (integer, string, etc). In particular:
34+
35+
- Two new properties are added: `RedisResult.Resp2Type` and `RedisResult.Resp3Type`
36+
- The `Resp3Type` property exposes the new semantic data (when using RESP3) - for example, it can indicate that a value is a double-precision number, a boolean, a map, etc (types that did not historically exist)
37+
- The `Resp2Type` property exposes the same value that *would* have been returned if this data had been returned over RESP2
38+
- The `Type` property is now marked obsolete, but functions identically to `Resp2Type`, so that pre-existing code (for example, that has a `switch` on the type) is not impacted by RESP3
39+
- The `ResultType.MultiBulk` is superseded by `ResultType.Array` (this is a nomenclature change only; they are the same value and function identically)
40+
41+
Possible changes required due to RESP3:
42+
43+
1. To prevent build warnings, replace usage of `ResultType.MultiBulk` with `ResultType.Array`, and usage of `RedisResult.Type` with `RedisResult.Resp2Type`
44+
2. If you wish to exploit the additional semantic data when enabling RESP3, use `RedisResult.Resp3Type` where appropriate
45+
3. If you are enabling RESP3, you must verify whether the commands you are using can give different result shapes on RESP3 connections

docs/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ Documentation
3939
- [Transactions](Transactions) - how atomic transactions work in redis
4040
- [Events](Events) - the events available for logging / information purposes
4141
- [Pub/Sub Message Order](PubSubOrder) - advice on sequential and concurrent processing
42+
- [Using RESP3](Resp3) - information on using RESP3
4243
- [ServerMaintenanceEvent](ServerMaintenanceEvent) - how to listen and prepare for hosted server maintenance (e.g. Azure Cache for Redis)
4344
- [Streams](Streams) - how to use the Stream data type
4445
- [Where are `KEYS` / `SCAN` / `FLUSH*`?](KeysScan) - how to use server-based commands

src/StackExchange.Redis/APITypes/LatencyHistoryEntry.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ private sealed class Processor : ArrayResultProcessor<LatencyHistoryEntry>
1313
{
1414
protected override bool TryParse(in RawResult raw, out LatencyHistoryEntry parsed)
1515
{
16-
if (raw.Type == ResultType.MultiBulk)
16+
if (raw.Resp2TypeArray == ResultType.Array)
1717
{
1818
var items = raw.GetItems();
1919
if (items.Length >= 2

src/StackExchange.Redis/APITypes/LatencyLatestEntry.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ private sealed class Processor : ArrayResultProcessor<LatencyLatestEntry>
1313
{
1414
protected override bool TryParse(in RawResult raw, out LatencyLatestEntry parsed)
1515
{
16-
if (raw.Type == ResultType.MultiBulk)
16+
if (raw.Resp2TypeArray == ResultType.Array)
1717
{
1818
var items = raw.GetItems();
1919
if (items.Length >= 4

src/StackExchange.Redis/ChannelMessageQueue.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.Reflection;
43
using System.Threading;
54
using System.Threading.Channels;
65
using System.Threading.Tasks;
6+
#if NETCOREAPP3_1
7+
using System.Reflection;
8+
#endif
79

810
namespace StackExchange.Redis
911
{
@@ -125,6 +127,7 @@ public ValueTask<ChannelMessage> ReadAsync(CancellationToken cancellationToken =
125127
/// <param name="count">The (approximate) count of items in the Channel.</param>
126128
public bool TryGetCount(out int count)
127129
{
130+
// This is specific to netcoreapp3.1, because full framework was out of band and the new prop is present
128131
#if NETCOREAPP3_1
129132
// get this using the reflection
130133
try

src/StackExchange.Redis/ClientInfo.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -181,18 +181,23 @@ public ClientType ClientType
181181
}
182182

183183
/// <summary>
184-
/// Client RESP protocol version. Added in Redis 7.0
184+
/// Client RESP protocol version. Added in Redis 7.0.
185185
/// </summary>
186186
public string? ProtocolVersion { get; private set; }
187187

188188
/// <summary>
189-
/// Client library name. Added in Redis 7.2
189+
/// Client RESP protocol version. Added in Redis 7.0.
190+
/// </summary>
191+
public RedisProtocol? Protocol => ConfigurationOptions.TryParseRedisProtocol(ProtocolVersion, out var value) ? value : null;
192+
193+
/// <summary>
194+
/// Client library name. Added in Redis 7.2.
190195
/// </summary>
191196
/// <remarks><seealso href="https://redis.io/commands/client-setinfo"/></remarks>
192197
public string? LibraryName { get; private set; }
193198

194199
/// <summary>
195-
/// Client library version. Added in Redis 7.2
200+
/// Client library version. Added in Redis 7.2.
196201
/// </summary>
197202
/// <remarks><seealso href="https://redis.io/commands/client-setinfo"/></remarks>
198203
public string? LibraryVersion { get; private set; }
@@ -280,7 +285,7 @@ private class ClientInfoProcessor : ResultProcessor<ClientInfo[]>
280285
{
281286
protected override bool SetResultCore(PhysicalConnection connection, Message message, in RawResult result)
282287
{
283-
switch(result.Type)
288+
switch(result.Resp2TypeBulkString)
284289
{
285290
case ResultType.BulkString:
286291
var raw = result.GetString();

0 commit comments

Comments
 (0)