Introduction to API Gateway in Microservices Architecture

Some architects, cloud engineers, and DevOps folks often say, “Microservices are small monoliths.” This stems from the complexity of dealing with a number of services, especially managing and configuring their network rules and security aspects.

When clients make requests to microservices spread over multiple clusters and clouds in a distributed system, tracking each request to ensure security and proper routing rules becomes tedious. Ideally, backend services should not be doing that, as they should be left alone with delivering the business logic. This is where an API gateway, a single entry point for all requests, comes in. Let us see what an API gateway is and what features and benefits it offers.

What Is an API Gateway?

An API gateway is a server (or L7 proxy) between a client and microservices that acts as a centralized entry point for all clients into the system. It is a reverse proxy that accepts client API calls and forwards them to the appropriate microservice (refer to Fig. A below).

By providing an API for each client, API gateways encapsulate the complexity of the underlying system and let clients talk to it instead of calling specific services. They also perform security checks (authentication and authorization) before the traffic reaches the service, thus leaving services to focus on their core functionalities.

API gateway implementation conceptual diagram Fig. A – API gateway implementation conceptual diagram for microservices architecture
Fig. A – API gateway implementation conceptual diagram for microservices architecture

The Need for API Gateways for Microservices

The challenges posed by the direct client-to-microservices pattern of communication led to the popularity of API gateways. Let us go through some of them.

The Problem of Service Discovery and Traffic Routing

For direct client-to-microservice connection, the clients have to know the specific endpoints of service instances. But keeping track of endpoints adds complexity for clients because of the dynamic (de)scaling of services. Also, if clients are coupled to the services, scaling becomes an issue as it will require configuration changes on the client side. Besides, routing traffic based on certain attributes, such as geography (geo-routing), is hard to configure when clients invoke services directly.

Security Concerns

Publicly exposing service endpoints for direct client-to-service communication causes security concerns. It increases the attack surface for intruders and leaves backend services prone to threats, such as packet sniffing, man-in-the-middle attack, etc. Besides, direct client-to-microservices put the burden of authenticating and authorizing API calls on services instead of letting them focus on delivering the business logic.

Diverse Protocols Affecting Interoperability

The flexibility provided by microservices architecture lets developers build services using the language (Python, Java, Go) they choose. Similarly, they can implement those services in different API types, such as REST, gRPC, and others. In a direct client-to-microservice communication pattern, it then becomes a requirement for the client to understand and communicate using different protocols. This adds additional complexity as the client application will require more code and logic.

Latency Caused by Round Trips

Consider a product page from the Amazon website. Some attributes like product pricing, quantity, and reviews will be deployed as different services in the backend. If the client is invoking services directly, it will have to make separate requests for each service (product price, reviews, quantity, etc.) to retrieve the required information since there is no mechanism to cache a response from the upstream services. These calls add to the overhead of establishing multiple connections, and the round trips caused by these network requests add to latency and suboptimal user experience.

API gateways’ architecture is in a way that it mitigates the challenges caused by direct client-to-microservice connections and provides a variety of features.

API Gateway Traffic Flow

An API gateway is an L7 proxy that abstracts the traffic management out of front-end microservices, which are usually requested by clients. API gateways can read and understand the HTTP messages (refer to the picture below), so they can apply filters or take actions on the traffic.

HTTP message structure
HTTP message structure


A request flows through multiple steps at the API gateway. The below image (Fig. B) represents an API gateway sitting at the edge of a Kubernetes cluster and the stages through which a request flows.

Fig. B – Traffic flow of an incoming gRPC request through the API gateway
Fig. B – Traffic flow of an incoming gRPC request through the API gateway

Once the API gateway completes the above steps, it will return the response from the service back to the client. However, note that the outlined steps may vary depending on the way the gateway is configured and the implementation of additional features.

API Gateway Features

API gateways provide many features apart from the critical ones mentioned above.

All these features provided by API gateways deliver enormous benefits to managing a distributed system of services.

Benefits of API Gateway

API gateway implementation helps organizations reap the following benefits, among others.

Improved Application Security

As a centralized point for API management, gateways hide the services and the underlying infrastructure from being exposed publicly. This makes it difficult for attackers trying to bring down the application, particularly by overwhelming the services with requests (DoS attack). Since gateways process every request before reaching the backend, they can apply rate limiting against such an attack. Other security features, such as request validation, authN/Z, circuit breaking, and policy enforcement, coupled with logging and monitoring, makes API gateway contribute to the overall security of the applications.

Enhanced Flexibility in Handling and Scaling Microservices

API gateways decouple external clients from internal microservices. This gives high flexibility for DevOps and Infrastructure Engineers in making changes to the backend services without requiring updates to configurations in the client applications. Clients can still make requests through the gateway and get responses without knowing about the changes the backend has undergone. Many important functionalities, such as authN/Z and load balancing, will be taken care of by the gateway. Offloading these responsibilities to the gateway helps developers write less amount of code for applications, which fosters innovation and enables rapid releases.

Better Monetization for API Providers

API monetization is all about productizing the API for third-party consumers. API gateway provides a better way for companies to monetize their APIs to generate revenue or cover the operational cost of maintaining the APIs. The gateway connects the client requests to a billing system, thus providing a centralized billing and metering mechanism for API providers. This helps companies track API usage and collect payments for the service by implementing different pricing models, such as pay-as-you-go, tiered, and unit-based, for API consumers.

Improved User Experience (UX)

API gateway eases clients from making too many requests by requesting the underlying services and aggregating them. That is, a single request to the gateway will be sufficient for client applications, significantly reducing latency. And in the case of frequently repeated requests, the gateway can promptly serve cached responses without forwarding the request to the backend. Also, with monitoring and logging features, API gateways make tracking and troubleshooting any performance issues easier, which helps minimize application downtime. All of these help in improving the application’s performance, reliability, and user experience.

Top 3 Open-Source API Gateway Tools

While evaluating an API gateway tool, organizations can look for open-source tools, cloud service providers, or enterprise editions. If open-source is your priority, we have outlined the top three open-source API gateway tools based on the factors such as ease of use, flexibility, and scalability.

1. Tyk API Gateway

Tyk provides a fully open-source gateway that supports multiple protocols like REST, GraphQL, and gRPC. It has no third-party dependency apart from Redis and is one of the fastest gateways available today.

Below are some features of the Tyk API gateway:

Tyk API Gateway


2. Kong API Gateway

Kong API Gateway is a cloud-native gateway suited for both multi-cloud and hybrid cloud deployments. The gateway is also Kubernetes-native with the help of its own Kubernetes ingress controller. Kong is known for its flexibility and extensibility through modules and plugins.

Some open-source features of Kong API Gateway include the following:

Kong Gateway


3. KrakenD API Gateway

A high-performing API gateway, KrakenD is built with a serverless architecture that provides true linear scalability. It helps in scaling out without a single point of failure. KrakenD runs on on-prem, hybrid, or cloud and is extensible with plugins and embedded scripts.

The open-source version of KrakenD offers the following features:

KrakenD


Is API Gateway a Silver Bullet?

No, it is not. Like any other tool, API gateways come with a set of challenges. Here are a few of them:

Explore these challenges of API gateways in detail and understand why considering a service mesh platform would be ideal.

Also, there are different scenarios to implement Istio with your existing API gateway infrastructure.

 

 

 

 

Top