Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add redis persistence #12

Merged
merged 2 commits into from
Dec 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion DevelopmentGuide.md
Original file line number Diff line number Diff line change
Expand Up @@ -767,7 +767,7 @@ The following diagram illustrates how various classes are used in the execution
- Call `Handler` on the command handler to process commands.
- Map parameters and results to DTO's.
10. Add a `CustomerDatabaseSettings` class to a **Configuration** folder.
- Implement `IMongoDbSettings`.
- Extend `MongoDbSettings`.
11. Configure services and endpoints in `Program`.
- Automapper
```csharp
Expand Down
14 changes: 14 additions & 0 deletions EventDriven.Sagas.sln
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EventDriven.Sagas.Dependenc
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EventDriven.Sagas.Persistence.Abstractions.Tests", "test\EventDriven.Sagas.Persistence.Abstractions.Tests\EventDriven.Sagas.Persistence.Abstractions.Tests.csproj", "{F61E2F95-2A59-4E0B-98E4-78204D78590D}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EventDriven.Sagas.Persistence.Redis", "src\EventDriven.Sagas.Persistence.Redis\EventDriven.Sagas.Persistence.Redis.csproj", "{D7471622-6570-410B-861F-8B462854D270}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EventDriven.Sagas.Persistence.Pool.Abstractions", "src\EventDriven.Sagas.Persistence.Pool.Abstractions\EventDriven.Sagas.Persistence.Pool.Abstractions.csproj", "{69DE022F-0AEC-44A7-BA78-4830A60235CC}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -154,6 +158,14 @@ Global
{F61E2F95-2A59-4E0B-98E4-78204D78590D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F61E2F95-2A59-4E0B-98E4-78204D78590D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F61E2F95-2A59-4E0B-98E4-78204D78590D}.Release|Any CPU.Build.0 = Release|Any CPU
{D7471622-6570-410B-861F-8B462854D270}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D7471622-6570-410B-861F-8B462854D270}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D7471622-6570-410B-861F-8B462854D270}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D7471622-6570-410B-861F-8B462854D270}.Release|Any CPU.Build.0 = Release|Any CPU
{69DE022F-0AEC-44A7-BA78-4830A60235CC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{69DE022F-0AEC-44A7-BA78-4830A60235CC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{69DE022F-0AEC-44A7-BA78-4830A60235CC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{69DE022F-0AEC-44A7-BA78-4830A60235CC}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -181,6 +193,8 @@ Global
{C67BF2F3-1B5E-44EB-93A5-634FEC0E073F} = {2F6C2678-B38D-4784-AEDA-1DF293E1296A}
{75635BC4-39E9-45D6-AB55-DE9AEE0670F1} = {7340565F-A10B-4B7D-B75E-9320CAE5BD05}
{F61E2F95-2A59-4E0B-98E4-78204D78590D} = {7340565F-A10B-4B7D-B75E-9320CAE5BD05}
{D7471622-6570-410B-861F-8B462854D270} = {4B466731-0CE1-4A92-B93A-AE088C5B6784}
{69DE022F-0AEC-44A7-BA78-4830A60235CC} = {4B466731-0CE1-4A92-B93A-AE088C5B6784}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {3BFC4ACA-7DDF-4583-A578-5CAB3893AE40}
Expand Down
29 changes: 29 additions & 0 deletions ReadMe.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,35 @@ The Saga orchestrator coordinates the entire process by telling each participant
- If the saga was unsuccessful due to insufficient credit, then the order `State` property should be set to `0` (`Initial`) and the customer credit should be unchanged.
- If the saga was unsuccessul due to an error condition, the order `State` property may be set to `1` (`Pending`). If this is the case you may need to delete the order record in MongoDB, or reset the `State` property of the order to `0` (`Initial`).

### Persist Sagas to Redis

You have the option of using Redis for saga persistence, which offers performance advantages over MongoDB.

1. Add `PersistableSagaRedisSettings` section to appsettings.json.

```json
"PersistableSagaRedisSettings": {
"ConnectionString": "localhost:6379",
"InstanceName": "OrderService",
"DistributedCacheEntryOptions": {
"SlidingExpiration": "00:05:00"
}
}
```

2. Add saga Redis settings

```csharp
builder.Services.AddSagaRedisSettings(builder.Configuration);
```

3. Pass `RedisPersistableSagaRepository` to `AddSaga`.

```csharp
builder.Services.AddSaga<CreateOrderSaga, OrderMetadata, CreateOrderSagaConfigSettings, CreateOrderSagaCommandDispatcher,
SagaConfigRepository, SagaSnapshotRepository, RedisPersistableSagaRepository<CreateOrderSaga, OrderMetadata>>(builder.Configuration);
```

## Development Guide

### Overview
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@

namespace SagaConfigService.Configuration;

public class SagaConfigDatabaseSettings : IMongoDbSettings
public class SagaConfigDatabaseSettings : MongoDbSettings
{
public string CollectionName { get; set; } = null!;
public string ConnectionString { get; set; } = null!;
public string DatabaseName { get; set; } = null!;
}
2 changes: 1 addition & 1 deletion administration/SagaConfigService/SagaConfigService.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

<ItemGroup>
<PackageReference Include="EventDriven.DependencyInjection.URF.Mongo" Version="1.2.2" />
<PackageReference Include="MongoDB.Driver" Version="2.21.0" />
<PackageReference Include="MongoDB.Driver" Version="2.22.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
<PackageReference Include="URF.Core.Mongo" Version="7.0.0" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@

namespace SagaSnapshotService.Configuration;

public class SagaSnapshotDatabaseSettings : IMongoDbSettings
public class SagaSnapshotDatabaseSettings : MongoDbSettings
{
public string CollectionName { get; set; } = null!;
public string ConnectionString { get; set; } = null!;
public string DatabaseName { get; set; } = null!;
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

<ItemGroup>
<PackageReference Include="EventDriven.DependencyInjection.URF.Mongo" Version="1.2.2" />
<PackageReference Include="MongoDB.Driver" Version="2.21.0" />
<PackageReference Include="MongoDB.Driver" Version="2.22.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
<PackageReference Include="URF.Core.Mongo" Version="7.0.0" />
</ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion reference-architecture/Common/Common.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

<ItemGroup>
<PackageReference Include="EventDriven.CQRS.Extensions" Version="2.0.0" />
<PackageReference Include="EventDriven.EventBus.Abstractions" Version="1.3.2" />
<PackageReference Include="EventDriven.EventBus.Abstractions" Version="1.4.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="6.0.4" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@

namespace CustomerService.Configuration;

public class CustomerDatabaseSettings : IMongoDbSettings
public class CustomerDatabaseSettings : MongoDbSettings
{
public string CollectionName { get; set; } = null!;
public string ConnectionString { get; set; } = null!;
public string DatabaseName { get; set; } = null!;
}
6 changes: 3 additions & 3 deletions reference-architecture/CustomerService/CustomerService.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
<PackageReference Include="EventDriven.CQRS.Abstractions" Version="2.1.0" />
<PackageReference Include="EventDriven.CQRS.Extensions" Version="2.0.0" />
<PackageReference Include="EventDriven.DependencyInjection.URF.Mongo" Version="1.2.2" />
<PackageReference Include="EventDriven.EventBus.Dapr" Version="1.3.8" />
<PackageReference Include="EventDriven.EventBus.Dapr.EventCache.Mongo" Version="1.3.8" />
<PackageReference Include="EventDriven.EventBus.Dapr" Version="1.4.0" />
<PackageReference Include="EventDriven.EventBus.EventCache.Mongo" Version="1.4.0" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="6.0.16" />
<PackageReference Include="MongoDB.Driver" Version="2.21.0" />
<PackageReference Include="MongoDB.Driver" Version="2.22.0" />
<PackageReference Include="NeoSmart.AsyncLock" Version="3.2.1" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
<PackageReference Include="URF.Core.Mongo" Version="7.0.0" />
Expand Down
2 changes: 1 addition & 1 deletion reference-architecture/CustomerService/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

// Add Dapr Event Bus and event handlers
builder.Services.AddDaprEventBus(builder.Configuration);
builder.Services.AddDaprMongoEventCache(builder.Configuration);
builder.Services.AddMongoEventCache(builder.Configuration);
builder.Services.AddSingleton<CustomerCreditReserveRequestedEventHandler>();
builder.Services.AddSingleton<CustomerCreditReleaseRequestedEventHandler>();

Expand Down
8 changes: 3 additions & 5 deletions reference-architecture/CustomerService/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,12 @@
"DaprEventBusOptions": {
"PubSubName": "pubsub"
},
"DaprEventCacheOptions": {
"DaprStateStoreOptions": {
"StateStoreName": "statestore-mongodb"
},
"MongoEventCacheOptions": {
"AppName": "customer-service",
"EventCacheTimeout": "00:01:00",
"EventCacheCleanupInterval": "00:05:00"
},
"DaprStoreDatabaseSettings": {
"MongoStoreDatabaseSettings": {
"ConnectionString": "mongodb://localhost:27017",
"DatabaseName": "daprStore",
"CollectionName": "daprCollection"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@

namespace InventoryService.Configuration;

public class InventoryDatabaseSettings : IMongoDbSettings
public class InventoryDatabaseSettings : MongoDbSettings
{
public string CollectionName { get; set; } = null!;
public string ConnectionString { get; set; } = null!;
public string DatabaseName { get; set; } = null!;
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
<PackageReference Include="EventDriven.CQRS.Abstractions" Version="2.1.0" />
<PackageReference Include="EventDriven.CQRS.Extensions" Version="2.0.0" />
<PackageReference Include="EventDriven.DependencyInjection.URF.Mongo" Version="1.2.2" />
<PackageReference Include="EventDriven.EventBus.Dapr" Version="1.3.8" />
<PackageReference Include="EventDriven.EventBus.Dapr.EventCache.Mongo" Version="1.3.8" />
<PackageReference Include="EventDriven.EventBus.Dapr" Version="1.4.0" />
<PackageReference Include="EventDriven.EventBus.EventCache.Mongo" Version="1.4.0" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="6.0.16" />
<PackageReference Include="MongoDB.Driver" Version="2.21.0" />
<PackageReference Include="MongoDB.Driver" Version="2.22.0" />
<PackageReference Include="NeoSmart.AsyncLock" Version="3.2.1" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
<PackageReference Include="URF.Core.Mongo" Version="7.0.0" />
Expand Down
2 changes: 1 addition & 1 deletion reference-architecture/InventoryService/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

// Add Dapr Event Bus and event handler
builder.Services.AddDaprEventBus(builder.Configuration);
builder.Services.AddDaprMongoEventCache(builder.Configuration);
builder.Services.AddMongoEventCache(builder.Configuration);
builder.Services.AddSingleton<ProductInventoryReserveRequestedEventHandler>();
builder.Services.AddSingleton<ProductInventoryReleaseRequestedEventHandler>();

Expand Down
8 changes: 3 additions & 5 deletions reference-architecture/InventoryService/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,12 @@
"DaprEventBusOptions": {
"PubSubName": "pubsub"
},
"DaprEventCacheOptions": {
"DaprStateStoreOptions": {
"StateStoreName": "statestore-mongodb"
},
"MongoEventCacheOptions": {
"AppName": "inventory-service",
"EventCacheTimeout": "00:01:00",
"EventCacheCleanupInterval": "00:05:00"
},
"DaprStoreDatabaseSettings": {
"MongoStoreDatabaseSettings": {
"ConnectionString": "mongodb://localhost:27017",
"DatabaseName": "daprStore",
"CollectionName": "daprCollection"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@

namespace OrderService.Configuration;

public class OrderDatabaseSettings : IMongoDbSettings
public class OrderDatabaseSettings : MongoDbSettings
{
public string CollectionName { get; set; } = null!;
public string ConnectionString { get; set; } = null!;
public string DatabaseName { get; set; } = null!;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@

namespace OrderService.Configuration;

public class PersistableSagaDatabaseSettings : IMongoDbSettings
public class PersistableSagaDatabaseSettings : MongoDbSettings
{
public string CollectionName { get; set; } = null!;
public string ConnectionString { get; set; } = null!;
public string DatabaseName { get; set; } = null!;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@

namespace OrderService.Configuration;

public class SagaConfigDatabaseSettings : IMongoDbSettings
public class SagaConfigDatabaseSettings : MongoDbSettings
{
public string CollectionName { get; set; } = null!;
public string ConnectionString { get; set; } = null!;
public string DatabaseName { get; set; } = null!;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@

namespace OrderService.Configuration;

public class SagaSnapshotDatabaseSettings : IMongoDbSettings
public class SagaSnapshotDatabaseSettings : MongoDbSettings
{
public string CollectionName { get; set; } = null!;
public string ConnectionString { get; set; } = null!;
public string DatabaseName { get; set; } = null!;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using EventDriven.CQRS.Abstractions.Commands;
using EventDriven.Sagas.Abstractions;
using EventDriven.Sagas.Persistence.Abstractions.Pools;
using EventDriven.Sagas.Persistence.Pool.Abstractions.Pools;
using OrderService.Domain.OrderAggregate.Commands;
using OrderService.Helpers;
using OrderService.Repositories;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Common.Integration.Events;
using Common.Integration.Models;
using EventDriven.EventBus.Abstractions;
using EventDriven.Sagas.Persistence.Abstractions.Pools;
using EventDriven.Sagas.Persistence.Pool.Abstractions.Pools;
using OrderService.Domain.OrderAggregate;
using OrderService.Repositories;
using OrderService.Sagas.CreateOrder;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Common.Integration.Events;
using Common.Integration.Models;
using EventDriven.EventBus.Abstractions;
using EventDriven.Sagas.Persistence.Abstractions.Pools;
using EventDriven.Sagas.Persistence.Pool.Abstractions.Pools;
using OrderService.Domain.OrderAggregate;
using OrderService.Repositories;
using OrderService.Sagas.CreateOrder;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Common.Integration.Events;
using Common.Integration.Models;
using EventDriven.EventBus.Abstractions;
using EventDriven.Sagas.Persistence.Abstractions.Pools;
using EventDriven.Sagas.Persistence.Pool.Abstractions.Pools;
using OrderService.Domain.OrderAggregate;
using OrderService.Repositories;
using OrderService.Sagas.CreateOrder;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Common.Integration.Events;
using Common.Integration.Models;
using EventDriven.EventBus.Abstractions;
using EventDriven.Sagas.Persistence.Abstractions.Pools;
using EventDriven.Sagas.Persistence.Pool.Abstractions.Pools;
using OrderService.Domain.OrderAggregate;
using OrderService.Repositories;
using OrderService.Sagas.CreateOrder;
Expand Down
7 changes: 4 additions & 3 deletions reference-architecture/OrderService/OrderService.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
<PackageReference Include="EventDriven.CQRS.Abstractions" Version="2.1.0" />
<PackageReference Include="EventDriven.CQRS.Extensions" Version="2.0.0" />
<PackageReference Include="EventDriven.DependencyInjection.URF.Mongo" Version="1.2.2" />
<PackageReference Include="EventDriven.EventBus.Dapr" Version="1.3.8" />
<PackageReference Include="EventDriven.EventBus.Dapr.EventCache.Mongo" Version="1.3.8" />
<PackageReference Include="MongoDB.Driver" Version="2.21.0" />
<PackageReference Include="EventDriven.EventBus.Dapr" Version="1.4.0" />
<PackageReference Include="EventDriven.EventBus.EventCache.Mongo" Version="1.4.0" />
<PackageReference Include="MongoDB.Driver" Version="2.22.0" />
<PackageReference Include="NeoSmart.AsyncLock" Version="3.2.1" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
<PackageReference Include="URF.Core.Mongo" Version="7.0.0" />
Expand All @@ -24,6 +24,7 @@
<ProjectReference Include="..\..\src\EventDriven.Sagas.Configuration.Mongo\EventDriven.Sagas.Configuration.Mongo.csproj" />
<ProjectReference Include="..\..\src\EventDriven.Sagas.DependencyInjection\EventDriven.Sagas.DependencyInjection.csproj" />
<ProjectReference Include="..\..\src\EventDriven.Sagas.Persistence.Mongo\EventDriven.Sagas.Persistence.Mongo.csproj" />
<ProjectReference Include="..\..\src\EventDriven.Sagas.Persistence.Redis\EventDriven.Sagas.Persistence.Redis.csproj" />
<ProjectReference Include="..\Common\Common.csproj" />
</ItemGroup>

Expand Down
9 changes: 6 additions & 3 deletions reference-architecture/OrderService/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
using EventDriven.Sagas.DependencyInjection;
using EventDriven.Sagas.Persistence.Abstractions.DTO;
using EventDriven.Sagas.Persistence.Mongo.Repositories;
using EventDriven.Sagas.Persistence.Redis;
using EventDriven.Sagas.Persistence.Redis.Repositories;
using MediatR;
using OrderService.Configuration;
using OrderService.Domain.OrderAggregate;
Expand Down Expand Up @@ -40,16 +42,17 @@
// Add behaviors
builder.Services.AddTransient(typeof(IPipelineBehavior<,>), typeof(LoggingBehavior<,>));

// App settings
// Add settings
builder.Services.AddAppSettings<CreateOrderSagaConfigSettings>(builder.Configuration);
builder.Services.AddSagaRedisSettings(builder.Configuration);

// Sagas
builder.Services.AddSaga<CreateOrderSaga, OrderMetadata, CreateOrderSagaConfigSettings, CreateOrderSagaCommandDispatcher,
SagaConfigRepository, SagaSnapshotRepository, PersistableSagaRepository<CreateOrderSaga,OrderMetadata>>(builder.Configuration);
SagaConfigRepository, SagaSnapshotRepository, RedisPersistableSagaRepository<CreateOrderSaga, OrderMetadata>>(builder.Configuration);

// Event Bus and event handlers
builder.Services.AddDaprEventBus(builder.Configuration);
builder.Services.AddDaprMongoEventCache(builder.Configuration);
builder.Services.AddMongoEventCache(builder.Configuration);
builder.Services.AddSingleton<CustomerCreditReserveFulfilledEventHandler>();
builder.Services.AddSingleton<CustomerCreditReleaseFulfilledEventHandler>();
builder.Services.AddSingleton<ProductInventoryReserveFulfilledEventHandler>();
Expand Down