Redis Storage Provider

Ultra-fast in-memory caching with real-time notifications and streaming capabilities.

Lightning Fast Performance

Sub-ms LatencyAuto ChunkingPub/Sub EventsRedis StreamsCluster Support

Installation

dotnet add package Zetian
dotnet add package Zetian.Storage.Redis

Quick Start

QuickStart.cs
using Zetian.Server;
using Zetian.Storage.Redis.Extensions;

// Basic Redis cache setup
var server = new SmtpServerBuilder()
    .Port(25)
    .WithRedisStorage("localhost:6379")
    .Build();

await server.StartAsync();

Advanced Configuration

AdvancedConfig.cs
var server = new SmtpServerBuilder()
    .Port(25)
    .WithRedisStorage(
        "localhost:6379,password=secret,ssl=false",
        config =>
        {
            config.DatabaseNumber = 1;
            config.KeyPrefix = "smtp:msg:";
            
            // TTL for auto-expiration
            config.MessageTTLSeconds = 3600; // 1 hour
            
            // Chunking for large messages
            config.EnableChunking = true;
            config.ChunkSizeKB = 64;
            
            // Real-time features
            config.UseRedisStreams = true;
            config.StreamKey = "smtp:events";
            config.EnablePubSub = true;
            config.PubSubChannel = "smtp:notifications";
            
            // Indexing
            config.MaintainIndex = true;
            config.IndexKey = "smtp:index";
            
            // Compression
            config.CompressMessageBody = true;
        })
    .Build();

Automatic Chunking

Large messages are automatically split into manageable chunks:

Chunking.cs
// Large messages are automatically split into chunks
// This prevents Redis memory fragmentation

// Message stored as:
// smtp:msg:123456 -> metadata
// smtp:msg:123456:chunk:0 -> first 64KB
// smtp:msg:123456:chunk:1 -> second 64KB
// smtp:msg:123456:chunk:2 -> third 64KB

// Retrieval automatically reassembles chunks
var fullMessage = await store.GetMessageAsync(messageId);

Smart Splitting

Configurable chunk size

Auto Assembly

Transparent to client

Memory Efficient

Prevents fragmentation

Real-time Pub/Sub

Get instant notifications when messages arrive:

PubSub.cs
// Real-time notifications with Pub/Sub
var subscriber = redis.GetSubscriber();

// Subscribe to message events
await subscriber.SubscribeAsync("smtp:notifications", (channel, value) =>
{
    var notification = JsonSerializer.Deserialize<MessageNotification>(value);
    Console.WriteLine($"New message: {notification.MessageId} from {notification.From}");
});

// Publish notification (done automatically by storage)
await subscriber.PublishAsync("smtp:notifications", JsonSerializer.Serialize(new
{
    Event = "message_received",
    MessageId = "ABC123",
    From = "[email protected]",
    Timestamp = DateTime.UtcNow
}));

Redis Streams

Event sourcing with Redis Streams:

Streams.sh
// Redis Streams for event sourcing
// Automatically creates event stream

XADD smtp:events * event message_received id ABC123 from [email protected]
XADD smtp:events * event message_stored id ABC123 size 45678
XADD smtp:events * event message_deleted id ABC123

// Read stream events
XREAD STREAMS smtp:events 0

// Consumer groups for processing
XGROUP CREATE smtp:events processors $
XREADGROUP GROUP processors consumer1 STREAMS smtp:events >

Configuration Options

OptionDefaultDescription
DatabaseNumber0Redis database (0-15)
KeyPrefix"smtp:"Key namespace prefix
MessageTTLSeconds3600Message expiration time
EnableChunkingtrueSplit large messages
ChunkSizeKB64Chunk size in KB
UseRedisStreamsfalseEnable Redis Streams
EnablePubSubfalseEnable Pub/Sub events

When to Use Redis

✅ Perfect For

  • • Recent message cache
  • • Real-time notifications
  • • Temporary message queue
  • • High-frequency access patterns
  • • Session storage

⚠️ Consider Alternatives

  • • Long-term archival
  • • Very large attachments
  • • Complex queries needed
  • • Persistent storage required
  • • Limited memory available

Performance Tips

•

Use TTL for automatic cleanup

Prevents memory bloat

•

Enable connection pooling

Reuse connections for better performance

•

Consider Redis Cluster for scaling

Horizontal scaling for high loads

•

Use pipelining for batch operations

Reduce network round-trips