Fluens.Messaging.Redis
0.9.2
See the version list below for details.
dotnet add package Fluens.Messaging.Redis --version 0.9.2
NuGet\Install-Package Fluens.Messaging.Redis -Version 0.9.2
<PackageReference Include="Fluens.Messaging.Redis" Version="0.9.2" />
<PackageVersion Include="Fluens.Messaging.Redis" Version="0.9.2" />
<PackageReference Include="Fluens.Messaging.Redis" />
paket add Fluens.Messaging.Redis --version 0.9.2
#r "nuget: Fluens.Messaging.Redis, 0.9.2"
#:package Fluens.Messaging.Redis@0.9.2
#addin nuget:?package=Fluens.Messaging.Redis&version=0.9.2
#tool nuget:?package=Fluens.Messaging.Redis&version=0.9.2
Fluens.Messaging.Redis
Instance broadcast over Redis pub/sub for Fluens.Messaging: a message consumed once through the mesh and the inbox is handed to every live replica of the application, so in-process state (caches, local projections) can be invalidated on each replica.
Installation
dotnet add package Fluens.Messaging.Redis
Usage
There is no IFluensBuilder extension for Redis. The only opt-in is the nested callback on AddMessaging:
fluensBuilder.AddMessaging(o => o.AddRedisBroadcast());
AddRedisBroadcast binds Fluens:Messaging:Redis, applies the optional callback, validates the result and registers the broadcast transport, the broadcast dispatcher, the gap notifier and two hosted services (a startup check and the subscriber). It is a no-op when Enabled is false. Calling it twice registers each service once.
fluensBuilder.AddMessaging(o => o.AddRedisBroadcast(r =>
{
r.ConnectionString = "localhost:6379";
}));
Replicas of one application also need a cluster identity: set Fluens:Web:Discovery:ClusterId to a non-blank value and call AddDiscovery(). A non-blank ClusterId registers IClusterIdentity, which the channel name is built from. A missing IClusterIdentity fails host start with an InvalidOperationException that names the setting.
Configuration
Section: Fluens:Messaging:Redis.
{
"Fluens": {
"Messaging": {
"Redis": {
"Enabled": true,
"ConnectionString": "localhost:6379",
"ChannelPrefix": "fluens",
"DeduplicationWindowSeconds": 600,
"DeduplicationCapacity": 10000
}
}
}
}
| Property | Type | Default | Description |
|---|---|---|---|
Enabled |
bool |
true |
When false, AddRedisBroadcast registers nothing. |
ConnectionString |
string? |
null |
Redis connection string. Used only when no IConnectionMultiplexer is registered in DI; then it is required. |
ChannelPrefix |
string |
"fluens" |
First segment of the broadcast channel name. Must not be blank. |
DeduplicationWindowSeconds |
int |
600 |
How long a received message id is remembered per module. Must be greater than 0. |
DeduplicationCapacity |
int |
10000 |
Maximum number of message ids remembered for deduplication. Must be greater than 0. |
An invalid instance throws OptionsException<RedisBroadcastOptions>. A missing connection string throws the same exception when no multiplexer is registered in DI.
Instance consumers
The broadcast is driven by IInstanceMessageConsumer<TMessage> in Fluens.Messaging. Register the consumer with SubscribesFromAssembly as for plain consumers; see the Fluens.Messaging README for the consumer contract, the relay inbox row and the deployment scope.
Connection reuse and channel naming
- If an
IConnectionMultiplexeris registered in DI, the broadcast publishes and subscribes through it. - Otherwise one multiplexer is connected lazily from
ConnectionString, once, and shared by the publisher and the subscriber. It is disposed with the host. - The channel is
{ChannelPrefix}:{ClusterId}:{ApplicationKey}:broadcast, for examplefluens:cluster-1:orders:broadcast. Each replica of an application subscribes to the same channel; a different cluster or application never shares it.
Delivery semantics
- The message reaches one replica through the mesh and that replica's inbox, which stay durable and deduplicated. The broadcast from the inbox row is then best effort.
- The relay publishes the frame and acknowledges the inbox row after Redis accepts the publish. A transport failure leaves the row to the standard retry and dead-letter path. A publish that reached no subscriber is logged as a warning and still acknowledged.
- Each replica deduplicates received frames by
(message id, module)within the window and capacity above, so a frame delivered twice is handled once per replica. - A consumer that throws is logged and counted, is not retried, and does not stop the other consumers of the same frame.
- Frames are handled one at a time per replica, in the order Redis delivers them.
Gap handling
The subscriber watches the subscription connection. When it is lost and then restored, the interval between the loss and the restore is a subscription gap. Every replica reports the gap to every IInstanceSubscriptionGapHandler it has registered. A gap handler is an instance-consumer type that also implements IInstanceSubscriptionGapHandler, so the module scan picks it up.
public sealed class CatalogGapHandler : IInstanceMessageConsumer<CatalogChangedMessage>, IInstanceSubscriptionGapHandler
{
public Task ConsumeAsync(CatalogChangedMessage message, InstanceMessageContext context, CancellationToken cancellationToken) { /* invalidate */ return Task.CompletedTask; }
public Task OnSubscriptionGapAsync(SubscriptionGap gap, CancellationToken cancellationToken) { /* drop the local state */ return Task.CompletedTask; }
}
Limitation: gap detection keys on the dedicated subscription connection. StackExchange.Redis 3.x negotiates RESP3 by default and still opens that dedicated connection under both RESP2 and RESP3. A multiplexer with SharedSubscriptionConnection enabled carries pub/sub on the interactive connection and reports no gaps; do not rely on gap handlers with that setting.
Startup
- Startup connects to Redis and returns only once Redis has confirmed the subscription. An unreachable Redis therefore fails host start.
- Registering the broadcast without a cluster identity or without a multiplexer or connection string fails host start.
- Starting a host with instance consumers but without a broadcast transport fails start with a message naming
AddRedisBroadcast().
Metrics
The broadcast records these instruments on the Fluens.Messaging meter. Add that meter to ObservabilityOptions.AdditionalMeters to export them.
| Instrument | Type | Tags |
|---|---|---|
broadcast.published |
counter | module, message_type, has_receivers |
broadcast.received |
counter | module, message_type |
broadcast.duplicate |
counter | module, message_type |
broadcast.handled |
counter | module, handler_type |
broadcast.failed |
counter | module, handler_type, stage (decode, handle or gap) |
broadcast.subscription_gap |
counter | none |
Constraints
- Best effort. Redis pub/sub does not persist or replay. A replica that is down or disconnected misses the broadcast, and nothing re-sends it. The gap handler tells the replica that it may have missed messages; it does not recover them.
- Derived or expiring state only. Use instance consumers for state that can be rebuilt or that expires on its own: caches, local projections. Do not use them for state that must be exact or durable. The durable path is the mesh and the inbox, not the broadcast.
- One messaging database per replica. Each replica of an application owns its own messaging database (its own
inbox,outboxand related tables). Replicas share the application identity key, so the mesh sees one application. Replicas must not point at the same messaging database.
License
This project is licensed under the MIT License.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net10.0
- Fluens.Messaging (>= 0.9.2)
- Microsoft.EntityFrameworkCore.Relational (>= 10.0.13)
- Microsoft.Extensions.Configuration.Abstractions (>= 10.0.13)
- Microsoft.Extensions.Configuration.Binder (>= 10.0.13)
- Microsoft.Extensions.DependencyInjection (>= 10.0.13)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 10.0.13)
- Microsoft.Extensions.Hosting.Abstractions (>= 10.0.13)
- Microsoft.Extensions.Logging (>= 10.0.13)
- Microsoft.Extensions.Logging.Abstractions (>= 10.0.13)
- Microsoft.Extensions.Options.ConfigurationExtensions (>= 10.0.13)
- Scrutor (>= 7.0.0)
- StackExchange.Redis (>= 3.4.5)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.