Managing Inter-Service Communication

1. Using Synchronous Communication

ProtocolBest ForTrade-off
REST/HTTPPublic APIs, broad clientsVerbose, text-based
gRPCInternal high-perf, streamingBrowser support needs gRPC-Web
GraphQLAggregating UIsCaching complexity
DrawbackTight coupling; caller waits; failures cascade

2. Using Asynchronous Communication

PatternDescription
Event publishingProducer fires events; consumers subscribe
Command queueSingle consumer processes work item
ProsLoose coupling, buffering, resilience
ConsEventual consistency, debugging complexity

3. Implementing Request/Reply Pattern

FieldUse
reply-toQueue/topic where reply is sent
correlation-idMatch reply to request
TimeoutDrop request if no reply in N seconds
Use caseRPC over messaging (NATS request, RabbitMQ RPC)

4. Implementing Fire-and-Forget Pattern

AspectDetail
SemanticsSend and proceed; no reply expected
DeliveryAt-least-once via broker ack
IdempotencyRequired on consumer (dup possible)
Use casesLogging, audit, notifications, telemetry

5. Handling Service Timeouts

LayerDefault Timeout
Connect1–2s
Read/socket2–5s
Overall request1–10s (per SLA)
Background jobMinutes–hours; explicit per job
RuleCaller timeout > downstream timeout + retries

6. Implementing Retry Mechanisms

StrategyDetail
Exponential Backoffdelay = base * 2^attempt
JitterRandom offset prevents thundering herd
Max attempts3–5 typical
Retry-only-onIdempotent ops + transient errors (5xx, 429, network)
BudgetCap retries as % of total RPS to prevent retry storms

Example: Resilience4j retry (Java)

RetryConfig cfg = RetryConfig.custom()
    .maxAttempts(4)
    .intervalFunction(IntervalFunction.ofExponentialRandomBackoff(200, 2.0, 0.5))
    .retryOnException(e -> e instanceof IOException)
    .build();
Retry retry = Retry.of("payments", cfg);
Payment p = retry.executeSupplier(() -> client.charge(req));

7. Managing Connection Pooling

ParamGuideline
Max connections2–4× CPU cores per upstream
Idle timeout30–60s
Max lifetime10–30 min (refresh DNS, LB)
Acquire timeoutFail-fast if pool exhausted
HTTP/2Use multiplexing; fewer connections needed

8. Using Service Mesh

CapabilityProvided By Mesh
mTLSSidecar handles cert rotation
Retries/TimeoutsConfigured via CRDs, not code
Circuit BreakingOutlier detection
Traffic SplittingCanary, A/B, mirroring
TelemetryAuto metrics + traces

9. Handling Partial Failures

FailureStrategy
Optional dependency downGraceful degradation (return defaults)
Critical dependency downFail fast with clear error
Slow dependencyTimeout + circuit breaker
Partial dataReturn what's available + status flag

10. Implementing Bulkheads

TypeExample
Thread poolSeparate executor per downstream
Connection poolOne pool per upstream
ProcessDedicated workers per workload
ClusterTenant-isolated namespaces

11. Understanding Communication Trade-offs

Synchronous

  • + Simple mental model
  • + Immediate response
  • − Cascading failure
  • − Tight temporal coupling

Asynchronous

  • + Loose coupling, buffering
  • + Resilient to consumer downtime
  • − Eventual consistency
  • − Harder to reason/debug
Decision DriverPick
User-facing immediate responseSync
Long-running / fan-out / decoupledAsync

12. Implementing Correlation ID Propagation

HeaderDescription
X-Request-IDPer-request unique ID (UUID)
X-Correlation-IDBusiness workflow ID across services
traceparentW3C Trace Context (preferred)
tracestateVendor-specific extensions
baggageCross-cutting key-value (tenant, user)