DEV Community

Cover image for Brighter and Redis/Valkey: How to setup and use Brighter with Redis/Valkey
Rafael Andrade
Rafael Andrade

Posted on

Brighter and Redis/Valkey: How to setup and use Brighter with Redis/Valkey

Introduction

Redis is an in-memory data structure store, commonly used as a message broker, cache, or database. It has support to queue and you can integrate with that using the Paramore.Brighter.MessagingGateway.Redis package.

Requirement

Brighter Recap

Before continuing about Redis configuration, let's recap what we already know about Brighter.

Request (Command/Event)

Define messages using IRequest:

public class Greeting() : Event(Guid.NewGuid())
{
    public string Name { get; set; } = string.Empty;
}
Enter fullscreen mode Exit fullscreen mode
  • Commands: Single-recipient operations (e.g., SendEmail).
  • Events: Broadcast notifications (e.g., OrderShipped).

Message Mapper

Translates between Brighter messages and your app objects:

public class GreetingMapper : IAmAMessageMapper<Greeting>
{
    public Message MapToMessage(Greeting request)
    {
        var header = new MessageHeader();
        header.Id = request.Id; 
        header.TimeStamp = DateTime.UtcNow;
        header.Topic = "greeting.topic"; // The target topic to be publish
        header.MessageType = MessageType.MT_EVENT;

        var body = new MessageBody(JsonSerializer.Serialize(request));
        return new Message(header, body);
    }

    public Greeting MapToRequest(Message message)
    {
        return JsonSerializer.Deserialize<Greeting>(message.Body.Bytes)!;
    }
}
Enter fullscreen mode Exit fullscreen mode

Request Handler

Processes incoming messages:

public class GreetingHandler(ILogger<GreetingHandler> logger) : RequestHandler<Greeting>
{
    public override Greeting Handle(Greeting command)
    {
        logger.LogInformation("Hello {Name}", command.Name);
        return base.Handle(command);
    }
}
Enter fullscreen mode Exit fullscreen mode

Configuring Brighter with Redis

1. Connection Setup

Define Redis connection details:

var connection = new RedisMessagingGatewayConfiguration
{
    RedisConnectionString = "localhost:6379?connectTimeout=1000&sendTimeout=1000&",
    MaxPoolSize = 10,
    MessageTimeToLive = TimeSpan.FromMinutes(10)
};
Enter fullscreen mode Exit fullscreen mode

2. Redis Subscription

Subscribe to a queue:

.AddServiceActivator(opt =>
{
    opt.Subscriptions = [
       new RedisSubscription<Greeting>(
           new SubscriptionName("kafka.greeting.subscription"),
           new ChannelName("greeting.queue"),
           new RoutingKey("greeting.topic"),
           makeChannels: OnMissingChannel.Create
       ),
    ];

    opt.ChannelFactory = new ChannelFactory(
        new RedisMessageConsumerFactory(connection)
    );
})
Enter fullscreen mode Exit fullscreen mode

3. Redis Producer Configuration

Publish events to a queue:

.UseExternalBus(new RedisProducerRegistryFactory(connection, [
    new RedisMessagePublication
    {
        Topic = new RoutingKey("greeting.topic"),
        MakeChannels = OnMissingChannel.Create
    }
}).Create());
Enter fullscreen mode Exit fullscreen mode

Limitations and Considerations

1. ServiceStack.Redis Licensing

Brighter's Redis integration relies on ServiceStack.Redis.Core, which imposes a concurrent message consumption limit without a commercial license. This can bottleneck high-throughput systems.

2. No Redis Streams Support (Yet):

Brighter currently uses Redis lists for queuing, not the more modern Redis Streams (which offers persistence and stream processing). A future update will add Redis Streams support for better scalability.

Conclusion

Integrating Brighter with Redis enables lightweight, scalable messaging in .NET applications. By leveraging Redis’ in-memory speed and Brighter's abstraction layer.

For production use, validate Redis configurations against Brighter's latest documentation, as support for Redis Streams and alternative clients may evolve.

Reference

Github Code

Top comments (0)