Skip to content

MinChanSike/RedisRpc

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

2 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Redis RPC Sample

A complete implementation of a service-to-service communication mechanism using Redis Pub/Sub for RPC-style request/response patterns. This solution is designed for multi-instance service environments with robust error handling and comprehensive JSON serialization.

πŸš€ Features

  • Redis Pub/Sub Communication: Uses Redis as the messaging backbone for inter-service communication
  • RPC-Style Pattern: Supports both request/response and fire-and-forget notification patterns
  • JSON Serialization: All messages are serialized using Newtonsoft.Json with consistent formatting
  • Robust Error Handling: Comprehensive error handling with structured error responses (no automatic retries)
  • Multi-Instance Support: Designed to work across multiple service instances
  • Thread-Safe: All components are designed for concurrent usage
  • Type-Safe: Generic methods for strongly-typed responses
  • Extensive Documentation: Comprehensive inline documentation and examples

πŸ“‹ Requirements Met

βœ… C# Implementation: Complete C# solution with .NET 9.0
βœ… JSON Serialization: Uses Newtonsoft.Json for all message payloads
βœ… Robust Error Handling: Structured error responses without automatic retries
βœ… Multi-Instance Support: Works with multiple service instances via Redis channels
βœ… Documentation: Extensive inline documentation and usage examples

πŸ—οΈ Architecture

Core Components

  1. RpcClient - Sends RPC requests and handles responses
  2. RpcServer - Listens for requests and processes them using registered handlers
  3. BaseRpcMethodHandler - Base class for easy method handler implementation
  4. JsonSerializer - Consistent JSON serialization/deserialization
  5. RedisConnectionManager - Thread-safe Redis connection management

Message Flow

Client                    Redis                    Server
  |                        |                        |
  |-- RPC Request -------->|                        |
  |                        |-- Request -----------> |
  |                        |                        |-- Process
  |                        |<-- Response -----------|
  |<-- RPC Response -------|                        |

πŸ› οΈ Setup & Installation

Prerequisites

  • .NET 9.0 or later
  • Redis server (local or remote)
  • Visual Studio 2022 or VS Code (optional)

Quick Start

  1. Clone and Build

    git clone <repository-url>
    cd redis-rpc-sample
    dotnet build
  2. Start Redis (if not already running)

    # Using Docker
    docker run -d -p 6379:6379 redis:alpine
    
    # Or install Redis locally and run
    redis-server
  3. Run the Example

    # Full demonstration (server + client)
    dotnet run --project RedisRpc.Example
    
    # Server only
    dotnet run --project RedisRpc.Example server
    
    # Client only (assumes server running elsewhere)
    dotnet run --project RedisRpc.Example client

πŸ“– Usage Examples

Creating an RPC Server

using RedisRpc.Core;
using RedisRpc.Core.Implementation;

// Create server with default options
using var server = RpcFactory.CreateServer();

// Register method handlers
server.RegisterHandler(new CalculatorService());
server.RegisterHandler(new GreetingService());

// Start listening on channels
await server.StartListeningAsync(new[] { "calculator", "greeting" });

// Keep server running
Console.ReadKey();

Creating Method Handlers

public class CalculatorService : BaseRpcMethodHandler
{
    [RpcMethod("Add", Description = "Adds two numbers together")]
    public int Add(int a, int b)
    {
        return a + b;
    }

    [RpcMethod("Divide", Description = "Divides two numbers")]
    public double Divide(double a, double b)
    {
        if (Math.Abs(b) < double.Epsilon)
        {
            throw new InvalidParametersException("Division by zero is not allowed");
        }
        return a / b;
    }
}

Making RPC Calls

using RedisRpc.Core;

// Create client
using var client = RpcFactory.CreateClient();

// Typed request/response
var result = await client.SendRequestAsync<int>("calculator", "Add",
    new { a = 10, b = 5 });

// Untyped request/response
var response = await client.SendRequestAsync("greeting", "SayHello", "Alice");

// Fire-and-forget notification
await client.SendNotificationAsync("data", "LogActivity",
    new { activity = "User login", userId = 123 });

Advanced Configuration

var options = new RedisRpcOptions
{
    ConnectionString = "localhost:6379",
    DefaultTimeoutMs = 15000,
    MaxConcurrentRequests = 200,
    ChannelPrefix = "myapp-rpc",
    IncludeStackTraceInErrors = false, // Production setting
    Database = 0
};

using var client = RpcFactory.CreateClient(options);
using var server = RpcFactory.CreateServer(options);

πŸ”§ Configuration Options

Option Description Default
ConnectionString Redis connection string "localhost:6379"
DefaultTimeoutMs Default request timeout in milliseconds 30000
MaxConcurrentRequests Maximum concurrent requests for server 100
ChannelPrefix Prefix for all Redis channels "redis-rpc"
IncludeStackTraceInErrors Include stack traces in error responses false
Database Redis database index to use 0

πŸ§ͺ Example Services

The solution includes three comprehensive example services:

1. CalculatorService

  • Mathematical operations (Add, Subtract, Multiply, Divide, Power, SquareRoot)
  • Expression evaluation
  • Batch operations
  • Statistics tracking

2. GreetingService

  • Multi-language greetings
  • Personalized messages
  • Time-based greetings
  • Name formatting

3. DataService

  • CRUD operations for users
  • Data processing with various operations
  • Activity logging
  • Caching functionality

⚠️ Error Handling

The system provides structured error handling with specific error codes:

public enum RpcErrorCode
{
    Unknown = 0,
    MethodNotFound = 1001,
    InvalidParameters = 1002,
    InternalError = 1003,
    Timeout = 1004,
    SerializationError = 1005,
    ConnectionError = 1006
}

Example error response:

{
  "id": "req-123",
  "success": false,
  "error": {
    "code": 1001,
    "message": "Method 'NonExistentMethod' not found",
    "details": {
      "methodName": "NonExistentMethod"
    }
  },
  "timestamp": "2024-01-01T12:00:00.000Z"
}

πŸ”’ Production Considerations

Security

  • Consider Redis authentication and SSL/TLS
  • Validate and sanitize all input parameters
  • Use appropriate error detail levels for production

Performance

  • Monitor Redis memory usage and performance
  • Consider Redis clustering for high availability
  • Implement circuit breakers for external dependencies

Monitoring

  • Add structured logging (Serilog, NLog, etc.)
  • Implement health checks
  • Monitor message throughput and latency

Configuration

// Production configuration example
var prodOptions = RpcFactory.CreateProductionOptions("your-redis-connection-string");
prodOptions.IncludeStackTraceInErrors = false;
prodOptions.DefaultTimeoutMs = 15000;

πŸ§ͺ Testing

The solution can be tested in several ways:

  1. Run full demo: dotnet run --project RedisRpc.Example
  2. Server only: dotnet run --project RedisRpc.Example server
  3. Client only: dotnet run --project RedisRpc.Example client

Testing Scenarios Covered

  • βœ… Successful request/response cycles
  • βœ… Method not found errors
  • βœ… Invalid parameter handling
  • βœ… Division by zero and other business logic errors
  • βœ… Fire-and-forget notifications
  • βœ… Complex object serialization/deserialization
  • βœ… Multi-parameter method calls
  • βœ… Timeout handling (configurable)

πŸ“¦ Project Structure

redis-rpc-sample/
β”œβ”€β”€ RedisRpc.Core/                 # Core library
β”‚   β”œβ”€β”€ Models/                    # Request/Response/Error models
β”‚   β”œβ”€β”€ Interfaces/               # Service interfaces
β”‚   β”œβ”€β”€ Implementation/           # Core implementations
β”‚   β”œβ”€β”€ Infrastructure/           # Redis connection, serialization
β”‚   β”œβ”€β”€ Exceptions/              # Custom exceptions
β”‚   └── RpcFactory.cs            # Factory for easy setup
β”œβ”€β”€ RedisRpc.Example/            # Example application
β”‚   β”œβ”€β”€ Services/               # Example service implementations
β”‚   └── Program.cs              # Demonstration program
└── README.md                   # This file

🀝 Contributing

This is a sample implementation demonstrating Redis-based RPC patterns. Feel free to extend it with:

  • Authentication and authorization
  • Request/response middleware
  • Circuit breaker patterns
  • Metrics and monitoring
  • Additional serialization formats
  • Retry mechanisms (if needed)

πŸ“„ License

This sample is provided as-is for educational and demonstration purposes.


Note: Make sure Redis is running before starting the application. The examples assume Redis is available at localhost:6379.

About

Demonstrate RPC with Redis Pub/Sub

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages