Best practices to communicate between microservices

Communication types

  • Synchronous protocol: HTTP is a synchronous protocol. The client sends a request and waits for a response from the service. That’s independent of the client code execution that could be synchronous (thread is blocked) or asynchronous (thread isn’t blocked, and the response will reach a callback eventually). The important point here is that the protocol (HTTP/HTTPS) is synchronous and the client code can only continue its task when it receives the HTTP server response.
  • Asynchronous protocol: Other protocols like AMQP (a protocol supported by many operating systems and cloud environments) use asynchronous messages. The client code or message sender usually doesn’t wait for a response. It just sends the message to a message broker service e.g, RabbitMQ or Kafka (if we’re using event-driven architecture).

Why you should avoid Synchronous protocol

  • If you keep on adding new microservices that are communicating with each other then consuming endpoints within code will create a mess especially when you have to pass extra information in the endpoint. e.g, auth token.
  • You have to wait for the time-consuming calls to get a response.
  • If the response fails and you have a retry policy in place then it can create a bottleneck.
  • If receiver service is down or not able to process the request then we want to wait until the service is up. e.g, In e-commerce site, user places an order and request is sent to shipment service to ship the order, but shipment service is down and we lost the order. How to send same order to shipment service once it's up?
  • The receiver might not be able to handle a lot of requests at a time, so there should be a place where requests have to wait until the receiver is ready to process the next request.

How to use RabbitMQ to handle communication between microservices

Image explains where rabbitMQ will fit in microservices
RabbitMQ Exchange

Exchange types

  • order.logs.customer
  • order.logs.international
  • order.logs.customer.electronics
  • order.logs.international.electronics

Implement RabbitMQ

Installation

RabbitMQ dashboard

Create sender service

  1. Sender: To send messages to RabbitMQ
  2. Receiver: To receive messages from RabbitMQ
The above code will create a connection to RabbitMQ, creates a queue ‘hello’ and publishes a message to the queue.
The above code will create a connection to RabbitMQ, create a queue (if it’s not created yet), and register a handler that will receive and process the message.

NserviceBus Configuration:

Summary

--

--

--

Entrepreneur, Software Engineer, Founder of mind-mapped.com & dealkar.pk

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

AWS Private API Gateway with Custom domain names

GSoC 2018: BNF Grammar, Parsec, and the YAML Spec

Technical Development Progress Update #8 12 Jan

A few valuable things from my journey in a web development boot camp.

PowerDirector Mod Apk No Watermark Download

PowerDirector Mod Apk No Watermark Download

A Panicked GUI Girl never wants to face a black screen with cryptic white text on her Linux laptop…

Squids or Ice cream cones

Garnet Silver beauty jewellery Earring Red L-0.2in UK gift

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Irfan Yusanif

Irfan Yusanif

Entrepreneur, Software Engineer, Founder of mind-mapped.com & dealkar.pk

More from Medium

Best practice and Cheat sheet for REST API Design

The Clean Architecture — Beginner’s Guide

API & Web Architecture — Security Best Practices