Implementing Service-to-Service Communication
1. Implementing REST Client Logic
Example: Spring RestClient (modern)
RestClient client = RestClient.builder()
.baseUrl("https://payments.internal")
.defaultHeader("Authorization", "Bearer " + token)
.requestFactory(new HttpComponentsClientHttpRequestFactory())
.build();
Receipt r = client.post()
.uri("/charges")
.contentType(MediaType.APPLICATION_JSON)
.body(new ChargeRequest(...))
.retrieve()
.body(Receipt.class);
2. Implementing gRPC Client/Server
Example: Server stub
public class OrderServiceImpl extends OrderServiceGrpc.OrderServiceImplBase {
public void place(PlaceOrderRequest req, StreamObserver<OrderResponse> obs) {
var o = service.place(toDomain(req));
obs.onNext(toProto(o));
obs.onCompleted();
}
}
3. Implementing Service Discovery Logic
| Approach | Detail |
|---|---|
| DNS | Kubernetes service DNS |
| Registry | Eureka, Consul, etcd |
| Service mesh | Istio, Linkerd |
| Client-side LB | Spring Cloud LoadBalancer |
4. Implementing Load Balancing Logic
| Algorithm | Use |
|---|---|
| Round-robin | Default, equal capacity |
| Least connections | Long-lived requests |
| Weighted | Heterogeneous nodes |
| Consistent hashing | Sticky sessions, cache affinity |
| EWMA / latency-based | Adaptive |
5. Implementing Circuit Breaker for External Calls
Example: Resilience4j
@CircuitBreaker(name = "fx", fallbackMethod = "stale")
@Retry(name = "fx")
@TimeLimiter(name = "fx")
public CompletableFuture<Rate> getRate(String pair) {
return CompletableFuture.supplyAsync(() -> client.fetch(pair));
}
public CompletableFuture<Rate> stale(String pair, Throwable t) {
return CompletableFuture.completedFuture(cache.get(pair));
}
6. Implementing Retry Logic for Service Calls
| Setting | Detail |
|---|---|
| Retryable codes | 5xx, 429, network |
| Non-retryable | 4xx (except 429) |
| Backoff | Exponential + jitter |
| Honor Retry-After | Server hint takes priority |
| Idempotency | Required for non-GET |
7. Implementing Service Mesh Patterns
| Capability | Detail |
|---|---|
| mTLS | Auto cert mgmt + rotation |
| Traffic shaping | Canary, A/B, mirror |
| Retries / timeouts | Mesh-level policy |
| Observability | Metrics, traces, logs out-of-the-box |
| AuthZ | Service identity policies |
8. Implementing API Gateway Communication
| Concern | Detail |
|---|---|
| Routing | Path/host → upstream |
| Auth | JWT validation, API key |
| Rate limit | Per consumer |
| Aggregation | BFF pattern |
| Tools | Spring Cloud Gateway, Kong, Envoy |
9. Implementing Saga Pattern
[Order] ──> [Reserve Inventory] ──> [Charge Payment] ──> [Ship] ↑ ↓ failure ↓ failure ↓ failure └── compensate: Release ←── compensate: Refund ←── compensate: Cancel
| Style | Detail |
|---|---|
| Choreography | Each service reacts to events |
| Orchestration | Central coordinator (e.g., Camunda) |
| Compensation | Define for every step |
10. Implementing Event-Driven Communication
| Pattern | Detail |
|---|---|
| Pub/sub | Loose coupling, fan-out |
| Outbox | Atomicity with DB write |
| Event sourcing | Events as truth |
| CDC | Debezium streams DB changes |
11. Implementing Synchronous vs Asynchronous
12. Implementing Service Versioning
| Approach | Detail |
|---|---|
| URI version | /v2/orders |
| Header version | API-Version: 2 |
| Side-by-side | Run v1 + v2 during migration |
| Deprecation policy | Sunset header, advance notice |
| Backward compat | Additive changes only within version |