Volume 4 · Chapter 4 — .NET Distributed Systems and Microservices Architecture

API Gateway Architecture

Goutham Mayaluri 7 min read .NETC#MicroservicesAPI Gateway

4.1 Introduction

In a microservices system, clients would otherwise need to know and call many services directly. An API Gateway provides a single entry point that centralizes cross-cutting concerns - routing, authentication, rate limiting, aggregation, monitoring - and simplifies the client’s view of the system. The defining rule is that a gateway handles infrastructure concerns, not business logic, which belongs in the domain services.

4.2 The Gateway as a Reverse Proxy

An API Gateway acts as a reverse proxy: it receives external client requests and forwards them to the appropriate internal service based on routing rules (URL patterns, headers). Clients talk only to the gateway, which hides the internal topology - so services can be added, moved, or split without changing clients.

flowchart TB
  Clients["Clients (web/mobile/partner)"] --> GW["API Gateway (reverse proxy)\nauth, routing, rate limit, aggregation"]
  GW --> Cust["Customer Service"]
  GW --> Pay["Payment Service"]
  GW --> Acc["Account Service"]

4.3 Gateway Responsibilities

The gateway centralizes: request routing, authentication and authorization (often validating tokens once at the edge), rate limiting, request/response transformation (headers, versioning, protocol conversion), response aggregation (combining several services’ responses for a client), and logging/monitoring. In .NET, YARP is the common building block, with Ocelot as an alternative.


// YARP reverse proxy: routing configured in appsettings, auth/rate-limit as middleware.
builder.Services.AddReverseProxy()
    .LoadFromConfig(builder.Configuration.GetSection("ReverseProxy"));

var app = builder.Build();
app.UseAuthentication();      // validate token once at the edge
app.UseRateLimiter();
app.MapReverseProxy();        // forward to backend services per routes
app.Run();

4.4 Backend for Frontend

Different clients need different API shapes, so the Backend-for-Frontend pattern provides a dedicated gateway per client type (a Mobile BFF, a Web BFF), each tailored to that consumer’s needs - avoiding a single chatty, general-purpose API and letting each frontend evolve independently.

4.5 Gateway vs Service Responsibilities

The gateway does traffic management, authentication, routing, and aggregation; services do business rules, domain validation, and data processing. Putting business logic in the gateway is a common mistake that turns it into a coupled bottleneck and a place where domain logic hides outside its owning service.

Senior Interview Question

Should business logic live in an API Gateway? What belongs there and what doesn't?

No - business logic should not live in the API Gateway; it belongs in the domain services that own the relevant capability. The gateway’s job is cross-cutting infrastructure concerns that apply across services: routing requests to the right service, authenticating and (coarsely) authorizing at the edge, rate limiting and throttling, TLS termination, request/response transformation (headers, versioning, protocol conversion), response aggregation, and logging/monitoring/tracing. These are horizontal concerns that make sense to centralize so every request is handled consistently and clients get a simplified, single entry point. What does NOT belong there is anything domain-specific: business rules, domain validation, workflows, calculations, and data processing - because putting those in the gateway causes real problems. It couples the gateway to the services’ internals and domains, so a business change forces gateway changes; it scatters business logic outside the service that owns it, undermining the service’s ownership and cohesion and making the domain harder to reason about and test; and it turns the gateway into a fat, stateful, business-aware component that becomes a bottleneck and a single point of complex failure. The gateway should stay thin and generic - a smart proxy for traffic and security concerns - while each service encapsulates its own business logic behind its API. The one nuance is aggregation: a gateway (or better, a Backend-for-Frontend) can compose responses from multiple services for a client, but that composition should be orchestration/shaping of data, not domain decision-making; if the aggregation starts embedding business rules, that logic belongs in a service. So the clean separation is: gateway handles routing, security, rate limiting, transformation, aggregation, and observability (infrastructure and cross-cutting concerns); services handle all business rules and domain logic. Keeping business logic out of the gateway is what preserves service ownership, keeps the gateway scalable and simple, and prevents it from becoming a coupled bottleneck.

Principal Engineer Question

How do you prevent an API Gateway from becoming a bottleneck or single point of failure?

The gateway sits in the path of every request, so if it’s slow, fat, or fails, it can throttle or take down the whole system - I prevent that through a combination of keeping it thin, scaling it out, protecting it, and making it resilient. First, keep its responsibilities lightweight and generic: routing, auth, rate limiting, transformation, aggregation - and explicitly no business logic, no heavy processing, and no blocking calls - so each request spends minimal time in the gateway; a thin, stateless gateway is fast and cheap to run. Second, scale it horizontally: run multiple gateway instances behind a load balancer so throughput scales with instances and there’s no single instance that everything depends on - because it’s stateless (state externalized to caches/token validation), instances are interchangeable and can autoscale. Third, protect the backends and the gateway itself with rate limiting and resilience so it doesn’t amplify problems - and importantly, wrap its calls to backend services with timeouts, retries (bounded, with backoff), circuit breakers, and bulkheads, so a slow or failing backend can’t hang gateway threads or cascade into a gateway-wide failure; the gateway should fail fast and degrade gracefully rather than blocking. Fourth, cache where appropriate - responses or tokens/metadata - to reduce load on backends and speed up the gateway, being careful about invalidation. Fifth, ensure high availability at the infrastructure level: deploy across availability zones, health-check instances so unhealthy ones are removed, and avoid making the gateway a stateful single point (externalize any state to a distributed store). Sixth, monitor it as critical infrastructure - latency, error rate, throughput, saturation - with alerting, so problems are caught early, and load-test it at target scale to know its limits. For very high scale, consider splitting concerns (e.g. per-client BFFs, or offloading TLS/edge to a CDN/edge layer) so no single gateway carries everything. So the answer is: keep the gateway thin and stateless (no business logic, no blocking), scale it horizontally behind a load balancer with multi-zone HA and health checks, protect it and its backend calls with rate limiting, timeouts, circuit breakers, and bulkheads so failures don’t cascade, cache to reduce load, and monitor and load-test it - which keeps a component that’s inherently on the critical path from becoming the system’s bottleneck or single point of failure.

4.6 Banking Enterprise Scenario

A banking mobile app needs customer details, account balances, and transaction history from several services. A Mobile BFF gateway authenticates the user once at the edge, routes requests, applies rate limiting, and aggregates the three services’ responses into one mobile-optimized payload - hiding the internal topology. The gateway stays thin (no business logic), runs multiple instances behind a load balancer across zones, and wraps backend calls in timeouts and circuit breakers so a slow service can’t take it down.

Chapter Revision Notes

  • The API Gateway is a single entry point and reverse proxy centralizing cross-cutting concerns.
  • It handles routing, auth, rate limiting, transformation, aggregation - not business logic.
  • BFF provides a dedicated, tailored gateway per client type.
  • YARP (or Ocelot) implements gateways in .NET.
  • Keep it thin, scale it out, and protect backend calls so it isn’t a bottleneck or SPOF.