CAP Documentation

repository·master·Indexed 27 days ago

https://github.com/dotnetcore/cap

A lightweight .NET library for distributed transactions and event bus integration using the Outbox Pattern to ensure reliable message delivery. CAP supports multiple storage providers including SQL Server, MySQL, PostgreSQL, and MongoDB, as well as various transports such as RabbitMQ, Kafka, Azure Service Bus, Amazon SQS, NATS, Redis Streams, and Pulsar. It includes a real-time dashboard for monitoring message status and supports service discovery via Consul and Kubernetes.

Tokens
23.7K
Snippets
72
Records
123
Agent score
93%

What's inside CAP

  1. Overview of CAP EventBus

    master

    CAP is an EventBus and a distributed transaction solution designed for microservices and SOA (Service-Oriented Architecture) systems. It enables scalable, reliable, and easily modifiable microservices by providing a mechanism for components to communicate via events without direct dependencies.

    Key characteristics include:

    • High Flexibility: Unlike many service buses, CAP does not require users to implement or inherit specific interfaces for sending or processing messages.
    • Modular Design: It is highly scalable and allows for custom implementations of message queues, storage, and serialization.
    • Lightweight: Follows a 'convention over configuration' philosophy, making it beginner-friendly.
  2. Overview of CAP Distributed Transactions and EventBus

    master

    CAP is a .NET Standard library designed to solve distributed transaction problems in SOA or Microservice architectures. It functions as both a distributed transaction solution and an EventBus.

    Key capabilities include:

    • Reliable Messaging: Uses the Outbox Pattern by integrating a local message table with your current database. This ensures that event messages are not lost during service-to-service communication, even if exceptions occur.
    • Simplified EventBus: Provides a lightweight way to implement event publishing and subscriptions without requiring you to inherit or implement specific interfaces.
    • Eventual Consistency: Manages distributed transactions through an eventually consistent model.
  3. Understand CAP delivery guarantees

    master

    CAP provides an At Least Once delivery guarantee. Because CAP does not use distributed transactions (like MS DTC or 2PC) to coordinate between the message broker and your database, it is impossible to guarantee 'Exactly Once' delivery.

    In an At Least Once model, a message is only removed from the queue after your 'work transaction' (e.g., a database commit) has successfully completed. If the consumer fails or the system crashes before the message is deleted from the queue, the message will be redelivered. Therefore, your message handlers must be designed to handle the same message multiple times without causing unintended side effects.

  4. Understand CAP Persistence Mechanism

    master

    CAP ensures message reliability by using a local storage medium (database or NoSQL) to protect against message loss during network issues or message queue unavailability.

    Persistence Workflow

    1. Before Sent: Before a message reaches the message queue, CAP persists it in a local database table. CAP uses the same database transactions as your business code to ensure atomicity. If message persistence fails, the business transaction rolls back.
    2. After Sent: Once the message enters the queue, CAP relies on the queue's persistence capabilities:
      • RabbitMQ: CAP uses consumer queues with message persistence. Production Tip: For RabbitMQ, it is recommended to start all consumers at least once before sending messages to ensure all persistent queues are created.
      • Kafka: Uses built-in file-based persistence to ensure messages are not lost once they enter Kafka.
  5. Install and configure SQL Server storage for CAP

    master

    To use SQL Server as the storage backend for CAP, install the DotNetCore.CAP.SqlServer NuGet package.

    Note: CAP uses Microsoft.Data.SqlClient as the database driver. The use of System.Data.SqlClient is deprecated.

    Register the storage in your Startup.cs within the ConfigureServices method using x.UseSqlServer().

    PM> Install-Package DotNetCore.CAP.SqlServer
    public void ConfigureServices(IServiceCollection services)
    {
        // ...
    
        services.AddCap(x =>
        {
            x.UseSqlServer(opt => {
                // SqlServerOptions configuration
            }); 
            // x.UseXXX ...
        });
    }
  6. Configure CAP in .NET

    master

    Configure CAP in your Startup.cs or Program.cs using services.AddCap. You must specify both a storage provider (to implement the Outbox pattern) and a message transport.

    Supported storage methods include UseEntityFramework, UseSqlServer, UseMySql, UsePostgreSql, and UseMongoDB.

    Supported transport methods include UseRabbitMQ, UseKafka, UseAzureServiceBus, UseAmazonSQS, UseNATS, UsePulsar, and UseRedisStreams.

    services.AddCap(x =>
    {
        // Storage configuration
        x.UseEntityFramework<AppDbContext>();
        // OR
        x.UseSqlServer("Your ConnectionString");
    
        // Transport configuration
        x.UseRabbitMQ("HostName");
        // OR
        x.UseKafka("ConnectionString");
    });