How do microservices communicate with each other?

Microservices communicate with each other through well-defined interfaces, either synchronous HTTP-based APIs or asynchronous messaging, without needing to know each other's internal implementation.

Key Points: • REST APIs over HTTP are the most common synchronous option, exchanging data usually in JSON format. • Messaging systems like Kafka or RabbitMQ enable asynchronous communication, where a service publishes a message and doesn't wait for it to be processed. • Communication happens through a contract, such as an OpenAPI spec or an event schema, so services can evolve independently as long as the contract is honored. • Service discovery (like Eureka or Kubernetes DNS) lets services find each other's network location dynamically rather than relying on hardcoded addresses. • The choice between synchronous and asynchronous communication depends on whether the caller needs an immediate response or can proceed without waiting.

Example: A Shopping Cart service might call a Pricing service synchronously over REST to get the current price of an item, while separately publishing a CartUpdated event to a Kafka topic that an Analytics service consumes later without the Cart service waiting on it.

Interview Tip: A concise interview answer is:

"Microservices talk to each other through APIs, either synchronous REST calls when an immediate response is needed, or asynchronous messaging through something like Kafka or RabbitMQ when the caller doesn't need to wait. Either way, they interact only through a well-defined contract, not by sharing internal implementation details."