This document provides detailed information about the Redis RPC API, including all available methods, parameters, and response formats.
The client interface for sending RPC requests.
Sends a typed RPC request and waits for a response.
Parameters:
channel(string): Redis channel to send the request tomethod(string): Method name to invokeparameters(object): Parameters for the method (optional)timeoutMs(int?): Optional timeout in millisecondscancellationToken(CancellationToken): Cancellation token (optional)
Returns: Task<T?> - Deserialized response of type T
Example:
var result = await client.SendRequestAsync<int>("calculator", "Add", new { a = 5, b = 3 });Sends an untyped RPC request and waits for a response.
Parameters: Same as above
Returns: Task<object?> - Raw response object
Example:
var result = await client.SendRequestAsync("greeting", "SayHello", "Alice");Sends a fire-and-forget notification (no response expected).
Parameters:
channel(string): Redis channel to send the notification tomethod(string): Method name to invokeparameters(object): Parameters for the method (optional)cancellationToken(CancellationToken): Cancellation token (optional)
Returns: Task - Completes when the notification is sent
Example:
await client.SendNotificationAsync("data", "LogActivity",
new { activity = "User login", userId = 123 });The server interface for handling RPC requests.
Registers a method handler for processing RPC requests.
Parameters:
handler(IRpcMethodHandler): The handler to register
Example:
server.RegisterHandler(new CalculatorService());Starts listening for RPC requests on a single channel.
Parameters:
channel(string): Redis channel to listen oncancellationToken(CancellationToken): Cancellation token to stop listening
Starts listening for RPC requests on multiple channels.
Parameters:
channels(IEnumerable): Redis channels to listen oncancellationToken(CancellationToken): Cancellation token to stop listening
Example:
await server.StartListeningAsync(new[] { "calculator", "greeting", "data" }, cancellationToken);Stops listening on all channels.
Returns: Task - Completes when all channels are unsubscribed
Interface for implementing RPC method handlers.
Gets the collection of method names that this handler can process.
Returns: IEnumerable<string>
Handles an RPC method invocation asynchronously.
Parameters:
method(string): The name of the method to invokeparameters(object?): The parameters for the method invocationcancellationToken(CancellationToken): Cancellation token for the operation
Returns: Task<object?> - The result of the method invocation
Represents an RPC request message.
{
"id": "550e8400-e29b-41d4-a716-446655440000",
"method": "Add",
"parameters": {
"a": 10,
"b": 5
},
"responseChannel": "redis-rpc:response:machine:1234:abc123",
"timestamp": "2024-01-01T12:00:00.000Z",
"timeoutMs": 30000
}Fields:
id(string): Unique identifier for request/response correlationmethod(string): Method name to invokeparameters(object): Method parameters (can be null)responseChannel(string): Channel for response (empty for notifications)timestamp(string): ISO 8601 timestamp when request was createdtimeoutMs(int?): Optional timeout in milliseconds
Represents an RPC response message.
{
"id": "550e8400-e29b-41d4-a716-446655440000",
"success": true,
"result": 15,
"timestamp": "2024-01-01T12:00:01.000Z"
}Success Response Fields:
id(string): Correlation ID matching the requestsuccess(boolean): Always true for successful responsesresult(object): The method result (can be null)timestamp(string): ISO 8601 timestamp when response was created
Error Response:
{
"id": "550e8400-e29b-41d4-a716-446655440000",
"success": false,
"error": {
"code": 1002,
"message": "Invalid parameters",
"details": {
"parameterName": "b",
"expectedType": "number"
},
"stackTrace": "..."
},
"timestamp": "2024-01-01T12:00:01.000Z"
}Error Response Fields:
id(string): Correlation ID matching the requestsuccess(boolean): Always false for error responseserror(RpcError): Error informationtimestamp(string): ISO 8601 timestamp when response was created
Represents error information in RPC responses.
Fields:
code(RpcErrorCode): Error code indicating the type of errormessage(string): Human-readable error messagedetails(object): Optional additional error detailsstackTrace(string): Optional stack trace (included based on configuration)
| Code | Name | Description |
|---|---|---|
| 0 | Unknown | Unknown or unspecified error |
| 1001 | MethodNotFound | The requested method was not found |
| 1002 | InvalidParameters | Invalid parameters were provided |
| 1003 | InternalError | An internal server error occurred |
| 1004 | Timeout | The request timed out |
| 1005 | SerializationError | Serialization or deserialization error |
| 1006 | ConnectionError | Connection or network error |
The library uses a structured channel naming convention:
- Request channels:
{channelPrefix}:request:{serviceName} - Response channels:
{channelPrefix}:response:{machineName}:{processId}:{guid}
Default channel prefix: redis-rpc
Example channels:
- Request:
redis-rpc:request:calculator - Response:
redis-rpc:response:SERVER01:1234:abc123def456
Configuration options for Redis RPC components.
public class RedisRpcOptions
{
public string ConnectionString { get; set; } = "localhost:6379";
public int DefaultTimeoutMs { get; set; } = 30000;
public int MaxConcurrentRequests { get; set; } = 100;
public string ChannelPrefix { get; set; } = "redis-rpc";
public bool IncludeStackTraceInErrors { get; set; } = false;
public int Database { get; set; } = 0;
}Provides convenient factory methods for creating RPC components.
// Default options
IRpcClient CreateClient(string connectionString = "localhost:6379")
// Custom options
IRpcClient CreateClient(RedisRpcOptions options)// Default options
IRpcServer CreateServer(string connectionString = "localhost:6379")
// Custom options
IRpcServer CreateServer(RedisRpcOptions options)// Default development options
RedisRpcOptions CreateDefaultOptions(string connectionString = "localhost:6379")
// Production-optimized options
RedisRpcOptions CreateProductionOptions(string connectionString)All RPC-specific exceptions inherit from RpcException:
MethodNotFoundException: Method not foundInvalidParametersException: Invalid parametersRpcTimeoutException: Request timeoutSerializationException: JSON serialization errorConnectionException: Redis connection errorInternalRpcException: Internal processing error
MethodNotFoundException → RpcErrorCode.MethodNotFound
InvalidParametersException → RpcErrorCode.InvalidParameters
RpcTimeoutException → RpcErrorCode.Timeout
SerializationException → RpcErrorCode.SerializationError
ConnectionException → RpcErrorCode.ConnectionError
InternalRpcException → RpcErrorCode.InternalError
Other exceptions → RpcErrorCode.InternalError- Inherit from BaseRpcMethodHandler for automatic method discovery
- Use RpcMethodAttribute to customize method names and descriptions
- Validate parameters and throw appropriate exceptions
- Handle cancellation tokens for long-running operations
- Return consistent result types for predictable client behavior
- Use specific exception types for different error conditions
- Provide meaningful error messages and details
- Don't expose sensitive information in error responses
- Log errors appropriately for debugging and monitoring
- Reuse client and server instances - they are thread-safe
- Configure appropriate timeouts based on expected processing times
- Monitor Redis memory usage and connection counts
- Consider connection pooling for high-throughput scenarios
- Validate all input parameters thoroughly
- Use Redis authentication in production environments
- Consider SSL/TLS for Redis connections
- Implement rate limiting if needed
- Sanitize error messages to prevent information disclosure