envelope-openCourier Feature

1. Overview

The Courier feature in Vidyano provides a reliable, message-based processing system built on top of RavenDB. It enables asynchronous communication between different parts of your application through a publish-subscribe pattern, supporting immediate and delayed message delivery with at-least-once guarantees.

2. Enabling the Courier Feature

To enable the Courier feature in your Vidyano application:

  1. Register the feature in your Startup.cs file:

public void ConfigureServices(IServiceCollection services)
{
    // Add other services...
    
    // Register your Courier message handlers
    services.AddRecipient<YourMessage, YourMessageHandler>();
}

3. How It Works

The Courier feature works as follows:

  1. Message Publishing: Messages are stored in RavenDB as CourierMessage documents.

  2. Message Processing:

    • A RavenDB subscription monitors the database for new messages

    • When a message is detected, the system finds the appropriate recipient(s)

    • Each recipient processes the message through its ReceiveAsync method

    • After processing, messages are marked as processed with an expiration time

  3. Message Lifecycle:

    • Created → Waiting for Processing → Processed → Expired

    • Or: Created → Delayed → Waiting for Processing → Processed → Expired

    • Or: Created → Waiting for Processing → Failed → Retry → Processed → Expired

4. Key Features

4.1 Message Delivery Patterns

  • Immediate Delivery: Messages are sent and processed as soon as possible

  • Delayed Delivery: Messages are processed after a specified time delay

  • At-Least-Once Delivery: Messages are guaranteed to be delivered at least once

4.2 Idempotent Messages

Messages can implement the IIdempotentMessage interface to ensure they are only processed once, identified by a unique identifier.

4.3 Message Retry

Failed messages can be automatically retried based on configurable retry strategies.

4.4 Message Expiration

Processed messages are automatically removed from the system after a configurable expiration period.

5. Usage Examples

5.1 Defining a Message

5.2 Creating a Message Handler

5.3 Sending Messages

5.4 Chain of Messages

6. Configuration Options

The Courier system can be configured in your appsettings.json file:

Configuration options:

  • RetryWaitTime: Time to wait before retrying subscription connection (default: 30 seconds)

  • MaxDownTime: Maximum period the system will be down before giving up (default: 5 minutes)

  • MaxDocsPerBatch: Maximum number of messages processed in a batch (default: 25)

7. Best Practices

  1. Keep Messages Small: Store only essential information in messages.

  2. Make Messages Immutable: Use C# record types or immutable classes.

  3. Design for Idempotency: Ensure that processing a message multiple times does not cause issues.

  4. Handle Exceptions: Implement proper exception handling in your message handlers.

  5. Use Message Chains: Break complex workflows into chains of simple messages.

  6. Consider Message Expiration: Set appropriate expiration times for processed messages.

  7. Monitor the System: Implement logging and monitoring for message processing.

8. Troubleshooting

If messages are not being processed as expected:

  1. Check Subscription Status: Ensure the RavenDB subscription is active.

  2. Verify Handler Registration: Confirm that your message handlers are registered correctly.

  3. Review Message Serialization: Check that your message classes can be properly serialized/deserialized by JSON.NET.

  4. Examine Expired Messages: Look for messages that have expired before processing.

  5. Look for Exceptions: Check exception logs for errors in message handlers.

  6. Inspect RetryWaitTime: Adjust RetryWaitTime if messages are not being retried quickly enough.

  7. Disable Specific Message Types: Use the Courier{MessageTypeName}Disabled setting to temporarily disable processing of specific message types.

Was this helpful?