Volume 4 · Chapter 5 — .NET Distributed Systems and Microservices Architecture
Inter-Service Communication
5.1 Introduction
How services talk to each other directly shapes the system’s scalability, reliability, and coupling. Enterprise systems combine synchronous communication (HTTP/gRPC) for immediate responses with asynchronous communication (events/messaging) for resilient, decoupled processing. The overarching lesson: choose sync for queries that need an immediate answer, async for state changes and workflows, and never make a remote call without a timeout and resilience protection.
5.2 Synchronous: REST and gRPC
REST/JSON is simple, widely supported, and easy to debug - good when a service needs an immediate response. gRPC (HTTP/2 + Protobuf) offers lower latency, smaller payloads, strong typed contracts, and streaming - ideal for high-frequency internal service-to-service calls. Both create temporal coupling: the caller depends on the callee being up and fast, so synchronous chains must be protected against failure.
// Typed gRPC client for internal high-performance calls, with resilience.
builder.Services.AddGrpcClient<Ledger.LedgerClient>(o => o.Address = new(ledgerUrl))
.AddStandardResilienceHandler(); // timeout + retry + circuit breaker (Polly-based)
// Resilient HttpClient for REST calls between services.
builder.Services.AddHttpClient<FraudClient>()
.AddStandardResilienceHandler(o =>
{
o.AttemptTimeout.Timeout = TimeSpan.FromSeconds(2);
o.Retry.MaxRetryAttempts = 3; // transient only, with backoff+jitter
o.CircuitBreaker.FailureRatio = 0.5;
});
5.3 Contracts, Discovery, and Timeouts
Service contracts must stay stable and backward-compatible so producers and consumers can deploy independently. Service discovery lets clients find instances whose locations change (DNS in Kubernetes, or a registry). And every remote call needs a timeout - the most important resilience control - because without one, a slow dependency can hold threads and resources indefinitely and cascade into a wider outage.
5.4 Resilience: Retries and Circuit Breakers
Retries with exponential backoff and jitter recover from transient failures - but only for transient errors, capped, and on idempotent operations, or they amplify load into a retry storm. Circuit breakers stop calling a dependency that is genuinely down, failing fast to protect both sides and giving it room to recover. These are covered in depth in Volume 7 and Chapter 9.
sequenceDiagram
autonumber
participant A as Service A
participant CB as Circuit Breaker
participant B as Service B
A->>CB: call
alt B healthy
CB->>B: request (timeout 2s)
B-->>CB: response
CB-->>A: result
else B failing repeatedly
CB-->>A: fail fast (circuit open) -> fallback
end 5.5 Choosing the Style
| Scenario | Approach |
|---|---|
| Immediate response needed | REST or gRPC (with resilience) |
| Long-running workflow | Messaging / async |
| Notify others of a state change | Events (pub/sub) |
When do you use REST/gRPC versus messaging between microservices?
The decision comes down to whether the caller needs an immediate response and how tightly the services should be coupled. I use synchronous communication - REST or gRPC - when a service needs an immediate answer to proceed: a query where the caller must have the data now, or a request/response interaction with simple semantics. Between REST and gRPC, I use REST/JSON for broad compatibility, external consumers, and simpler cases, and gRPC for internal, high-frequency, latency-sensitive service-to-service calls where its HTTP/2 transport, binary Protobuf encoding, typed contracts, and streaming give better performance. The trade-off of synchronous communication is temporal coupling: the caller depends on the callee being available and fast right now, so a slow or down callee propagates failure back and can cascade - which is why synchronous calls must always be wrapped in timeouts, retries, and circuit breakers. I use asynchronous messaging/events when the caller does not need an immediate response and I want loose coupling and resilience: state changes that other services should react to, long-running workflows, and load leveling of spiky traffic. With messaging, the producer hands a message to a durable broker and moves on; the consumer processes when it can, the broker absorbs bursts and outages, and multiple consumers can react independently - so a downstream service being down becomes a backlog rather than a failure, and new consumers can be added without changing the producer. The cost is asynchrony and eventual consistency. So the rule of thumb: synchronous (REST/gRPC) for queries and interactions needing an immediate result, accepting temporal coupling and hardening with resilience patterns; asynchronous (events/messaging) for state-change notifications, workflows, and anywhere loose coupling and resilience matter more than immediacy. Most real systems use both - synchronous where an answer is needed now, events for propagating what happened - and overusing synchronous calls in long chains is a common cause of fragile, coupled systems, so I bias toward events for cross-service reactions.
How do you prevent cascading failures in a system of synchronous service calls?
Cascading failure happens when one slow or failing service causes its callers to slow or fail, which causes their callers to slow or fail, until the whole system is degraded - synchronous chains are especially prone to it because a caller blocked waiting on a hung dependency holds threads and resources, and as those pile up the caller itself becomes unresponsive and passes the problem upstream. I prevent it with a layered set of resilience controls applied to every remote call. Timeouts first and foremost: every synchronous call has a bounded, sensibly short timeout, so a slow dependency can’t hold a request (and its thread) indefinitely - without timeouts, one hung service can exhaust a caller’s threads and take it down, which is the primary cascade mechanism. Circuit breakers: when a dependency is failing consistently, the breaker trips and the caller fails fast (or falls back) instead of continuing to hammer the dead dependency and pile up blocked calls - this both protects the caller from exhaustion and gives the dependency room to recover. Bulkheads: isolate resources (thread/connection pools) per dependency so calls to one slow dependency can only exhaust their own bounded pool, not the resources the rest of the service needs - containing the blast radius. Retries with bounded attempts, exponential backoff, and jitter for transient failures - but carefully, because naive aggressive retries amplify load on a struggling dependency (a retry storm) and make cascades worse, so retries are capped, backed off, jittered, and paired with circuit breakers. Fallbacks and graceful degradation: where the business allows, return a cached or default response when a non-critical dependency fails, so the failure degrades a feature rather than breaking the whole request. And crucially, reduce synchronous coupling itself: for interactions that don’t need an immediate response, use asynchronous messaging so a downstream outage becomes a backlog, not a cascading failure - the most robust protection is often not being synchronously coupled in the first place. Underpinning all of this is observability - monitoring latency, error rates, and circuit state - so I can see a dependency degrading and respond, and I validate the whole thing with fault-injection/chaos testing so I know the patterns actually engage. In .NET I compose timeouts, retries, circuit breakers, and bulkheads with the standard resilience handler (Polly). So the answer is: bound every call with timeouts, fail fast with circuit breakers, isolate with bulkheads, retry transient failures carefully with backoff, degrade gracefully with fallbacks, and reduce synchronous coupling with async messaging where possible - which together stop one failing service from taking down the system, turning cascading failures into contained, recoverable ones.
5.6 Banking Enterprise Scenario
A banking payment flow needs immediate customer and account validation (synchronous gRPC/REST with tight timeouts, retries, and circuit breakers) but publishes events for notifications, auditing, and fraud analysis (asynchronous, decoupled). When the fraud service slowed during peak, the circuit breaker and a cached-risk fallback kept core payment processing responsive, and because notifications were event-driven, their backlog never affected the payment path - synchronous where an answer is needed, events everywhere else, with resilience throughout.
Chapter Revision Notes
- Use REST/gRPC for immediate responses; gRPC for internal high-performance calls.
- Use events/messaging for state changes, workflows, and loose coupling.
- Keep contracts stable and backward-compatible; use service discovery for changing locations.
- Every remote call needs a timeout - the foundational resilience control.
- Prevent cascades with timeouts, circuit breakers, bulkheads, careful retries, fallbacks, and async decoupling.