Services in a microservice architecture communicate through APIs, either synchronously over HTTP/HTTPS using REST or GraphQL, or asynchronously through message brokers like Kafka or RabbitMQ.
Key Points: • REST is the most common synchronous style, exchanging JSON over HTTP, while GraphQL lets clients request exactly the fields they need in a single call. • Message brokers enable asynchronous, decoupled communication where the sender publishes a message without waiting for the receiver to process it. • Synchronous APIs are simpler to reason about but couple the caller's availability to the callee's uptime and response time. • Asynchronous messaging improves scalability and reliability since services don't need to be online at the same instant to communicate. • The choice depends on the use case: synchronous APIs fit request/response needs, while messaging fits event notification and background processing.
Example: A Checkout service might synchronously call an Inventory service's REST API to check stock before confirming an order, while asynchronously publishing an OrderPlaced event to Kafka so a separate Analytics service can process it whenever it's ready.
Interview Tip: A concise interview answer is:
"Services communicate through APIs, synchronously via REST or GraphQL over HTTP when an immediate answer is needed, and asynchronously via message brokers like Kafka or RabbitMQ when they just need to notify others of something that happened. Mixing both lets each interaction use the style that fits its actual consistency and latency needs."