Implementing an API Gateway in ASP.NET Core with Ocelot
Tags: api, dotnet, security
Published: 2021-10-13 11:19:22

#### Learn what an API Gateway is and how to build yours in ASP.NET Core by using Ocelot.

### <br>

The urge towards adopting microservices architecture has been a welcome trend in the software development industry.  Microservices architecture has been one of the most talked-about technologies in recent times – it has been embraced by many leading organizations worldwide. Originally developed to solve the limitations of monolithic systems, microservices architecture has seen a significant increase in popularity over the last several years, mainly due to increased scalability, flexibility, and performance.

Since microservices-based applications comprise several different services, you often need a common interface or gateway to call these services so that you define and manage all concerns in one place rather than replicate them across all downstream services. This is precisely where an API Gateway comes in. This article briefly discusses the concepts around microservices architecture and how you can work with API Gateways to have a consistent way to connect to the microservices. Let's get started!

## What Is Microservices Architecture?

Microservices architecture is a service-oriented architecture (SOA) variant in which an application comprises a collection of lightweight, loosely coupled, modular services. These services can be built to execute on various platforms and independently developed, tested, deployed, and managed.

Microservices architecture can replace long-running, complicated monolithic systems requiring considerable resource and management overhead. Microservice is a word that refers to a service having a limited set of functionalities rather than referring to the length of the code used to create it.

## What Is an API Gateway?

When building microservices-based applications, an API Gateway is needed to have a central place where authentication, throttling, orchestration, etc., is implemented. Without an API Gateway in place, you might typically implement each of these in each service, and hence maintaining them for each service would be a daunting task at hand. An API Gateway decouples the service producer from its consumer, providing a security layer since you need not expose your microservices directly.

As soon as it receives a request, it breaks it into multiple requests (if needed) and then routes them to the appropriate downstream microservice. You can take advantage of an API Gateway to centralize, manage, and monitor the non-functional requirements of an application, orchestrate the cross-functional microservices, and reduce roundtrips. By managing requests consistently, an API Gateway can help reduce the latency and improve security.

The figure below illustrates an API Gateway used to connect to two downstream microservices named Customer and Product.

![API Gateway architecture diagram](https://images.ctfassets.net/23aumh6u8s0i/3GS9a9hK4MG45UQuG2DVC8/bf86b3d095b072199d88dce416907472/api-gateway-architecture.png)

Usually, the service consumers or clients of a microservice don't communicate with it directly. Instead, an API Gateway provides a single entry point for directing traffic to various microservices, as shown in the figure above. Hence the clients don't have any direct access to the services and cannot exploit the services. If your API Gateway is behind the firewall, you can add an extra layer of protection around the attack surface.

An API Gateway pattern corresponds to two famous [Gang of Four design patterns](https://en.wikipedia.org/wiki/Design_Patterns): Facade and Adapter. Like the Facade design pattern, an API Gateway provides an API to the consumers while encapsulating the internal architecture. An API Gateway enables communication and collaboration like in the Adapter design pattern, even if the interfaces are incompatible.

[Read more...](https://auth0.com/blog/implementing-api-gateway-in-aspnet-core-with-ocelot/?utm_source=content_synd&utm_medium=sc&utm_campaign=asp)