Advanced Backend Java· 45 min

Microservices and Service Communication

Choose service boundaries, communication styles, API gateways, and service discovery patterns.

Java interview lesson
Question 30 · Not reviewed

Distributed systems · architecture

Can you explain this clearly in an interview?

Use the strong answer as your reference, explain it aloud in your own words, then choose how soon you want to review it again.

Interview prompt

Microservices and Service Communication

Explain it aloud in your own words

Strong answer

Microservices trade in-process simplicity for independent deployment and team autonomy, so boundaries should follow business capabilities and data ownership rather than technical layers. Each service owns its data; other services interact through APIs or events, never through its tables. Choose synchronous calls (REST or gRPC) when the caller needs an immediate answer, and asynchronous messaging when work can complete later or many consumers react to the same fact. Gateways, discovery, and timeouts become first-class concerns.

Key points

  • A distributed monolith (services that must deploy together or share a database) has the costs of both styles and the benefits of neither.
  • Every remote call needs a timeout, a retry policy for safe operations, and a fallback decision.
  • An API gateway handles edge concerns such as routing, auth, and rate limits, not business logic.
  • Version contracts and use consumer-driven contract tests so services can deploy independently.

Interviewer looks for

Justify a boundary with a concrete domain example, compare synchronous and asynchronous communication for that case, and explain what happens when a downstream service is slow or down.

Java example

1@HttpExchange("/api/inventory")
2interface InventoryClient {
3  @GetExchange("/{sku}")
4  StockLevel stock(@PathVariable String sku);
5}
6
7@Configuration
8class ClientConfig {
9  @Bean
10  InventoryClient inventoryClient(RestClient.Builder builder) {
11    var factory = new SimpleClientHttpRequestFactory();
12    factory.setConnectTimeout(Duration.ofMillis(500));
13    factory.setReadTimeout(Duration.ofSeconds(2));
14    RestClient client = builder
15        .baseUrl("http://inventory-service")
16        .requestFactory(factory)
17        .build();
18    return HttpServiceProxyFactory
19        .builderFor(RestClientAdapter.create(client))
20        .build()
21        .createClient(InventoryClient.class);
22  }
23}

Topic interview questions

Revise this topic like an interviewer

These questions are tied to this lesson. Read each question aloud, then compare your explanation with the strong answer.

5 questions
Q1Intermediate

How do you decide microservice boundaries?

Strong answer

Start from business capabilities and bounded contexts, keep data that changes together and has shared invariants in one service, and align services with team ownership. If two services always change and deploy together or need each other's tables, the boundary is probably wrong.

Q2Basic

When would you choose synchronous REST or gRPC versus asynchronous messaging?

Strong answer

Use synchronous calls when the caller needs an answer to continue, such as a price check. Use messaging when work can complete later, many consumers react to one event, or you want to decouple availability. gRPC fits internal, high-throughput calls with strict schemas.

Q3Basic

Why should each microservice own its database?

Strong answer

Shared tables couple services through schema, so one team's migration can break another and services cannot deploy independently. Owning data means other services access it through a contract, which makes ownership, scaling, and change explicit.

Q4Intermediate

What does an API gateway do and what should it not do?

Strong answer

It provides a single entry point for routing, TLS termination, authentication, rate limiting, and request aggregation at the edge. It should not contain domain logic, or it becomes a coupling point that every team must change.

Q5Advanced

How do you prevent a slow downstream service from taking down its callers?

Strong answer

Set connect and read timeouts, bound concurrency with bulkheads, add a circuit breaker to fail fast, retry only idempotent calls with backoff and jitter, and define a fallback or degraded response. Monitor the dependency's latency so problems are visible early.

How well do you know this?

Your review choice is saved locally and synced to your learner profile when signed in.