Microservice architecture: Synchronous and Asynchronous communication

In the last few years, microservices become one of the most important architectural patterns to be used in application development. It is often important to mention that, choosing between monolith and microservices we should understand their theoretical and implementational aspects.

In a microservices architecture, communication between services can be either synchronous or asynchronous. Both have their advantages and disadvantages, and the choice of which to use depends on the specific scenario.

Synchronous Communication

Mechanism: The requesting service sends a message and waits for a response from the receiving service before continuing.

Example: A user clicks "buy now" on an online store. The payment service sends a request to the order service, waits for confirmation, and then updates the UI accordingly.

Advantages

  • Simple and predictable: The flow of execution is straightforward.
  • Immediate feedback: The requestor receives the response immediately, making it suitable for interactive applications.

Disadvantages

  • Blocking: The requestor is blocked until the response arrives, which can lead to performance issues if the receiving service takes a long time.
  • Coupling: Services become tightly coupled, making them more difficult to change and scale independently.

Synchronous Communication

Real-world examples of synchronous communication in microservices

1. Shopping Cart Checkout

  • Flow: The user adds items to the cart, and clicks "Checkout."
  • Synchronous communication: The cart service sends order requests to the payment service.
  • Benefits: Immediate feedback for the user, ensures payment is processed before proceeding to order creation.

2. Fraud Detection

  • Flow: Order service receives orders, and sends requests to the fraud detection service.
  • Synchronous communication: Fraud detection service analyzes order data, and returns approval/rejection instantly.
  • Benefits: Real-time decision making, prevents fraudulent orders before processing.

3. Live Chat Support

  • Flow: The user enters a chat message, and the system sends it to the chat agent service.
  • Synchronous communication: The agent receives messages, and responds to the user in real-time.
  • Benefits: Immediate interaction, builds trust and engagement with users.

4. Online Multiplayer Gaming

  • Flow: Player actions trigger messages sent to the game server.
  • Synchronous communication: The server updates the world state based on messages, and sends the updated world to all players in real-time.
  • Benefits: Smooth and responsive gameplay experience for all players.

5. Stock Market Trading

  • Flow: The trading platform sends order requests to the brokerage service.
  • Synchronous communication: Brokerage service executes a trade, sends confirmation, and updates the account balance back to the platform.
  • Benefits: Immediate execution and feedback, critical for managing risk and positions.

These are just a few examples, and in general, you should understand that synchronous communication is ideal when you need:

  1. Immediate feedback and interaction/ Real-time dialogue
  2. Real-time decision-making and execution/ Just-in-time analysis and implementation
  3. Tightly coupled workflows with high dependency

Asynchronous Communication

The most popular communication between microservice elements is Asynchronous Communication. It has a lot of benefits over synchronous ones.

Let's dive into details and try to understand Asynchronous Communication.

Mechanism: The requesting service sends a message to the receiving service without waiting for a response. The response is delivered later, either through a callback or a separate channel.

Example: A user uploads a large file to a cloud storage service. The upload service sends a message to the processing service and continues, notifying the user when the processing is complete.

Advantages

  • Non-blocking: The requestor can continue processing without waiting for the response, improving performance and scalability.
  • Decoupling: Services are loosely coupled, making them easier to change and scale independently.
  • Resilience: Asynchronous communication can handle failures and retries more gracefully than synchronous communication.

Disadvantages

  • Complexity: Implementing asynchronous communication can be more complex than synchronous communication.
  • Delayed feedback: The requestor may not receive the response immediately, which can be inconvenient for some applications.

Asynchronous Communication

Real-world examples of asynchronous communication in microservices

1. Email Confirmation and Notifications

  • Flow: User registers, order is placed, or action is completed.
  • Asynchronous communication: Registration service sends message to email queue. Notification service consumes messages later and sends emails.
  • Benefits: Decoupling of services, improved scalability, emails sent even if registration/order fails temporarily.

2. Background Processing and Data Analysis

  • Flow: The application receives large data files or performs complex calculations.
  • Asynchronous communication: The application sends data/tasks to the processing queue. Processing service consumes messages and performs tasks in the background.
  • Benefits: Improved responsiveness of primary application, data analysis or processing without blocking the user interface.

3. Social Media Feed Updates

  • Flow: User posts content or interacts with others.
  • Asynchronous communication: Post service publishes a message to feed queue. The feed service subscribes to the queue and updates user feeds in the background.
  • Benefits: Scalable updates, avoids overloading the user interface with real-time updates, allows for efficient batch processing.

4. Video Transcoding and Thumbnail Generation

  • Flow: The user uploads a video.
  • Asynchronous communication: Upload service sends video to transcoding queue. The transcoding service consumes messages and generates different video formats and thumbnails in the background.
  • Benefits: Improves user experience by providing different video formats later, and avoids delaying upload while processing.

5. Order Fulfillment and Shipment Tracking

  • Flow: Order service confirms the order and transmits details to the fulfillment center.
  • Asynchronous communication: The fulfillment center receives order details through a message queue, processes the order, and sends shipment tracking information later.
  • Benefits: Decoupling of services, avoids blocking order confirmation while fulfillment happens and allows for flexible and scalable fulfillment processes.

These examples showcase how asynchronous communication empowers microservices architecture by decoupling services, improving scalability, and enabling background processing without impacting critical user interactions. Remember, asynchronous communication is ideal when:

  • Immediate response is not essential
  • Background tasks or long-running processes are needed
  • Decoupling and scalability are key considerations

Ultimately, the choice between synchronous and asynchronous communication depends on the specific needs of your microservices. Here are some general guidelines:

  1. Use synchronous communication for tasks that require immediate feedback, such as user interactions or critical operations.
  2. Use asynchronous communication for tasks that can be processed independently, such as background tasks, data processing, and notifications.

You may use hybrid approaches that combine synchronous and asynchronous communication for optimal flexibility.


Similar Articles