What Is an API Gateway?

5 min. read

An API gateway is a server that acts as an intermediary between clients and microservices in a distributed software architecture, often within a cloud environment. Serving as a reverse proxy, it handles client requests, routes them to the appropriate microservices and aggregates responses from multiple microservices into one response for the client. The API gateway also provides additional functionality, such as authentication, authorization, rate limiting, caching, and monitoring. By centralizing these functions, the API gateway simplifies the development, management, and scalability of microservices-based applications.

API Gateway Explained

API gateways are traffic conductors. They facilitate interactions between client applications and backend services. Operating as an intermediary between application programming interfaces (APIs) and their clients, the API gateway serves as the enforcement point for policies at runtime. This control includes how the gateway accepts API calls, routes requests to the appropriate services, aggregates results for responses, and translates protocols.

Gateways also handle tasks like API authentication, rate limiting, and logging. By governing end-to-end access, transforming data and facilitating analytics, the API gateway earns a place in an organization’s API management strategy.

API Gateway for microservices
Figure 1: Using an API gateway for client-to-microservice communication

 

The API gateway offers a unified point of access to the distributed architecture of cloud-native microservices. By orchestrating interactions, steering traffic, and providing a layer of abstraction, the gateway simplifies the client's view of the backend. Its primary role lies in encapsulating the internal microservices and system architecture and exposing a set of tailored APIs to the client.

How the API Gateway Works

From the API call and response to performance and API protection, fulfilling its multifaceted role involves many steps.

Request Handling and Routing

When a client sends a request, the API gateway receives it and inspects the HTTP method, headers and URI to determine the target microservice. The gateway uses preconfigured routing rules to forward the request to the appropriate microservice instance. This process often involves load-balancing algorithms, such as Round Robin or Least Connections, to distribute the requests among the available instances.

Protocol Translation

API gateways can perform protocol translation between clients and microservices, allowing them to communicate using different protocols. A client might send a request using HTTP/REST, for example, which the gateway translates into a gRPC or message queue-based request before forwarding it to the target microservice.

Authentication and Authorization

API security is a crucial aspect of API management. The API gateway can act as a centralized point for implementing authentication and authorization mechanisms. It can integrate with identity providers (e.g., OAuth, OpenID Connect, SAML, or JWT) to validate incoming requests and ensure that clients have permissions to access the requested resources.

Rate Limiting and Quotas

To protect microservices from excessive load or abuse, the API gateway can enforce rate limits and quotas on incoming requests. Developers can establish this based on the client Internet Protocol, API key, user account, or other factors.

Response Transformation and Aggregation

The API gateway can modify and aggregate responses from multiple microservices before sending them back to the client. This includes transformations like adding, removing, or renaming fields in the response payload, converting data formats (e.g., XML to JSON), and aggregating data from multiple microservice responses into a single response object.

Caching

The API gateway can cache responses from microservices to reduce latency and improve performance. By storing frequently accessed data in-memory, the gateway can quickly respond to client requests without needing to repeatedly query the same data from the microservices.

Monitoring, Logging and Analytics

The API gateway can log incoming requests, responses, and errors, which provide valuable analytics data. This information can help system administrators and developers identify performance bottlenecks, detect security incidents and ensure the overall health of the system.

Resilience and Fault Tolerance

API gateways can implement resiliency patterns such as circuit breakers, retries, and timeouts to handle failures in the underlying microservices. In case a microservice instance becomes unresponsive or fails, the gateway can automatically redirect requests to other available instances, ensuring high availability and fault tolerance.

API Gateways for Microservices Architectures

Without an API gateway, developers would need to expose microservices to incoming requests and let the microservices determine how to respond. Imagine the resources needed for each microservice to decide whether to respond — on a scale of multiple microservices per application and countless requests.

The API gateway is a building block in microservices architectures, facilitating their decoupling nature and enhancing independent scalability and resilience.

Acting as a façade, the gateway offers one entry point and a standardized interface that shields clients from the complexities of the underlying microservices. The gateway can tailor protocols to each microservice while presenting a consistent API to clients. It can also simplify client-side logic by translating between API styles — HTTP REST, gRPC, GraphQL — and aggregate responses from multiple services into a single response.

API Transformation
Figure 2: API transformation function in an API gateway

What Is the API Gateway Pattern?

The API gateway pattern defines an approach where a server acts as a single-entry point for different types of clients — web, mobile, desktop — accessing a system comprising numerous microservices. The pattern aims to encapsulate the system's architecture by simplifying the client's interaction and reducing the number of requests and amount of data that needs to pass between client and server.

Reverse Proxy or Gateway Routing

The gateway routes client requests to the appropriate services within the system. By doing so, it hides the complexity of the system's internal structure from the client and reduces the client's need to manage multiple service endpoints.

Requests Aggregation

The API gateway pattern can aggregate responses from multiple services into one response to minimize the number of round-trip requests between the client and the server. This helps to improve efficiency and reduce latency.

Cross-Cutting Concerns or Gateway Offloading

The gateway handles cross-cutting concerns applicable across various services. Concerns can include logging, metrics, rate limiting and security. By managing them at the gateway level, it offloads these tasks from individual services, allowing them to focus on their core business logic.

API Gateway Pattern
Figure 3: The pattern is part of a distributed system reverse proxy or gateway routing.

Challenges of API Gateways Potential Response Time Increases

While API gateways generally increase efficiency, they can introduce latency. The added processing steps for tasks like request routing, composition, and protocol translation can increase response time. Optimizing the performance of the API gateway will prevent it from forming a bottleneck.

Dependencies

If not designed for scalability, API gateways can create dependencies, hindering the independent deployment and scalability of microservices. When all traffic has to pass through the gateway, it may limit the velocity and autonomy that a microservices architecture usually enables.

Complexity

Implementing an API gateway adds another layer of complexity to the system. Configuration, maintenance, and the ongoing development of the gateway require effort and expertise, which might increase overhead on the team, especially for smaller projects.

Security Compromise

While an API gateway can enhance security, it can also pose risks if compromised. Because it's a single point of entry to the system, any security breach could expose the underlying services. Careful design and implementation of security controls are imperative to mitigate risks.

Single Gateway Reliability

The architecture of the API gateway poses another challenge — availability. Given its role in handling all incoming API calls, any downtime can impact the system. Ensuring high availability, redundancy, and fault tolerance of the API gateway gains importance in a production environment.

Use Cases and API Gateway Benefits

API gateways serve diverse use cases across different architectural environments, playing a notable role in shaping the behavior of applications, particularly in distributed architectures.

Microservices Security

As these architectures proliferate numerous discrete services, coordinating interactions and routing requests becomes convoluted. An API gateway can streamline the process, handling request routing, composition, and protocol translation. It stands at the frontline, directing incoming API calls to backend services.

And as mentioned, by serving as a single entry point into the system, the API gateway can offer a layer of defense to fortify the security of microservices. It can enforce policies like SSL termination, access control and IP whitelisting, eliminating the need for each microservice to implement these measures. This arrangement simplifies the security landscape and reduces the potential for errors and vulnerabilities.

API Authentication

An API gateway can authenticate user or system credentials, ensuring that only authorized entities can access the services. It supports different authentication mechanisms, such as OAuth or JWT, which offloads this responsibility from individual microservices and promotes better security practices.

Input Validation

API gateways can perform request validation to ensure only correctly formatted data enters the system. By rejecting invalid requests at the gateway, it lightens the load on individual microservices, allowing them to focus on their primary functions.

Serverless Support

In the context of serverless architectures, API gateways take on additional significance. With backend services running on demand, the API gateway needs to manage invocation requests for these functions. It also provides a buffer against the cold-start latency inherent in serverless environments.

API gateways also play a role in managing the lifecycle of function-based services, including their invocation, scaling, and performance monitoring. They also handle version control, deployment, and staging, helping to streamline the function-as-a-service (FaaS) model that underpins serverless computing.

Billing for Microservices

In commercial settings, API gateways can manage billing and metering for API usage. They can track the number of requests each client makes and can enforce usage quotas and limits, which allows for a granular understanding of API usage and supports a usage-based billing model.

Microservices Caching

API gateways can take advantage of caching. By storing the response of a service request, the gateway can serve repeated requests for the same data, improving response times and reducing the load on backend services. It can also implement response compression to reduce bandwidth consumption and improve network efficiency.

Extending Legacy Apps

Use cases extend to the modernization of legacy systems. An API gateway allows organizations to wrap legacy applications in modern, RESTful APIs. This technique enables these legacy systems to interact with modern applications and services without extensive refactoring.

Supports for DevOps and Serverless Environments

In DevOps practices, an API gateway forms a crucial bridge between development and operations teams by establishing a unified front for all the microservices. It promotes continuous integration and delivery by ensuring that API changes and deployments do not negatively affect the system's overall operation.

Service Mesh Vs. API Gateway

While both a service mesh and an API gateway manage interservice communication, their focus areas differ. A comprehensive microservices strategy often includes both an API gateway and a service mesh.

Acting as a reverse proxy, the API gateway deals with client-to-services communication, or north-south traffic. The service mesh provides service discovery, load balancing, failure recovery, metrics and monitoring through service-to-service communication, or east-west traffic.

API Gateway Vs. Load Balancer

A load balancer and an API gateway have overlapping functions, but their primary objectives diverge.

A load balancer distributes network traffic across multiple servers to enhance responsiveness and availability. Operating at the transport layer, it distributes load based on policies and the health of backend servers.

An API gateway operates at a higher level, focusing on application logic. It handles routing, aggregation, protocol translation and security. While it also can distribute requests among services, its features surpass those of a typical load balancer, encompassing application-level concerns and policies.

 

Video 1: Overview of API security and tips for enhancing API security

API Gateways Security and WAFs

API gateways and web application firewalls (WAFs) play distinct yet interconnected roles in the security and efficiency of web applications.

The API gateway focuses on ensuring the correct use of APIs by enforcing valid request structures and authenticating and authorizing clients. It can block requests that don't meet criteria, but it usually can’t inspect the content of the requests for patterns matching known attack vectors.

The WAF operates as a protective barrier for web applications, intercepting and inspecting HTTP traffic to identify and block threats. By using predefined security rules and anomaly detection, the WAF can prevent SQL injection, cross-site scripting (XSS), distributed denial-of-service (DDoS), and other types of attack. Its rules can also be updated to guard against newly discovered vulnerabilities and attack methods, which makes the WAF a vital line of defense against web application attacks.

In practice, using an API gateway with a WAF provides a layered security approach. The API gateway manages interactions with the API, while the WAF provides an additional security layer against malicious activity. Together, they offer a powerful combination of application management and API security.

API Gateway Solutions

Organizations have several deployment options for API gateway, depending on the requirements and infrastructure of their applications.

  • On-premises: Organizations can deploy an API gateway on-premises within a data center, which provides control over the infrastructure and security.
  • Cloud-based: API gateway can be deployed in a public or private cloud environment, offering scalability, and flexibility.
  • Serverless: Using serverless computing platforms such as AWS Lambda or Azure Functions eliminates the need for organizations to manage underlying infrastructure.
  • Hybrid: With a combination of on-premises, cloud or serverless environments, organizations can meet unique application requirements.

When exploring your API gateway options, look for a solution that meets your application's requirements. Consider factors like scalability, extensibility, and integration with existing infrastructure and services. Popular solutions include:

Amazon API Gateway

A fully managed service from Amazon Web Services (AWS), Amazon API Gateway handles all aspects of API management and traffic management. It supports WebSocket and REST APIs, has built-in DDoS protection, integrates with AWS services like Lambda and DynamoDB, and can scale to any volume of traffic.

Azure API Management

This Microsoft offering includes an API gateway, developer portal, and a variety of tools for defining and managing APIs. It offers features like throttling, routing, caching, identity provider integration and analytics.

Kong

An open-source API gateway and platform built on top of NGINX, Kong offers extensibility through a rich plugin ecosystem that includes integration with various databases and services. Kong also provides load balancing, authentication, rate limiting, and health checks.

Apigee

Apigee by Google Cloud provides a full-featured API platform that includes an API gateway, developer portal and tools for API design, monetization, security, analytics, and operations.

MuleSoft Anypoint Platform

An integration platform that includes API gateway functionality, MuleSoft Anypoint Platform offers features like security, caching and load balancing, along with integration and orchestration capabilities.

Industry Practices and Standards

Follow leading practices for optimal gateway performance.

Implement security best practices: Ensure that your API gateway is configured to enforce authentication, authorization, and encryption to protect your microservices from unauthorized access and potential security threats.

Monitor and log API traffic: Collect logs and metrics from your API gateway to gain insights into the performance, availability, and security of your microservices. Use this data to identify issues, optimize performance, and detect potential security threats.

Optimize performance: Configure caching, rate limiting, and load balancing in your API gateway to improve the performance and reliability of your microservices.

Plan for scalability: Design your API gateway to accommodate growth in your application's user base and the number of microservices it supports. Consider using cloud-based or serverless deployment options for better scalability.

Maintain API documentation: Keep your API documentation up to date and easily accessible for developers and clients, ensuring they have the information needed to interact with your microservices effectively.

Implement versioning: Allow versioning in your API gateway to manage changes and updates to your microservices without breaking existing clients.

Use health checks and monitoring: Regularly monitor the health and performance of your API gateway and microservices, setting up alerts and notifications to quickly detect and address issues.

Adopt a DevOps/DevSecOps culture: Encourage collaboration between development, security and operations teams, ensuring that security and performance standards are integrated throughout the application lifecycle.

API Gateway FAQs

APIs are sets of protocols and tools for building software applications. They define the methods and data formats that an application should use to communicate with other software components, operating systems or microservices.
An API call is the process that takes place when you send a request after setting up your API with the correct endpoints. Your information is transferred, processed, and feedback is returned back. An API call is a command from a user or system to the API of another system.
An API request is a communication from a client to an API that tells it to perform a specific action. It involves making a call to an API endpoint and typically includes:
  • Base URL of the API
  • Path specifying a resource
  • HTTP method indicating the desired action
  • Headers containing metadata
  • Possibly a body with data
API authentication is a process that validates the identities of parties involved in API interactions. It uses methods like API keys, OAuth, or JWT to ensure that only authorized parties can access the API.
An API key is a unique identifier used to authenticate a client, developer, or calling program when making requests to an API. Typically generated as a long string of random characters by the API provider, API keys are issued to users who include the key in their requests. API gateways can be configured to require API keys for access. While they don't directly provide security, they help track and control how the API is used.
An API ID is a unique identifier for an API. It is used for tracking, management, and controlling access to an API. In cloud services like AWS, each instance of an API gateway deployment is given an API ID, which is used in the invocation URL for the API. Similarly, in Google Cloud, each API is assigned a project ID and an API ID that are used to manage and call the API.
API caching stores the responses from microservices in a cache, allowing API gateway to serve cached responses to clients instead of forwarding requests to the microservices. Caching can improve application performance by reducing the load on the microservices and minimizing response times for clients.
Fault tolerance is the ability of a system to continue operating correctly even in the presence of failures, such as hardware failures, software errors, or network issues. API gateway can be designed to provide fault tolerance by implementing features like load balancing, circuit breakers, and retries, ensuring that client requests are still processed even if some microservices are unavailable.
API throttling is the process of limiting the number of API requests a client can make in a specific period. Throttling is used to prevent excessive load on the backend servers, ensuring fair usage of resources and protecting the API from potential denial-of-service attacks. API gateway can be configured to enforce rate limiting based on client IP addresses, API keys, or other identifiers.
Serverless computing is a cloud computing execution model that allows developers to build and run applications without managing the underlying infrastructure. In serverless computing, cloud providers automatically allocate resources and handle scaling, patching, and capacity planning. Developers only pay for the compute resources and execution time their applications consume.
A reverse proxy is a server that sits between clients and one or more backend servers, forwarding client requests to the appropriate backend server and returning the server's response to the client. Reverse proxies can be used for load balancing, caching, and security purposes.
A service mesh is a configurable infrastructure layer for a microservices application. It facilitates service-to-service communications, resilience, and observable behavior, while offloading these capabilities from the individual services.

API transformation refers to the process of modifying or converting the structure, format, or content of data exchanged between an API and its consumers. Transformation allows for communication and interoperability between different systems or applications that may have varying data requirements.

In the context of APIs, transformation typically involves modifying the payload or data representation format, such as converting XML to JSON or vice versa, altering the data structure or performing data validation and enrichment. It can also include tasks like data mapping, protocol translation, or encryption/decryption.

API transformation plays a crucial role in ensuring compatibility and efficient data exchange between the API provider and consumers, especially when dealing with heterogeneous systems or when adapting to different standards or protocols.

When you implement versioning in your API gateway, you allow multiple versions of your API to coexist and be accessible to clients. Versioning maintains backward compatibility for older clients when you introduce new features, make changes to existing ones, or fix bugs in your microservices. API versioning ensures that your existing clients don't break when updates are made to the API, and it provides a smooth transition for clients to adopt the new API changes.
A REST API (Representational State Transfer API) is a type of API that adheres to the principles of REST architectural style. It uses standard HTTP methods and is stateless, meaning each request from a client contains all the information needed to process the request. REST APIs interact with resources, where each resource is identified by a specific URL. The API uses different HTTP methods to manipulate these resources.
A RESTful API is essentially the same as a REST API. The term emphasizes that the API adheres strictly to the principles of REST. This means the API has a clear separation of concerns (client-server architecture), it is stateless; it can cache data to improve performance; it has a uniform interface; and it is layered, allowing for encapsulation of legacy services and components.
A WebSocket API is a type of API that enables live, bidirectional communication between client and server over a single, long-lived connection. Unlike REST APIs, which are stateless, a WebSocket API maintains a persistent connection, allowing for instantaneous data push and synchronization.
Envoy Proxy is a high-performance, open-source edge and service proxy designed for cloud-native applications. It can handle outbound and inbound traffic for all services in an architecture and provides dynamic service discovery, load balancing, TLS termination, HTTP/2 and gRPC proxies, circuit breakers, health checks, and more. Envoy is often used as a component in service mesh deployments.
An API endpoint is a specific route or URL where an API can be accessed by a client. Each endpoint is associated with a specific function or resource, and it often includes specific paths to perform various operations on that resource. For example, in a RESTful API, an endpoint for a user resource might be '/users', with '/users/{id}' allowing access to a specific user, and HTTP methods like GET, POST, PUT, DELETE used to perform operations on the resource.
An HTTP endpoint is a URL where a client application can access your API. This endpoint is exposed over HTTP(S) and defined by a URL pattern, an HTTP method (GET, POST, PUT, DELETE, etc.), and the types of data it can accept and return. An API is typically made up of multiple HTTP endpoints, each corresponding to a specific service or functionality.
An HTTP API is an API that is accessed via HTTP (Hypertext Transfer Protocol) methods, such as GET, POST, PUT, and DELETE. It allows communication between software systems, often serving as a bridge to connect the client and server, so they can send and receive data. HTTP APIs can be built in multiple architectural types, including REST, GraphQL, or gRPC. Similarly, they can return data in various formats, such as JSON or XML.
A backend service refers to any service that handles requests from a client application or frontend service but is not directly exposed to the user. Backend services can include databases, microservices, business logic services, and other server-side components. They are responsible for processing requests, performing operations, and returning the results back to the client application.
A backend API is an API that serves as the interface for backend services. It encapsulates the business logic, data access operations and other server-side processing needed by client applications or frontend services. Client apps interact with backend APIs to retrieve data, execute business processes, and interact with underlying systems, databases, or other services. A backend API may be implemented in any number of architectural styles, such as REST, GraphQL, or gRPC.
Open source refers to a type of software whose source code is available to the public and can be modified or enhanced by anyone. This promotes collaboration and transparency, as users are free to improve the software, fix bugs, or adapt it for their needs. Notable examples of open-source software include the Linux operating system, the Apache HTTP Server, and the MySQL database management system.
OAuth (Open Authorization) is an open standard for access delegation, allowing users to grant third-party applications limited access to their resources without sharing their credentials. OAuth provides a secure and standardized way for applications to request and manage access tokens, which can be used to access protected resources on behalf of a user.
OpenID Connect (OIDC) is an identity layer built on top of the OAuth 2.0 protocol, used for user authentication. It allows clients to verify the identity of the user and obtain basic profile information in an interoperable manner. OIDC issues an ID token to the client after successful authentication. This can then be validated to ensure the integrity and authenticity of the user's data.
JSON Web Token (JWT) is an open standard for securely transmitting information between parties as a JSON object. JWTs are often used for authentication and authorization purposes, as they can be digitally signed or encrypted to ensure the integrity and confidentiality of the transmitted data. In API gateway, JWTs can be used to authenticate clients and authorize access to microservices.
A cloud shell is a browser-based command-line interface for interacting with cloud resources. It provides a preconfigured, on-demand environment from which users can manage and administer their cloud services. Cloud shells often come with a set of tools pre-installed, allowing developers to run scripts, build applications and carry out administrative tasks directly within the cloud environment.
Proxy integration is a type of API integration in AWS API gateway, where the API acts as a pass-through for requests and responses. This means that incoming requests pass directly to the backend service without needing to be mapped. Responses from the backend service likewise pass directly back to the client. It simplifies the process of integrating APIs with backend services.
A client app is a software application that runs on a user's device, such as a smartphone or computer, and communicates with remote servers to request services. Examples include web browsers, email clients and messaging apps. They interact with backend servers via APIs to retrieve and manipulate data.
A private API is an API that’s not publicly available or accessible. Its access is restricted to a specific audience, typically within an organization or among specific partners. Private APIs serve internal business needs, improving productivity and efficiency. They are often used to expose data and services for use by other parts of the organization or by specific external partners.