Hybrid Federated Ocelot Gateway


People who have experience with .net core distributed microservices architecture will be very familiar with an ocelot. It's an API gateway to forward the request based on the routes. It supports the HTTP for rest services and WebSocket protocol for SignalR services. It provides a lot of features like authentication, authorization, adding custom headers, and a lot more. All are configurable. We can easily orchestrate multiple services using ocelot. It's a single point of contact to the outside world. It acts as a reverse proxy.

As part of microservices architecture, each service will have a single responsibility. Sometimes service needs some data from other services or needs to complete some action from other services as part of business workflow.

As part of distributed microservices architecture, there are many ways to enable communication between the services. 

  1. Service to Service communication via HTTP or Grpc.
  2. Event-Driven via pub/sub model

You can do a service to service communication by API call or publish the message, but it will increase the microservices complexity because microservices are loosely coupled systems. If we make an API call or publish the message to other services, then-current service will depend on the other services. Apart from this, we can enable service-to-service communication in different ways also.

Wrapper Service

We can create some wrapper service and move the business logic there. So wrappers will behave like logical apps. it will have series of actions. Each step will call the corresponding services. Each service will behave like a function app. Adding a wrapper again will increase application complexity.

Gateway orchestration

Move the logic to the gateway especially for reading operations. Gateway will fetch information from multiple services, aggregate it and return the response. It's very simple and straightforward. We can get all the information we require from multiple services. Consider a complex approach, like if we need to configure some additional properties like pagination. I need some paginated data from two services. In this case, I need to add some custom logic in the gateway because the paginated input can be passed via headers or queries, or body. Gateway needs to parse the input and forward it to the corresponding service. So gateway complexity is getting increased. the gateway should be lightweight it just needs to be a reverse proxy to forward the request and aggregate the results.

We can't add business logic in the gateway. We can't add additional wrapper service either. So we need a solution to communicate to multiple services with a single request with customized inputs. A Federated gateway serves this purpose.

Federated Gateway

Apollo provided this feature as part of graphql development. Each microservice will have its corresponding entity. If the service needs data from other services it will extend the corresponding entities. All the microservices schema (Api specs) will be merged and create a single schema and get registered in the gateway.

Federated gateway creates the graph based on the schema. It will maintain the relationship based on the graph. So it will parse the request body and forward the request into the corresponding services and aggregate the results back to the client. We can pass custom input to multiple services via a gateway. Federated gateway parses the request and forwards it to corresponding services.

Federated Gateway in .Net Core

We can implement the federated gateway concepts in .Net core using hot chocolate. Its NuGet package recently developed by ChilliCream. The NuGet package is an initial release. Using this we can achieve federated graphql development using .net core. But It needs to evolve a lot, to support the enterprise level to support distributed architecture. Ocelot + Federated

Ocelot provides very good routing based on the request URL. Federated provides routes based on the body. So we can add federated implementation in the ocelot gateway. The gateway will support both rest and graphql based routing. It's yet another hybrid gateway. Using this approach we can maintain all the rest and graphql services under a single umbrella. Currently, it's working in the POC with some customization. But it will require a lot of improvements to support enterprise-level applications.

If you have any ideas and suggestions please let me know your thoughts about this approach.