IConfiguration vs IOptions NET
Synchronous and Asynchronous in .NET Core
Model Binding and Validation in ASP.NET Core
ControllerBase vs Controller in ASP.NET Core
ConfigureServices and Configure methods
IHostedService interface in .NET Core
ASP.NET Core request processing
| Service Per Business Capability Pattern :👈 | 👉:Database per Service Pattern |
Asynchronous Messaging Pattern |
Asynchronous Messaging is a communication pattern where messages are sent between components without waiting for an immediate response.
Asynchronous Messaging is a communication style where one microservice sends a message to another service without waiting for an immediate response.
Instead of:
Service A ---> Service B ---> Response
(Wait)
the sender publishes a message and continues its work:
Service A ---> Message Queue ---> Service B Service A does not wait.
Asynchronous Messaging allows microservices to communicate by exchanging messages through a message broker (RabbitMQ, Kafka, Azure Service Bus, etc.) without being directly dependent on each other's availability.
Imagine an E-Commerce application.
When a customer places an order, multiple actions are required:
If we use synchronous communication:
Order Service
│
├── Payment Service
├── Inventory Service
├── Invoice Service
├── Email Service
└── Shipping Service
Order Service waits for every service.
Problems:
RabbitMQ / Kafka
│
▼
Order Service ---> OrderCreated Event
│
┌─────────────────┼────────────────┐
│ │ │
▼ ▼ ▼
Payment Service Inventory Service Email Service
Order Service publishes an event and immediately responds.
"Order Created Successfully"
Other services process the event independently.
Think of sending an email.
You call your friend. You wait until he answers.
You send an email. Continue your work. Friend reads it later.
This is exactly how asynchronous messaging works.
The service that sends messages.
Example:
Order Service
Produces:
OrderCreated Event
Stores and routes messages.
Popular Brokers:
Service that receives messages.
Example:
Payment Service Inventory Service Notification Service
Customer places order:
Customer
│
▼
Order Service
│
▼
Publish OrderCreated Event
│
▼
RabbitMQ
│
┌──┼─────────────┬───────────┐
│ │ │ │
▼ ▼ ▼ ▼
Payment Inventory Notification
Service Service Service
Each service reacts independently.
Shared contract:
public class OrderCreatedEvent
{
public int OrderId { get; set; }
public int CustomerId { get; set; }
public decimal Amount { get; set; }
}
Install:
MassTransit MassTransit.RabbitMQ
Publish event:
public class OrderService
{
private readonly IPublishEndpoint _publishEndpoint;
public OrderService(IPublishEndpoint publishEndpoint)
{
_publishEndpoint = publishEndpoint;
}
public async Task CreateOrder()
{
var order = new Order
{
Id = 1001,
CustomerId = 10,
Amount = 500
};
await _publishEndpoint.Publish(
new OrderCreatedEvent
{
OrderId = order.Id,
CustomerId = order.CustomerId,
Amount = order.Amount
});
Console.WriteLine("Event Published");
}
}
Order Service finishes immediately after publishing.
public class OrderCreatedConsumer :
IConsumer<OrderCreatedEvent>
{
public async Task Consume(
ConsumeContext<OrderCreatedEvent> context)
{
var message = context.Message;
Console.WriteLine(
$"Processing payment for Order {message.OrderId}");
await Task.CompletedTask;
}
}
builder.Services.AddMassTransit(x =>
{
x.AddConsumer<OrderCreatedConsumer>();
x.UsingRabbitMq((context, cfg) =>
{
cfg.Host("localhost");
cfg.ReceiveEndpoint("payment-queue", e =>
{
e.ConfigureConsumer<OrderCreatedConsumer>(context);
});
});
});
Asynchronous Messaging is often implemented using Events.
Examples:
OrderCreated PaymentCompleted ProductAdded InventoryUpdated CustomerRegistered
Publisher:
Order Service
Event:
OrderCreated
Subscribers:
Payment Service Shipping Service Email Service
Order Service
│
▼
Payment Service
│
▼
Inventory Service
│
▼
Email Service
Total Time:
500ms + 600ms + 800ms = 1900ms
Order waits for everything.
Order Service
│
▼
Publish Event
Response sent immediately
Total Time:
100ms
Other services continue later.
Services are independent.
Order Service doesn't know who processes the event.
User receives response faster.
Order Created Successfully
without waiting for emails or shipping.
Example:
Payment Service
10 Instances
Email Service
2 Instances
Each service scales independently.
Suppose Email Service is down.
Order Service → Working Payment Service → Working Inventory Service → Working Email Service → Down
Messages remain in the queue until Email Service becomes available.
Supports modern patterns:
Sometimes the same message may be processed more than once.
Example:
OrderCreated OrderCreated
Solution:
Idempotency
Process the same event safely multiple times.
Data is not updated instantly across services.
Example:
Order Created Inventory updates after 2 seconds
Temporary differences may exist.
Request flow becomes:
Order Service ↓ RabbitMQ ↓ Payment Service ↓ Inventory Service
Distributed tracing tools are needed.
| Broker | Usage |
|---|---|
| RabbitMQ | Most popular for .NET microservices |
| Kafka | High-volume event streaming |
| Azure Service Bus | Microsoft cloud environments |
| AWS SQS | AWS applications |
| ActiveMQ | Enterprise messaging |
Asynchronous Messaging is a communication pattern in microservices where services exchange messages through a message broker without waiting for an immediate response. The sender publishes an event or message and continues processing, while consumer services handle the message independently. This approach improves scalability, fault tolerance, performance, and loose coupling. Common implementations in .NET use RabbitMQ, Azure Service Bus, Kafka, and MassTransit.
| Service Per Business Capability Pattern :👈 | 👉:Database per Service Pattern |