When building .NET Microservices , one of the most important decisions is:

πŸ‘‰ How will microservices talk to each other?


πŸ“Œ What is Inter-Service Communication?

In Microservices Architecture, we break a big application into small services.

Example:

  • User Service

  • Product Service

  • Order Service

  • Payment Service

Now suppose:

Order Service needs Product details.

How will it get that?

It must communicate with Product Service.

That communication method is called:

Inter-Service Communication


πŸš€ Three Main Ways to Communicate in .NET Microservices
  1. REST API (HTTP-based)

  2. gRPC (High-performance binary protocol)

  3. Message Queue (Event-driven communication)

Let's understand each one in simple way.


1️⃣ REST API Communicationhttps://www.researchgate.net/publication/369358390/figure/fig1/AS%3A11431281127810255%401679180216268/HTTP-request-and-response-flow.png

4

πŸ“– What is REST?

REST (Representational State Transfer) is HTTP-based communication.

Services talk using:

  • GET

  • POST

  • PUT

  • DELETE

Data format:

  • JSON (mostly)


πŸ›  Example in .NET

Product Service Controller

Code
  
    [ApiController]
[Route("api/products")]
public class ProductController : ControllerBase
{
    [HttpGet("{id}")]
    public IActionResult GetProduct(int id)
    {
        return Ok(new { Id = id, Name = "Laptop", Price = 50000 });
    }
}

Order Service Calling Product Service

Code
  
    public class ProductServiceClient
{
    private readonly HttpClient _httpClient;

    public ProductServiceClient(HttpClient httpClient)
    {
        _httpClient = httpClient;
    }

    public async Task<string> GetProduct(int id)
    {
        return await _httpClient.GetStringAsync($"api/products/{id}");
    }
}

Register in Program.cs:

Code
  
    builder.Services.AddHttpClient<ProductServiceClient>
    (c => c.BaseAddress = new Uri("https://productservice"));

βœ… Advantages of REST

  • Simple

  • Easy to debug

  • Works everywhere

  • Human readable (JSON)

❌ Disadvantages

  • Slower than gRPC

  • More bandwidth usage

  • Tight coupling (synchronous)


2️⃣ gRPC Communication

4

πŸ“– What is gRPC?

gRPC is a high-performance communication protocol developed by:

Google

It uses:

  • HTTP/2

  • Protocol Buffers (binary format)

It is much faster than REST.


πŸ›  Example in .NET

Step 1: Define .proto file

Code
  
    syntax = "proto3";

service ProductService {
  rpc GetProduct (ProductRequest) returns (ProductResponse);
}

message ProductRequest {
  int32 id = 1;
}

message ProductResponse {
  int32 id = 1;
  string name = 2;
  double price = 3;
}

Step 2: Implement Service

Code
  
    public class ProductService : Product.ProductServiceBase
{
    public override Task<ProductResponse> GetProduct(ProductRequest request, ServerCallContext context)
    {
        return Task.FromResult(new ProductResponse
        {
            Id = request.Id,
            Name = "Laptop",
            Price = 50000
        });
    }
}

βœ… Advantages of gRPC

  • Very fast

  • Less bandwidth

  • Strongly typed

  • Great for internal microservices

❌ Disadvantages

  • Harder to debug

  • Not browser friendly

  • Requires HTTP/2


3️⃣ Message Queue (Event-Driven Communication)

4

πŸ“– What is Message Queue?

Instead of direct communication:

Service A β†’ sends message β†’ Queue β†’ Service B reads message.

Popular message brokers:

  • RabbitMQ

  • Apache Kafka

  • Azure Service Bus


πŸ›  Example Using RabbitMQ in .NET

Producer (Order Service)

Code
  
    var factory = new ConnectionFactory() { HostName = "localhost" };
using var connection = factory.CreateConnection();
using var channel = connection.CreateModel();

channel.QueueDeclare(queue: "orderQueue",
                     durable: false,
                     exclusive: false,
                     autoDelete: false,
                     arguments: null);

var body = Encoding.UTF8.GetBytes("Order Created");
channel.BasicPublish(exchange: "",
                     routingKey: "orderQueue",
                     basicProperties: null,
                     body: body);

Consumer (Payment Service)

Code
  
    var consumer = new EventingBasicConsumer(channel);
consumer.Received += (model, ea) =>
{
    var message = Encoding.UTF8.GetString(ea.Body.ToArray());
    Console.WriteLine("Received: " + message);
};

channel.BasicConsume(queue: "orderQueue",
                     autoAck: true,
                     consumer: consumer);

βœ… Advantages of Message Queue

  • Asynchronous

  • Loose coupling

  • Highly scalable

  • Fault tolerant

❌ Disadvantages

  • More complex

  • Debugging is harder

  • Eventual consistency


πŸ”₯ REST vs gRPC vs Message Queue (Comparison Table)
FeatureRESTgRPCMessage Queue
TypeSynchronousSynchronousAsynchronous
SpeedMediumVery FastFast
Data FormatJSONBinary (Proto)JSON/Binary
CouplingTightMediumLoose
Best ForPublic APIsInternal servicesEvent-driven systems

🧠 When Should You Use What?

βœ… Use REST When:

  • Public APIs

  • External clients

  • Browser-based apps

  • Simplicity required


βœ… Use gRPC When:

  • Internal microservices

  • High performance needed

  • Real-time systems


βœ… Use Message Queue When:

  • Event-driven architecture

  • Background processing

  • Payment processing

  • Notifications

  • High scalability required


πŸ— Real-World Architecture Example

E-Commerce Microservices:

  • User Service β†’ REST

  • Order Service β†’ gRPC to Product Service

  • Order Created Event β†’ Message Queue

  • Payment Service β†’ consumes event

  • Notification Service β†’ consumes event

Modern systems combine all three.


🌍 Official Learning Resources

Microsoft Docs – REST:
https://learn.microsoft.com/en-us/aspnet/core/web-api

Microsoft Docs – gRPC:
https://learn.microsoft.com/en-us/aspnet/core/grpc

RabbitMQ Official:
https://www.rabbitmq.com/tutorials

Kafka Official:
https://kafka.apache.org/documentation


πŸ† Final Conclusion

There is no "best" communication method.

βœ” REST β†’ Simple & universal
βœ” gRPC β†’ High performance
βœ” Message Queue β†’ Scalable & event-driven

Modern .NET Microservices use a hybrid approach.