Synchronous and Asynchronous Communication between Microservices

2805 views Designing μ-services

Say, we are building a Social Network and anytime someone reacts to your post, you need to be notified. So, how should the Reaction service talk to the Notification service to send out a notification?

The communication would be much simpler and reliable, just a function call if it was a monolith; but things become tricky as we go distributed.

Microservices need to talk to each other to exchange information and get things done; and there are two categories of communication patterns - Synchronous and Asynchronous.

Synchronous Communication

Communication is synchronous when one service sends a request to another service and waits for the response before proceeding further.

The most common implementation of Sync communication is over HTTP using protocols like REST, GraphQL, and gRPC.

Advantages of Synchronous Communication

  • It is simple and intuitive
  • Communication happens in realtime

Disadvantages of Synchronous Communication

  • Caller is blocked until the response is received
  • Servers need to be pro-actively provisioned for peaks
  • There is a risk of cascading failures
  • The participating services are strongly coupled

When to use Synchronous Communication

  • When you cannot proceed without a response from the other service
  • When you want real-time responses
  • When it takes less time to compute and respond

Asynchronous Communication

The communication is asynchronous when the one service sends a request to another service and does NOT wait for the response; instead, it continues with its own execution.

Async communication is most commonly implemented using a message broker like RabbitMQ, SQS, Kafka, Kinesis, etc.

Advantages of Asynchronous Communication

  • Services do not need to wait for the response and can move on
  • Services can handle surges and spikes better
  • Servers do not need to be proactively provisioned
  • No extra network hop due to Load Balancer
  • No request drop due to target service being overwhelmed
  • Better control over failures and retires is possible
  • Services are truly decoupled

Disadvantages of Asynchronous Communication

  • Eventual consistency
  • Broker could become a SPoF
  • It is harder to track the flow of the message between services

When to use Asynchronous Communication

  • When delay in processing is okay
  • When the job at hand is long-running and takes time to execute
  • When multiple services need to react to the same event
  • When it is okay for the processing to fail and you are allowed to retry

Arpit Bhayani

Arpit's Newsletter

CS newsletter for the curious engineers

❤️ by 17000+ readers

If you like what you read subscribe you can always subscribe to my newsletter and get the post delivered straight to your inbox. I write essays on various engineering topics and share it through my weekly newsletter.

Other essays that you might like

BFF - Backend for Frontend - Pattern in Microservices

2225 views 114 likes 2022-07-04

As your application evolves, supporting multiple types of clients like Desktop, Mobile apps, etc becomes tricky. The bac...

Best practices that make microservices integration easy

850 views 50 likes 2022-06-27

Running microservices in isolation does not make any sense. To get something done, multiple microservices need to talk t...

Things to remember while building Microservices

856 views 35 likes 2022-06-20

An engineer working on Microservices should not only just focus on engineering; there are so many other aspects to look ...

Why should we have a standard way of building Microservices?

631 views 27 likes 2022-06-17

We all love creating microservices, but what if every team creates its own microservice uniquely and uses its own conven...

Be a better engineer

A set of courses designed to make you a better engineer and excel at your career; no-fluff, pure engineering.

System Design Masterclass

A masterclass that helps you become great at designing scalable, fault-tolerant, and highly available systems.

Enrolled by 700+ learners

Details →

Designing Microservices

A free course to help you understand Microservices and their high-level patterns in depth.

Enrolled by 17+ learners

Details →

GitHub Outage Dissections

A free course to help you learn core engineering from outages that happened at GitHub.

Enrolled by 67+ learners

Details →

Hash Table Internals

A free course to help you learn core engineering from outages that happened at GitHub.

Enrolled by 25+ learners

Details →

BitTorrent Internals

A free course to help you understand the algorithms and strategies that power P2P networks and BitTorrent.

Enrolled by 42+ learners

Details →

Topics I talk about

Being a passionate engineer, I love to talk about a wide range of topics, but these are my personal favourites.

Arpit's Newsletter read by 17000+ engineers

🔥 Thrice a week, in your inbox, an essay about system design, distributed systems, microservices, programming languages internals, or a deep dive on some super-clever algorithm, or just a few tips on building highly scalable distributed systems.

  • v12.4.4
  • © Arpit Bhayani, 2022

Powered by this tech stack.