When we build microservices, the hard part is usually not publishing a message. The hard part is making database writes and message delivery behave correctly when failures happen between them. If a service stores data in PostgreSQL, publishes an integration event to RabbitMQ or Kafka, and also needs to update another store like MongoDB, we need a reliable way to keep those steps consistent without using a distributed transaction.
In this post, I want to show a practical way to solve that problem with Wolverine. The sample uses a small e-commerce system with two microservices, Catalogs and Orders, and demonstrates three important patterns together while keeping the broker selectable through configuration:
- transactional outbox for publishing integration events after the database commit
- durable inbox for idempotent downstream message consumption
- durable local processing for internal work that should run after commit in a separate transaction
The sample is based on my Wolverine transactional messaging structure and uses a two-service e-commerce scenario with Catalog and Order. In the local blog sample, I kept the same architectural boundaries so the article stays practical: shared contracts, separate service projects, an Aspire AppHost, focused integration tests, and a transport switch between RabbitMQ and Kafka.
The Problem We Want to Solve
Assume Catalogs receives a request to create a product.
That request needs to do three things:
- Store the product in PostgreSQL.
- Publish
ProductCreatedV1soOrderscan import that product through RabbitMQ or Kafka. - Update a MongoDB read model for query scenarios.
These three steps do not belong to one physical transaction:
- PostgreSQL write uses one database transaction.
- RabbitMQ or Kafka publish is external broker communication.
- MongoDB projection should happen in a different persistence boundary.
If we do these steps manually in sequence, failures create inconsistent state very quickly:
- product saved in PostgreSQL but event not published
- event published but local transaction rolled back
- product saved and event published but MongoDB projection failed
- downstream service receives duplicate messages and writes duplicate records
This is exactly where Wolverine helps.
What Wolverine Gives Us
Wolverine is not only a message bus. It also gives us durable messaging primitives that fit very well with transactional application code.
For this scenario, the important capabilities are:
- durable outgoing messages for outbox behavior
- durable incoming messages for inbox behavior
- durable local queues for internal post-commit processing
- EF Core integration so message persistence can participate in the same PostgreSQL transaction as the write model
That means we can persist application data and outgoing work together, commit once, and let Wolverine flush the durable work only after the transaction succeeds.
Sample Scenario
The sample uses two microservices in an e-commerce domain:
Catalogs: write-side service for creating productsOrders: downstream consumer that imports product data from integration events
The flow is:
CatalogsreceivesPOST /api/v1/catalogs/products.Catalogsstores the product in PostgreSQL.- In the same transaction,
CatalogsqueuesProductCreatedV1for the configured broker. - In the same transaction,
Catalogsalso queues an internal durable command namedProjectProductReadModel. - After commit, Wolverine dispatches both durable messages.
- The local durable handler projects the product into MongoDB.
OrdersconsumesProductCreatedV1through a durable inbox and stores its own imported product record in PostgreSQL.
This gives us one transactional write boundary in Catalogs, one separate local processing boundary for MongoDB, and one separate consumer boundary in Orders.
Here is the sample API entry point that starts the flow:
|
|
The endpoint has one job: parse the HTTP request, build a command, send it through MediatR, and return the response.
Why Durable Local Processing Matters
This is the part many teams miss.
Sometimes we only think about external messaging, but internal asynchronous work can be just as important. In this sample, MongoDB projection is not an external integration event. It is internal application work, but it still should not run inside the same request transaction.
Why?
- MongoDB is a different persistence technology.
- We do not want the HTTP request to fail because a secondary projection store is temporarily unavailable.
- We want the write model in PostgreSQL to stay the source of truth.
- We still want reliable eventual consistency for the read model.
So instead of writing to MongoDB inline, Catalogs sends a durable local command after the PostgreSQL transaction is committed. That gives us a clean separation:
- PostgreSQL write model is committed first.
- MongoDB projection runs later in its own handler and its own persistence boundary.
- If projection fails, Wolverine can retry it without losing the original business write.
This is what I mean by internal message processing in a transactional way. It is not one distributed transaction across PostgreSQL and MongoDB. It is a reliable post-commit workflow with durable local messaging.
Solution Structure
The sample keeps the structure close to a real microservice solution instead of putting everything in one demo project.
Main parts of the local sample:
src/Aspire/ECommerce.AppHost: Aspire orchestration entry point forcatalogs-api,orders-api, PostgreSQL, MongoDB, and either RabbitMQ or Kafka depending on configurationsrc/Services/Catalog/Catalog: write-side product logic and internal read-model projection flowsrc/Services/Catalog/Catalog.Api: HTTP endpoints for creating products and reading write/read modelssrc/Services/Order/Order: downstream import logic with inbox-style duplicate protectionsrc/Services/Order/Order.Api: HTTP endpoints for importing and querying products in the order servicesrc/Shared/Contracts:MessageEnvelope<T>,ProductCreatedV1, andProjectProductReadModelsrc/BuildingBlocks/BuildingBlocks.Integration.Wolverine: shared Wolverine configuration with MediatR bridge for internal command dispatchtests/Shared/Tests.Shared: shared end-to-end flow helper used by service teststests/Services/Catalog/Catalog.Tests: integration test for create + project flowtests/Services/Order/Order.Tests: integration test for idempotent import behavior
This structure matters because the article is not only about Wolverine APIs. It is also about where to place contracts, handlers, persistence, and feature slices in a maintainable solution.
Each feature follows a consistent pattern with three files:
- Command/Query (
CreateProductCommand.cs): defines the request as a MediatRIRequest<TResponse>record - Handler (
CreateProductHandler.cs): implementsIRequestHandler<TRequest, TResponse>with the business logic - Endpoint (
CreateProductEndpoint.cs): thin HTTP layer that injectsIMediatorand callsmediator.Send(command)
This keeps endpoints focused on HTTP concerns (routing, binding, status codes) while all business logic lives in MediatR handlers that are auto-discovered and testable in isolation.
The shared contracts are intentionally small and explicit:
|
|
System Architecture
The diagram below shows the full architecture with both services, the message broker, and both persistence stores:
Message Flow End to End
Letβs walk through the full request flow.
1. Catalogs receives product creation request
Catalogs exposes an endpoint like:
|
|
The request creates a product in the write model.
In the sample, that endpoint delegates to a MediatR command through the thin CreateProductEndpoint: it receives the HTTP request, sends a CreateProductCommand through IMediator, and returns a CreatedAtRoute response. The handler then orchestrates the write model, integration event, and internal projection command.
2. MediatR handler orchestrates the write transaction
The actual business logic lives in the CreateProductHandler, which MediatR invokes when the endpoint sends a CreateProductCommand:
|
|
The important point is not the exact syntax. The important point is the behavior:
- product write is persisted in PostgreSQL
- outgoing broker event is persisted durably
- local projection command is persisted durably
- commit happens once
- Wolverine dispatches durable work only after commit succeeds
That is the outbox story. The handler brings the write model, integration event, and internal command into one persistence boundary.
The handler follows the same responsibilities in one MediatR pipeline:
- persist the write model in PostgreSQL
- publish the integration event through
IEventBus(Wolverine outbox) - enqueue internal projection work through
IInternalCommandBus(MediatR bridge) SaveChangesAsynctriggers the transactional outbox β Wolverine flushes durable messages only after commit
External Integration Event with RabbitMQ or Kafka
After commit, Wolverine publishes MessageEnvelope<ProductCreatedV1> to the configured broker.
In this sample, both APIs default to rabbitmq, but you can switch to kafka with configuration only. The application code does not need to change because the Wolverine transport wiring is selected in startup based on the configured transport.
If you run the APIs directly, set this in both API projects:
|
|
Change the value to kafka to switch brokers.
If you run through Aspire, the AppHost reads Messaging__Transport, provisions only the selected broker container, and forwards the same value to both services.
|
|
Use rabbitmq instead of kafka to switch back.
Why wrap the event in a message envelope?
Because in real systems we usually need metadata such as:
- message id
- correlation id
- causation id
- message type
- creation time
- tracing or tenant information
A reusable MessageEnvelope<T> keeps transport-level and observability metadata consistent across services.
The sample envelope and event contracts look like this:
|
|
In this sample, Orders subscribes to the same event and imports the product into its own database.
That means Catalogs does not know anything about Orders internals. It only publishes an integration event.
Durable Inbox in Orders
On the consumer side, Orders uses Wolverine durable inbox semantics backed by PostgreSQL.
This solves a common problem in broker-based systems: duplicate delivery.
RabbitMQ and Kafka both require idempotent consumers. Consumers can crash after partially processing a message. Network failures can make delivery uncertain. Without inbox protection, the same event may create duplicate rows or duplicate side effects.
With a durable inbox:
- incoming messages are tracked durably
- processing can be retried safely
- duplicate deliveries can be ignored or handled idempotently
- the consumer can recover after crashes without losing message state
In the sample, Orders consumes ProductCreatedV1 and writes an imported product record to its own PostgreSQL database. That is a classic microservice integration pattern: each service owns its own data and builds its own local view from events.
The consumer follows a thin bridge pattern: a Wolverine handler receives the message from the transport and delegates to a MediatR notification handler for the actual business logic.
|
|
The notification and its handler carry the actual business logic:
|
|
This separation keeps the transport consumer minimal and testable. The Wolverine handler only does transport-specific work (deserialization, envelope extraction), and all business logic lives in the MediatR notification handler. Unit tests can test the handler directly without bootstrapping Wolverine.
On the Orders side, the endpoint that receives external integration events follows the same thin pattern β it publishes through IEventBus and returns immediately:
|
|
Internal Durable Command for MongoDB Projection
Now back to the internal processor scenario.
After Catalogs commits the product in PostgreSQL, it also dispatches a durable local command:
|
|
A local MediatR handler processes that command and upserts a MongoDB read model.
ProjectProductReadModel implements both IInternalCommand (for the IInternalCommandBus interface constraint) and IRequest (for MediatR dispatch):
And the handler implements IRequestHandler<ProjectProductReadModel>:
|
|
This handler runs outside the original HTTP request transaction.
That gives us several benefits:
- MongoDB projection is retriable
- request latency stays lower
- PostgreSQL remains the transactional source of truth
- projection failures do not corrupt the write transaction
To register the MediatR handlers, the module configuration uses AddMediatR instead of Wolverine’s ScanHandlers:
|
|
The same applies to Orders, where MediatR is registered alongside Wolverine ScanHandlers for the thin transport consumer. Module prefixes for exchange/queue naming are shared via constants in MessagingConstants:
|
|
Using constants avoids hardcoded strings and makes the convention explicit across services:
|
|
This is a very useful pattern when one service needs to persist data in multiple stores but should only treat one of them as the primary transactional boundary.
In this sample, Wolverine is used exclusively as the transport layer for RabbitMQ or Kafka, while the local command dispatch is handled by MediatR. The WolverineInternalCommandBus bridges the two by accepting an internal command through Wolverine’s durable queue and forwarding it to MediatR:
|
|
This separation keeps Wolverine focused on reliable transport with transactional outbox/inbox while MediatR handles the in-process command dispatch with its pipeline behaviours, request/response semantics, and notification broadcasting.
The simplified sample implementation of that projection flow is:
|
|
In the actual implementation, this method is encapsulated in the MediatR handler discussed above, so projection runs after commit with retries through Wolverine’s durable local queue.
Integration Tests
The sample includes focused integration tests so the transport switch is not only documented, but also verified.
The tests verify the business flow works with both supported transport values using TestContainers:
Catalog flow test β creates a product and verifies both the PostgreSQL write model and the MongoDB read model:
|
|
Order import tests β insert on first delivery, update on duplicate, and verify error handling for faulty payloads:
|
|
These tests make the article more concrete because they verify the most important outcomes:
- the product is written and projected across PostgreSQL and MongoDB
- duplicate messages update existing records idempotently
- faulty payloads are rejected and moved to Wolverine’s error queue
There are also transport configuration tests that validate normalization and rejection of unsupported values. All integration tests use TestContainers with real PostgreSQL, MongoDB, and RabbitMQ or Kafka instances.
Conclusion
Transactional messaging is really about failure handling, not only message publishing. When one request needs to update a write model, publish an integration event, and trigger internal asynchronous work, Wolverine gives a clean way to model those steps with outbox, inbox, and durable local processing.
This sample shows how Wolverine can model transactional outbox, durable inbox, and durable local processing in a way that fits real microservice boundaries. RabbitMQ and Kafka stay interchangeable through configuration, the APIs are wired for both transports, and the tests keep the switching behavior covered.