Synchronous vs asynchronous communication between services
Model answer
Synchronous communication, such as HTTP/REST or gRPC, means the caller sends a request and waits for the response. It is simple to reason about, gives an immediate result, and fits queries and operations where the user needs the answer now. The downsides are temporal coupling: if the callee is slow or down, the caller is affected, and failures can cascade through a chain of calls. Timeouts, retries and circuit breakers help.
Asynchronous communication, such as messaging through a queue or an event broker, means the sender publishes a message and carries on. The receiver processes it later. This decouples services in time, absorbs traffic spikes, lets consumers scale independently, and allows several consumers per event. The costs are eventual consistency, harder debugging and tracing, ordering and duplicate-delivery concerns that need idempotent consumers, and the need to run a broker.
Use sync for request/response needs and async for workflows that can complete later, such as sending email or updating search indexes.
Key points
- Sync: simple and immediate, but couples availability and latency
- Async: decouples in time and absorbs spikes, but adds eventual consistency
- Async consumers must handle duplicates and ordering
Likely follow-ups
- How would you give the user feedback on an operation that completes asynchronously?
- What is a circuit breaker and when does it help?
Common mistakes
- Building long chains of synchronous calls without timeouts or fallbacks
- Using async messaging without idempotent consumers