Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion docs/Failover.md
Original file line number Diff line number Diff line change
Expand Up @@ -581,7 +581,7 @@ IDatabaseAsync db = conn.GetDatabase().WithRetry();
var value = await db.StringGetAsync("mykey");
```

> You can call `WithRetry` on any database (`IDatabase` or `IDatabaseAsync`), but the wrapper it returns exposes only the **async** API — there is no synchronous form, since retrying may inherently have delays. It cannot wrap a batch or an existing transaction, nor an already-retrying database.
> You can call `WithRetry` on any database (`IDatabase` or `IDatabaseAsync`), but the wrapper it returns exposes only the **async** API — there is no synchronous form, since retrying may inherently have delays. It cannot wrap a batch or an existing transaction, nor an already-retrying database (see [Batches](#batches) for what to do instead).

> **`asyncState` is not respected.** A database's `asyncState` is stamped onto the task produced by a single dispatch, but a retrying database hands back its own task spanning however many attempts the operation takes, and the per-operation tasks from `retryDb.CreateTransaction()` are durable proxies that outlive any single attempt. Neither can carry it. Rather than dropping the state silently, both refuse it: `conn.GetDatabase(0, asyncState).WithRetry(policy)` throws `InvalidOperationException`, as does `retryDb.CreateTransaction(asyncState)`. Wrap a database obtained without an `asyncState` - note that a key-prefixed view of a state-carrying database is refused too, since it inherits the inner state.

Expand All @@ -602,6 +602,28 @@ In either case every per-operation task is cancelled, so `await`ing one throws `

How likely is this in practice? Much less so than in `redis-cli`-style usage, where a `WATCH` can sit open for as long as the application takes to think. SE.Redis buffers the whole transaction and sends the `WATCH`, the condition reads, the `MULTI`, the queued commands and the `EXEC` as a single dispatch on a multiplexed connection, so a competing writer has only the gap between the condition reads and the `EXEC` landing to squeeze into. Small - but not zero, and on a busy key it will happen.

#### Batches

There is no retrying batch: a retrying database has no `CreateBatch()`, and `WithRetry` refuses a batch. A batch guarantees one thing beyond a series of commands: they are written as one uninterrupted run, with nothing from other callers interleaved. Retrying would break that. A batch cannot be replayed as a unit the way a transaction can, because some of its commands may already have been applied. And re-issuing just the element that failed would run it *after* the commands that followed it, so a batch whose commands depend on each other, or on their order, could end up somewhere a single run never would. The batch itself is not what makes a round trip cheap, either: the multiplexer pipelines everything it is given, so commands issued together share round trips whether or not they are batched.

So pick by what the commands need from each other:

- **Independent commands** (for example, several reads issued together): issue them on the retrying database without awaiting each one in turn. They are pipelined just as a batch's would be, and each is retried, or not, according to its own [retry category](#which-operations-are-safe-to-retry).

```csharp
IDatabaseAsync db = conn.GetDatabase().WithRetry();

// not awaited one by one, so these are pipelined; each is retried on its own
var a = db.StringGetAsync("key-a");
var b = db.StringGetAsync("key-b");
var c = db.HashGetAllAsync("hash-c");
await Task.WhenAll(a, b, c);
```

This applies equally to a module wrapper that issues its commands through an `IDatabaseAsync` (NRedisStack's search and JSON commands, for example): hand it the retrying database rather than a batch.
- **Commands that must apply together, or not at all:** use `retryDb.CreateTransaction()`, described above. It is retried as a single unit, replayed against a fresh `MULTI`/`EXEC`. A transaction with no conditions is the nearest thing to a retryable batch. In a cluster, as with any transaction, its keys must all hash to the same slot.
- **Commands that need a batch's uninterrupted run, but not atomicity:** use a batch on the plain (non-retrying) database, and handle faults yourself.

### Configuring the retry policy

Like the health check and the circuit breaker, `RetryPolicy` can be set as a group-wide default - and the parameterless `WithRetry()` picks it up, so callers don't have to thread a policy through their code:
Expand Down
Loading