What are Microservices?
Microservices architecture is a design approach where an application is built as a collection of small, independent services that communicate over well-defined APIs. Each service is:- Independently deployable
- Loosely coupled
- Organized around business capabilities
- Owned by a small team
- Implemented with different technologies if needed
Microservices in AspNetRun
The AspNetRun project implements four core microservices, each representing a distinct business capability:Service Characteristics
1. Catalog Service
Product Catalog Management
Responsibility: Manage product catalog, categories, and product informationTechnology: PostgreSQL with Marten (Document DB)Pattern: Vertical Slice ArchitectureAPI Endpoints:
GET /products- List all products with paginationGET /products/{id}- Get product by IDGET /products/category/{category}- Get products by categoryPOST /products- Create new productPUT /products- Update productDELETE /products/{id}- Delete product
2. Basket Service
Shopping Cart Management
Responsibility: Manage user shopping carts, apply discountsTechnology: Redis for distributed cachingPattern: Simple CQRSAPI Endpoints:
GET /basket/{userName}- Get user’s basketPOST /basket- Store/update basketDELETE /basket/{userName}- Delete basketPOST /basket/checkout- Checkout basket
- High-performance with Redis caching
- gRPC integration with Discount service
- Publishes integration events on checkout
3. Ordering Service
Order Processing
Responsibility: Process orders, maintain order history, manage order lifecycleTechnology: SQL Server with Entity Framework CorePattern: Clean Architecture + DDDAPI Endpoints:
GET /orders- List all orders with paginationGET /orders/{id}- Get order by IDGET /orders/customer/{customerId}- Get orders by customerPOST /orders- Create new orderPUT /orders- Update orderDELETE /orders/{id}- Delete order
- Rich domain model with DDD patterns
- Domain events for side effects
- Subscribes to basket checkout events
- Complex business logic and validation
4. Discount Service
Discount Management
Responsibility: Manage product discounts and promotional codesTechnology: SQLite with Entity Framework CoreProtocol: gRPC for high-performancegRPC Services:
GetDiscount- Get discount for productCreateDiscount- Create new discountUpdateDiscount- Update discountDeleteDiscount- Delete discount
Database per Service Pattern
Each microservice has its own database, ensuring:Data Isolation
Data Isolation
Services cannot directly access each other’s databases, enforcing loose coupling.
Technology Independence
Technology Independence
Each service can choose the most appropriate database technology:
- Catalog: PostgreSQL with Marten for flexible document storage
- Basket: Redis for high-performance caching
- Ordering: SQL Server for ACID transactions
- Discount: SQLite for lightweight storage
Independent Scaling
Independent Scaling
Each database can be scaled independently based on service needs.
Fault Isolation
Fault Isolation
Database issues in one service don’t affect others.
API Gateway Pattern
The project uses YARP (Yet Another Reverse Proxy) as the API Gateway: Location:src/ApiGateways/YarpApiGateway/appsettings.json
Benefits of API Gateway
Single Entry Point
Clients connect to one endpoint instead of multiple services
Request Routing
Routes requests to appropriate microservices
Load Balancing
Distributes traffic across service instances
Cross-Cutting Concerns
Handles authentication, CORS, rate limiting centrally
YARP Configuration Example
Service Communication
Synchronous Communication
HTTP/REST via API Gateway Most client-to-service communication uses REST APIs: gRPC for Service-to-Service Basket service calls Discount service via gRPC for performance:src/Services/Basket/Basket.API/Basket/StoreBasket/StoreBasketHandler.cs:15-20
Asynchronous Communication
Message Broker with RabbitMQ and MassTransit For eventual consistency and loose coupling, services communicate via events: Integration Event Example:src/BuildingBlocks/BuildingBlocks.Messaging/Events/BasketCheckoutEvent.cs
src/Services/Ordering/Ordering.Application/Orders/EventHandlers/Integration/BasketCheckoutEventHandler.cs
Microservices Benefits
Independent Deployment
Each service can be deployed independently without affecting others
Technology Freedom
Services can use different technologies, frameworks, and databases
Scalability
Scale individual services based on demand
Fault Isolation
Failures in one service don’t bring down the entire system
Team Autonomy
Small teams can own and maintain services independently
Faster Development
Parallel development across teams
Microservices Challenges
Distributed Data Management
Distributed Data Management
Data consistency across services requires patterns like:
- Eventual consistency
- Saga pattern for distributed transactions
- Event sourcing
Service Discovery
Service Discovery
Services need to discover and communicate with each other. Solutions:
- DNS-based discovery
- Service mesh (Consul, Istio)
- API Gateway
Monitoring and Debugging
Monitoring and Debugging
Distributed tracing and centralized logging are essential:
- OpenTelemetry
- ELK Stack (Elasticsearch, Logstash, Kibana)
- Application Insights
Network Latency
Network Latency
Inter-service communication introduces latency:
- Use async communication where possible
- Implement caching strategies
- Consider service collocation for chatty services
Testing Complexity
Testing Complexity
Testing distributed systems is harder:
- Integration tests
- Contract testing
- Chaos engineering
Best Practices Implemented
1. Service Independence
Each service has its own:- Codebase
- Database
- Deployment pipeline
- Docker container
2. API Versioning
3. Health Checks
Each service exposes health check endpoints:src/Services/Catalog/Catalog.API/Program.cs:28-29
src/Services/Catalog/Catalog.API/Program.cs:38-42
4. Containerization
All services are containerized with Docker:5. Configuration Management
Services use:appsettings.jsonfor default configuration- Environment variables for environment-specific settings
- Docker Compose for local development
Related Topics
Clean Architecture
Learn how Ordering service implements Clean Architecture
Vertical Slice
See how Catalog service uses Vertical Slice pattern
CQRS Pattern
Understand CQRS implementation across services
DDD Principles
Deep dive into Domain-Driven Design patterns
