Microservices Mastery

API Gateways: Implementing Ocelot for single-entry access

1 Views Updated 5/4/2026

The API Gateway Pattern

You don't want your mobile app to have 50 different URLs for 50 different microservices. An API Gateway provides a single entry point (e.g., api.toolliyo.com) that routes traffic to the correct internal service. We use Ocelot in .NET to build powerful, lightweight gateways.

1. Routing and Aggregation

The gateway can "Aggregate" multiple calls. If a mobile app needs "User Info" and "Last 5 Orders," it makes ONE call to the gateway. The gateway then calls Service A and Service B in parallel, merges the JSON, and returns it to the phone.

2. Cross-Cutting Concerns

Instead of implementing **Authentication**, **Rate Limiting**, and **Logging** in every single microservice, you do it ONCE in the gateway. The internal services can then trust that any request reaching them has already been validated.

// ocelot.json configuration
{
  "Routes": [
    {
      "DownstreamPathTemplate": "/api/users/{everything}",
      "UpstreamPathTemplate": "/users/{everything}",
      "DownstreamHostAndPorts": [ { "Host": "identity-service", "Port": 80 } ]
    }
  ]
}

4. Interview Mastery

Q: "What is a 'Leaky Gateway' and how do we avoid it?"

Architect Answer: "A Leaky Gateway happens when you start putting too much **Business Logic** into the Gateway. The Gateway should only care about **Routing** (Networking). If you start calculating discounts or creating database records inside the gateway, you've created a new Monolith. Keep your gateway 'Thin' and move business logic back into the specialized microservices where it belongs."

Microservices Mastery
1. Distributed Systems Fundamentals
Monolith vs Microservices: When to migrate? The 12-Factor App Methodology for Cloud-Native Apps Database Per Service: Handling distributed data consistency
2. Containerization & Orchestration
Docker Essentials: Building efficient .NET images Docker Compose: Orchestrating a multi-service environment Kubernetes Architecture: Pods, Services, and Deployments K8s ConfigMaps & Secrets: Managing environment variables Helm Charts: Packaging your microservices for K8s
3. Service Communication
Synchronous vs Asynchronous Communication: Pros and Cons REST APIs in a Microservices World: Best Practices Mastering gRPC: High-performance binary communication API Gateways: Implementing Ocelot for single-entry access BFF Pattern: Backend-for-Frontend (Mobile vs Web)
4. Event-Driven Architecture
Message Brokers: Introduction to RabbitMQ & Azure Service Bus Pub/Sub Pattern: Implementing MassTransit for .NET The Outbox Pattern: Ensuring 100% data consistency Dead Letter Queues: Handling message failure gracefully Distributed Transactions: The Saga Pattern (State Machines)
5. Resilience & Scalability
Distributed Caching with Redis: Optimizing global state Service Discovery: IdentityServer4 & Consul Load Balancing: Nginx vs Ingress Controllers The Sidecar Pattern: Offloading cross-cutting concerns
6. Observability & Security
Distributed Logging with Serilog & SEQ Distributed Tracing: OpenTelemetry & Jaeger Health Checks: Monitoring system vitals in real-time OAuth2 & OpenID Connect: Centralized Identity (AuthN/AuthZ) Rate Limiting & Throttling: Protecting your services
7. Advanced Cloud Topics
Infrastructure as Code (IaC): Introduction to Terraform CI/CD Pipelines for Microservices (GitHub Actions/Azure DevOps) C# Architect Interview: Microservices & System Design Focus