Asynchronous communication with queues and microservices - A perfect combination?


In this article, we throw some light on what asynchronous messaging is all about and discuss why you should consider it for your microservices architectures.

What you will learn

  • What is Asynchronous Communication?
  • Why Do We Need Asynchronous Communication?
  • How is Asynchronous Communication implemented?
  • Why should you consider using Asynchronous Communication for your microservices?

Best Practices with Cloud and Microservices

This is the last article in a series of six articles on best practices with cloud and microservices:

What Is Synchronous Communication?

Consider the following example of a microservices architecture:

image info

You have Microservice1 calling Microservice2, which in turn invokes Microservice3. Suppose that Microservice3 offers a simple web service functionality. So Microservice1 can send it a request for data over HTTP, and get back a response containing the requested data.

The communication that exists between these two microservices is called synchronous communication. Microservice1 sends the request, waits for the data to be returned, and then proceeds.

Drawbacks Of Synchronous Communication

This mode of communication works well when the response arrives almost immediately. However, it puts restrictions on the microservices that are involved. In order that Microservice1 be available, Microservice2 also needs to be available.

In certain scenarios, synchronous communication might not even be user friendly.

Let’s say Microservice2 is down. In that case, the user who submitted the request needs to be intimated about it. You don’t always want to do that. In such situations, asynchronous communication provides a better alternative.

Asynchronous Communication

Have a look at the following application architecture:

image info

In the picture above, we make use of a ZipkinDistributedTracingServer. Different Microservices send logs that are finally consolidated in the Zipkin Tracing Service. Each microservice puts its tracing information into the RabbitMQ message queue. The server processes messages as and when they come into the queue.

In this architecture, applications communicate through RabbitMQ. CurrencyCalculationService puts its own tracing information into RabbitMQ, and then forgets about it. It does not worry about the responses coming back to it.

Now, what would happen if the ZipkinDistributedTracingServer were to go down?

The services communicating with it would not worry one bit. They would continue sending messages to the queue. When the server comes back up, it resumes processing the messages present in the queue, and saves them to the database.

Advantages Of Asynchronous Communication

Let’s look at a few advantages of asynchronous communication:

  • In a system that involves asynchronous messaging, the server does not need to be up and running, all the time. Messages that are put into the message queue can be processed in batches, at a later time.
  • Instead of a single instance of the tracing server processing the message queue, you could spawn multiple instances to lighten the load.
  • If you use a modern version of a messaging queue, there is a good chance you get to use a replay feature. This helps in re-processing a message that initially threw an error.
  • Asynchronous communication is great for systems that require eventual consistency.

Great thing is as long as we fix the errors and reprocess the message, the users of services that initiated the requests with errors do not even need to know about them.

Limitations Of Asynchronous Communication

Asynchronous communication cannot be used

  • If the processing needs to be real-time - if there is a hard constraint on the response time of a certain request. Do check out our video on this:

image info

Summary

In this article, we had a look at what asynchronous communication is all about, We started with a peek at synchronous communication, and saw that it puts constraints on applications and their availability. Asynchronous communication solves the problem by causing services to enqueue requests into a message queue. A server then processes them independently, often in batches.

Congratulations! You are reading an article from a series of 50+ articles on Spring, Spring Boot , Hibernate, Full Stack, Cloud and Microservices. We also have 20+ projects on our Github repository. For the complete series of 50+ articles and code examples, click here.

Join 300,000 Learners!

Learn Spring Boot in 10 Steps - FREE Course

Next Steps

Image

Image

Image

Image

Image

Image

Image

Image

Image

Image

Image

Image

Related Posts

Spring Boot Tutorials for Beginners

At in28Minutes, we are creating a number of tutorials with videos, articles & courses on Spring Boot for Beginners and Experienced Developers. This resources will help you learn and gain expertise at Spring Boot.

Spring and Spring Boot Video Tutorials for Beginners

At in28Minutes, we are creating a number of tutorials with videos, articles & courses on Spring Boot for Beginners and Experienced Developers. Here's a list of video tutorials and courses for you

What is an In Memory Database?

In this article, we understand what the term "In-Memory Database" actually means. We see what scenarios they can be used, and why they are important.

What is an Embedded Server?

In this article, we focus in exploring what an embedded server is, and how they help make application deployment simpler.

Java Tools and Frameworks - Introduction To Maven

In this article, we experience a brief introduction to Maven, and will see how it helps in managing your project dependencies.

Five Tools To Learn as a Java Developer

In this article, we introduce you to five great tools that we recommend every Java Developer to learn.

Five Languages To Learn as a Java Programmer

As a programmer, you want to explore new things. Learning a new language gives you a different perspective when solving problems. What are the 5 languages that a Java developer should explore?

Five Great Frameworks To Try for Java Programmers

As a programmer you want to explore new languages and frameworks. What would be the best frameworks to explore in 2019 for Java Programmers?

Unit Testing Best Practices - with Java and Junit Examples

Unit Testing is often underrated. As far as I am concerned, having good unit tests seperates good projects from bad. In this article, let's look at unit testing best practices in depth. We use examples from Java and JUnit to illustrate the concepts. However, the best practices are applicable irrespective of the programming language used.

Programming Basics - Unit Testing - What Is Mocking?

Mocking is an important concept that is made use of, when writing unit tests. In this article, we explore what mocking is all about, and explore some of the popular Java mocking frameworks in use.