Microservices in .NET (gRPC, Messaging, Resilience)
Build distributed systems with gRPC for fast inter-service calls, MassTransit for event-driven messaging, and Polly for fault tolerance.
What You'll Learn
- • Build gRPC services with server streaming
- • Event-driven messaging with MassTransit and RabbitMQ
- • Resilience patterns: retry, circuit breaker, timeout with Polly
- • Publish/subscribe vs command patterns
🏙️ Real-World Analogy
Microservices are like departments in a company. gRPC is a direct phone call between departments — fast and synchronous. Messaging is like the internal mail system — drop a letter in the outbox and the right department picks it up when ready. Polly resilience is like having a backup supplier — if your primary vendor fails, you retry, then switch to an alternative.
gRPC — High-Performance Communication
gRPC uses Protocol Buffers for serialization — 10x faster than JSON REST. Define service contracts in .proto files, then generate strongly-typed clients and servers.
gRPC Service & Client
Build a product service with gRPC, including server streaming for large datasets.
// ══════════════════════════════════════════════
// gRPC Service — high-performance inter-service communication
// ══════════════════════════════════════════════
// product.proto — define the service contract
// syntax = "proto3";
// service ProductService {
// rpc GetProduct (ProductRequest) returns (ProductReply);
// rpc ListProducts (Empty) returns (stream ProductReply);
// }
// message ProductRequest { int32 id = 1; }
// message ProductReply { int32 id = 1; string name = 2; double
...Event-Driven Messaging
Use MassTransit with RabbitMQ for async communication. Events (publish/subscribe) notify all interested services. Commands (send) target one specific consumer.
MassTransit Pub/Sub & Commands
Publish events and send commands between microservices with MassTransit.
// ══════════════════════════════════════════════
// Messaging with MassTransit + RabbitMQ
// ══════════════════════════════════════════════
using MassTransit;
// Shared message contracts (in a shared NuGet package)
public record OrderCreatedEvent
{
public Guid OrderId { get; init; }
public string CustomerId { get; init; } = "";
public decimal Total { get; init; }
public DateTime CreatedAt { get; init; }
}
public record SendEmailCommand
{
public string To { get; init; } =
...Resilience with Polly
Services fail. Polly adds retry logic, circuit breakers, and timeouts to your HTTP clients so your system degrades gracefully instead of cascading failures.
Polly — Retry, Circuit Breaker, Timeout
Add resilience policies to HTTP clients for fault-tolerant service calls.
// ══════════════════════════════════════════════
// Resilience with Polly — retry, circuit breaker, timeout
// ══════════════════════════════════════════════
using Microsoft.Extensions.Http.Resilience;
using Polly;
// Program.cs — Add resilient HttpClient with Polly
builder.Services.AddHttpClient("ProductApi", client =>
{
client.BaseAddress = new Uri("https://product-service:5001");
})
.AddResilienceHandler("standard", builder =>
{
// Retry — try 3 times with exponential backoff
b
...Pro Tip
Start with a modular monolith — separate bounded contexts in one deployable unit. Extract to microservices only when you have a clear scaling or team ownership need. Premature microservices add complexity without benefit.
Common Mistakes
- • Synchronous calls between every service — creates tight coupling and cascading failures
- • No circuit breaker — one failing service takes down the entire system
- • Shared databases between services — defeats the purpose of independence
Lesson Complete!
You've built microservices with gRPC, messaging, and resilience. Next, learn to schedule background jobs reliably.
Sign up for free to track which lessons you've completed and get learning reminders.