Designing Service Communication Patterns

1. Designing Synchronous Communication

ProtocolStrengthWatch Out
REST/HTTPUniversal, debuggableVerbose, no streaming
gRPCBinary, multiplexed, streamingBrowser needs proxy
GraphQLClient-shaped responsesCaching complex
Sync riskCascading failure, latency stacking

2. Designing Asynchronous Communication

PatternDetail
Event-drivenPub/sub via Kafka, NATS
Message queueWork queue (SQS, RabbitMQ)
ProsDecoupled, resilient, buffer load
ConsEventual consistency, harder debugging

3. Designing Request-Reply Pattern

ChannelMechanism
HTTPNative request/response
MQ-basedReplyTo + correlation_id; temp queue
Async + callbackWebhook URL + correlation
RPC over MQRabbitMQ RPC, NATS req/reply

4. Designing Publish-Subscribe Pattern

ComponentDetail
TopicLogical channel
ProducerPublishes events; doesn't know consumers
Consumer groupParallel processing within group
Fan-outMultiple groups → independent reads
ToolsKafka, Pulsar, Redis Streams, GCP Pub/Sub

5. Designing Event-Driven Communication

Event TypeUse
Domain eventPast-tense fact (OrderPlaced)
Notification event"Something happened, ask me for details"
State transferIncludes entity snapshot
Command (event-like)Async work request

6. Designing Service Timeout Strategy

TypeRecommendation
Connection timeout1–2s
Read timeoutp99 + buffer (often 1–3s)
Total request timeoutEnd-to-end budget
Cascading ruleCaller timeout < sum of callee budgets

7. Designing Retry Policies with Exponential Backoff

Example: Exponential backoff with jitter

long base = 100, cap = 5000;
for (int i = 0; i < maxRetries; i++) {
    try { return call(); }
    catch (Transient e) {
        long expo = Math.min(cap, base * (1L << i));
        long sleep = ThreadLocalRandom.current().nextLong(0, expo); // full jitter
        Thread.sleep(sleep);
    }
}
throw new RetriesExceeded();
PolicyDetail
Only retry transient5xx, timeout; not 4xx
Cap retries3–5
Add jitterAvoid thundering herd
Idempotency requiredFor non-GET

8. Designing Circuit Breaker Architecture

ParamTypical
Failure threshold50% over 20 reqs
Open duration30s
Half-open trial calls5
Per-dependencyOne breaker per downstream
MetricsState changes, rejection count

9. Designing Bulkhead Pattern

ImplementationDetail
Thread pool isolationPer dependency or tenant
SemaphoreBound concurrent calls
Connection pool per upstreamOne slow dep doesn't drain all
Pod isolationCritical workloads on dedicated nodes

10. Designing Service-to-Service Authentication

MethodDetail
mTLSSPIFFE/SPIRE, Istio auto-cert
Service JWTWorkload identity (OIDC)
API keyStatic; rotate regularly
OAuth2 client credentialsCentralized issuer

11. Designing Correlation ID Propagation

HeaderPurpose
traceparent (W3C)Trace ID + parent span
tracestateVendor-specific
x-correlation-idBusiness-level request id
PropagationHTTP headers + MQ message attributes

12. Designing Service Contracts

TypeTool
RESTOpenAPI 3.1
gRPCProtobuf .proto
Async/eventsAsyncAPI + Schema Registry
ValidationServer-side validation + consumer-driven contracts