Volume 6 · Chapter 9 — Enterprise Application Architecture and Design Patterns
Integration Architecture
9.1 Introduction
Integration architecture defines how applications, services, and external platforms communicate reliably, securely, and at scale. In enterprise .NET the choice between synchronous and asynchronous integration - and the patterns that make each robust - shapes the resilience and coupling of the whole system. The guiding principle is to couple as loosely as the business requirement allows, and no looser.
9.2 Synchronous vs Asynchronous
| Synchronous (REST/gRPC) | Asynchronous (messaging/events) | |
|---|---|---|
| Coupling | Temporal - both must be up | Decoupled in time |
| Latency | Immediate response | Deferred, throughput-oriented |
| Failure impact | Propagates to caller | Absorbed by the broker |
| Best for | Queries, request/response | Events, workflows, load levelling |
9.3 Reliable Synchronous Integration
Synchronous calls are simple but couple availability. Make them resilient with timeouts, retries with jitter, and circuit breakers so a slow dependency cannot exhaust your threads. In .NET this is expressed withHttpClientFactoryplus Polly resilience handlers.
builder.Services.AddHttpClient<CreditClient>()
.AddStandardResilienceHandler(o =>
{
o.Retry.MaxRetryAttempts = 3;
o.CircuitBreaker.FailureRatio = 0.5;
o.AttemptTimeout.Timeout = TimeSpan.FromSeconds(2);
});
9.4 Asynchronous Messaging Patterns
Messaging decouples producers from consumers in time. The core patterns are point-to-point (a queue delivering each message to one consumer), publish-subscribe (a topic fanning out to many subscribers), and request-reply over messaging. Brokers add persistence, retries, ordering, and dead-letter queues for messages that repeatedly fail.
sequenceDiagram autonumber participant Prod as Producer participant Q as Queue / Topic participant C1 as Consumer A participant C2 as Consumer B participant DLQ as Dead-Letter Queue Prod-)Q: publish message Q-)C1: deliver C1-->>Q: ack (processed) Q-)C2: deliver C2--xQ: nack (fails 5x) Q-)DLQ: move poison message Note over Q,DLQ: DLQ isolates poison messages for later inspection
9.5 The Transactional Outbox
The hardest integration problem is the dual write: updating the database and publishing a message must be atomic, or you get lost or phantom messages. The outbox pattern solves it by writing the message to an outbox table in the same transaction as the state change; a relay then publishes committed outbox rows and marks them sent. Consumers stay idempotent because delivery is at-least-once.
sequenceDiagram autonumber participant Svc as Service participant DB as Database (state + outbox) participant Relay as Outbox Relay participant Broker as Message Broker Svc->>DB: BEGIN TX: save state + insert outbox row DB-->>Svc: COMMIT (atomic) Relay->>DB: poll unsent outbox rows Relay->>Broker: publish message Broker-->>Relay: ack Relay->>DB: mark row sent
9.6 Event-Driven Integration
In event-driven integration, services announce facts (“PaymentCompleted”) rather than issuing commands, and interested parties react. This inverts dependencies: the producer knows nothing about consumers, so new consumers are added without changing the producer - the strongest form of loose coupling.
9.7 API Gateway and Anti-Corruption Layer
An API Gateway centralises the concerns of exposing services to clients: routing, auth, throttling, aggregation. At the other edge, an Anti-Corruption Layer wraps legacy or third-party systems so their models and quirks never contaminate your domain. Together they keep both the client-facing and partner-facing surfaces clean.
// ACL translating a partner's payload into our domain contract.
public sealed class PartnerRatesAcl : IExchangeRates
{
private readonly PartnerRatesClient _client;
public async Task<Rate> GetAsync(string pair, CancellationToken ct)
{
var dto = await _client.FetchAsync(pair, ct); // partner's shape
return new Rate(pair, dto.bid, dto.ask, dto.ts); // our shape
}
}
9.8 Azure Service Bus in Practice
Azure Service Bus provides queues and topics with sessions (for ordering), scheduled and deferred messages, dead-lettering, and duplicate detection. It is the common backbone for reliable enterprise messaging in Azure-hosted .NET, usually consumed through MassTransit or the native SDK.
When should you use messaging instead of REST APIs?
Use messaging when you want temporal decoupling, resilience to downstream outages, load levelling of spiky traffic, or fan-out to multiple consumers - and when the caller does not need an immediate answer. Use REST/gRPC for queries and request/response interactions that need a synchronous result and simple semantics. Many systems use both: synchronous for reads that need immediacy, asynchronous events for state changes that others react to. The deciding questions are “does the caller need the result now?” and “can the caller tolerate the callee being down?”
What is the dual-write problem and how does the outbox pattern solve it?
The dual-write problem is that writing to the database and publishing to a broker are two separate operations, so a crash between them leaves them inconsistent - either a state change with no event, or an event with no state change. The outbox pattern makes it atomic: the message is inserted into an outbox table inside the same database transaction as the state change, so they commit or roll back together. A separate relay publishes committed outbox rows to the broker and marks them sent. Because the relay guarantees at-least-once delivery, consumers must be idempotent, but no message is ever lost or phantom.
How do you make a message consumer idempotent?
Because brokers deliver at-least-once, a consumer may see the same message more than once and must produce the same effect each time. Techniques: give each message a unique id and record processed ids in a dedup table, rejecting repeats; make the operation naturally idempotent (an upsert keyed by business id, or a “set balance to X” rather than “add X”); or use the message id as an idempotency key on the downstream write. Combined with a dead-letter queue for poison messages, this makes at-least-once delivery safe.
How do you preserve message ordering when it matters?
Strict global ordering is expensive and usually unnecessary; what you normally need is ordering within a key (all events for one account). You achieve that by partitioning: route messages with the same key to the same partition or session (Azure Service Bus sessions, Kafka partition keys) and process each partition single-threaded. This preserves per-entity order while still allowing parallelism across entities. If your consumers are idempotent and commutative, you can often relax ordering entirely, which scales better.
How do you design enterprise integration architecture end to end?
I start from the business interactions and classify each by whether it needs an immediate answer and how tightly the parties may couple. Synchronous, resilience-wrapped calls handle queries; asynchronous events handle state changes and workflows. I make eventing reliable with the outbox pattern and idempotent, dead-lettered consumers, and I keep contracts versioned and backward compatible. I isolate external systems behind anti-corruption layers and expose internal services through a gateway. Cross-cutting concerns - auth, tracing, retries - are standardised in one place. Finally I make the whole thing observable with correlation IDs and distributed tracing so a business transaction can be followed across every hop.
A downstream partner API is slow and intermittently failing, and it is taking your service down with it. What do you do?
This is cascading failure from temporal coupling. Immediately I’d wrap the call with a tight timeout, a circuit breaker that trips on sustained failure, and a bulkhead so calls to that partner cannot exhaust the whole thread/connection pool. Where the interaction does not need to be synchronous, I’d convert it to asynchronous: enqueue the request and process it with retries and a dead-letter queue, returning a pending status to the caller. I’d add a fallback or cached response where the business allows, and an anti-corruption layer so their quirks stay contained. Longer term, the fix is to stop letting a partner’s availability dictate ours.
9.9 Banking Enterprise Scenario
A bank integrates internal services and external partners through a blended architecture. Customer-facing reads go through a gateway to services over resilient HTTP with Polly. State changes - payments, account updates - are published as events via the outbox pattern onto Azure Service Bus topics, where notification, fraud, and ledger services subscribe independently and process idempotently. A flaky third-party FX provider sits behind an anti-corruption layer and a circuit breaker, so its instability never propagates. Every hop carries a correlation ID, letting operations trace a single payment across the entire estate.
Chapter Revision Notes
- Choose sync for immediate request/response, async for decoupling and workflows.
- Harden synchronous calls with timeouts, retries, and circuit breakers.
- Solve the dual write with the transactional outbox pattern.
- Make consumers idempotent; use dead-letter queues for poison messages.
- Order within a key via partitions/sessions, not globally.
- Wrap external systems in anti-corruption layers; trace every hop.