Inter-Service Communication
TechLearningPRO Orders rarely owns every fact needed to place a cart: prices live in Pricing, stock in Inventory, notifications in a mailer.
Introduction
TechLearningPRO Orders rarely owns every fact needed to place a cart: prices live in Pricing, stock in Inventory, notifications in a mailer. Inter-service communication is how those Boot 3 services exchange data without becoming a distributed ball of mud.
Choose the transport by latency, coupling, and durability: synchronous HTTP/REST or gRPC when the caller needs an immediate answer; async messaging (Kafka/RabbitMQ) when work can complete later and you want blast-radius isolation.
On Spring Boot 3 / Java 21, prefer Spring 6 declarative HTTP interfaces (@HttpExchange) over legacy Feign for new sync clients, and keep timeouts, retries, and circuit breakers at the edge of every outbound call.
This lesson wires Orders to talk safely to its neighbors so later Docker and Kubernetes lessons ship a real multi-service estate.
Purpose of this lesson
Pick sync vs async communication for Orders and implement Spring 6 HTTP clients with timeouts on Boot 3 / Java 21.
Understanding the topic
Three common patterns, three different failure modes.
- HTTP/REST: universal, easy to test, caller waits — couples on availability.
- gRPC: binary, schema-first, excellent for internal high-throughput hops.
- Async messaging: durable, decoupled, eventually consistent — ideal for events.
- Idempotency: retries must not double-charge or double-ship Orders.
- Contracts: version APIs; never break consumers silently.
Step-by-step explanation
- Map each Orders dependency: quote (sync), reserve stock (sync), order.placed event (async).
- Add a declarative Pricing client with
WebClient+HttpServiceProxyFactory. - Set connect/read timeouts and a Resilience4j circuit breaker on the client.
- Publish
OrderPlacedEventto Kafka after commit; consumers stay eventually consistent.
Informative example
Spring 6 declarative HTTP client for Pricing (Boot 3).
public interface PricingClient {@GetExchange("/api/v1/quotes")Quote quote(@RequestParam List<Long> itemIds);}@Configurationclass PricingClientConfig {@BeanPricingClient pricingClient(WebClient.Builder builder) {WebClient wc = builder.baseUrl("http://pricing-service").clientConnector(new ReactorClientHttpConnector(HttpClient.create().responseTimeout(Duration.ofSeconds(2)))).build();return HttpServiceProxyFactory.builderFor(WebClientAdapter.create(wc)).build().createClient(PricingClient.class);}}@Servicepublic class CheckoutFacade {private final PricingClient pricing;private final OrderRepository orders;public CheckoutFacade(PricingClient pricing, OrderRepository orders) {this.pricing = pricing;this.orders = orders;}@Transactionalpublic OrderView place(CreateOrderRequest cmd) {Quote quote = pricing.quote(cmd.itemIds());return OrderView.from(orders.save(OrderEntity.from(cmd, quote)));}}
Real-world use
TechLearningPRO once called Inventory with an unbounded RestTemplate. A inventory GC pause hung checkout threads until the whole Orders pod became unresponsive. Switching to WebClient with a 2s response timeout and a circuit breaker kept checkout accepting orders while Inventory recovered — degraded quotes instead of a site outage.
Security implications
Service-to-service calls are an auth boundary.
- Use mTLS or short-lived service tokens — never bake long-lived keys into images.
- Do not log Authorization headers or payload secrets.
- Reference credentials only via env placeholders such as
${PRICING_CLIENT_TOKEN}.
Best practices
- Sync for request/response; async for side effects and fan-out.
- Prefer Spring 6 HTTP interfaces over Feign in new Boot 3 code.
- Always bound latency: timeouts + bulkheads + breakers.
Common mistakes
- Chatty sync fan-out on every page load with no bulk endpoints.
- Retries without idempotency keys that duplicate payments.
- Hard-coding hostnames instead of service discovery or config.
Advanced interview questions
Interview Prep
Practice concise answers, then expand each card for the explanation.
1BeginnerQuestionWhen choose async messaging over HTTP for Orders?+
Answer
2IntermediateQuestionWhy prefer Spring 6 @HttpExchange clients over Feign in Boot 3?+
Answer
3IntermediateQuestionWhat must accompany retries on place-order?+
Answer
Hands-on exercise
Lab. Add a declarative PricingClient with a 2s timeout. Force Pricing to sleep 5s and confirm Orders fails fast. Then emit an OrderPlacedEvent after a successful save and verify a stub consumer receives it once.
Summary
Inter-service communication keeps TechLearningPRO Orders honest about dependencies. Next: package those services as Docker images for microservices.