Monday, 22 September 2025

Microservices

1️⃣ What Are Microservices?

  • Microservices is a way to build a big application as many small, independent services.

  • Each service does one specific job (e.g., Orders, Payments, Inventory).

  • All services talk to each other using APIs or messages.

Think of it like a restaurant:

  • The kitchen, billing counter, and delivery are all separate teams, but together they run the restaurant.


2️⃣ Key Features

  • Independent Deployment: Update or restart one service without stopping others.

  • Own Database: Each service can have its own database.

  • Technology Freedom: One service can use SQL Server, another can use MongoDB if needed.

  • Scalable: You can scale only the busy service (e.g., Orders) instead of the whole app.


3️⃣ Why Use .NET for Microservices?

  • .NET 8 (or .NET 6/7) is cross-platform (Windows, Linux, containers).

  • ASP.NET Core is fast for building REST APIs.

  • Great support for Docker and Kubernetes.

  • Many built-in tools for security, logging, and dependency injection.


4️⃣ Typical .NET Microservice Architecture

Client (Web / Mobile) | API Gateway ← handles routing, security | ┌─────────┬───────────┬───────────┐ | Order | Payment | Inventory | └─────────┴───────────┴───────────┘ | | | Own DB Own DB Own DB
  • API Gateway: Single entry point (e.g., Ocelot or YARP).

  • Services: Each built with ASP.NET Core Web API.


5️⃣ Communication Between Services

  • HTTP/REST: Simple web APIs.

  • gRPC: Faster binary communication.

  • Messaging/Event Bus: RabbitMQ, Azure Service Bus, Kafka for async events.

Example:

  • When an order is placed, the Order service sends an “OrderPlaced” event.

  • Payment and Inventory services listen and act.


6️⃣ Steps to Build Microservices in .NET

  1. Plan Services

    • Identify business areas: e.g., Customer, Product, Order.

  2. Create Each Service

    • Use dotnet new webapi to create a separate ASP.NET Core project.

  3. Database Per Service

    • Add EF Core or Dapper for data access.

  4. API Gateway

    • Use Ocelot to route external requests.

  5. Messaging (optional but recommended)

    • Add RabbitMQ/Azure Service Bus for events.

  6. Containerize

    • Create Dockerfiles, run docker-compose.

  7. Deploy & Scale

    • Use Kubernetes (AKS), Azure Container Apps, or AWS EKS.


7️⃣ Example Tech Stack

  • Framework: ASP.NET Core

  • Database: SQL Server / PostgreSQL / MongoDB

  • Message Broker: RabbitMQ or Azure Service Bus

  • Gateway: Ocelot

  • Container: Docker + Kubernetes

  • Monitoring: Prometheus + Grafana or Azure Monitor


8️⃣ Advantages

  • ✅ Easy to scale specific services.

  • ✅ Faster deployments.

  • ✅ Fault isolation—one service crash doesn’t stop others.

  • ✅ Freedom to choose technology per service.


9️⃣ Challenges

  • More complex deployment.

  • Need strong DevOps (CI/CD, Kubernetes).

  • Service-to-service security and communication.

No comments:

Post a Comment