APIs, or Application Programming Interfaces, are a set of rules and protocols that allows two software applications or services to communicate with each other.
As applications grow in size, the number of APIs increases too. Without the right tools and infrastructure, managing these APIs can quickly become a challenge.
This is where API Gateway comes into play.
An API Gateway acts as a central server that sits between clients (e.g., browsers, mobile apps) and backend services.
Instead of clients interacting with multiple microservices directly, they send their requests to the API Gateway. The gateway processes these requests, enforces security, and forwards them to the appropriate microservices.
In this chapter, we will explore why do we need an API gateway, the key features it provides and how it works step by step.
Modern applications, especially those built using microservices architecture, have multiple backend services managing different functionalities.
For example, in an e-commerce service:
API Gateway secures the backend systems by ensuring only authorized users and clients can access backend services.
It handles tasks like:
By centralizing these tasks, the API gateway eliminates the need for individual services to handle authentication, reducing redundancy and ensuring consistent access control across the system.
To prevent abuse and ensure fair usage of resources, most API Gateways implement rate limiting.
This feature:
For example, a public API might allow a maximum of 100 requests per minute per user. If a client exceeds this limit, the API Gateway will block additional requests until the rate resets.
High-traffic applications rely on load balancing to distribute incoming requests evenly across multiple instances of a service.
The API Gateway can:
To improve response times and reduce the strain on backend services, most API Gateways provide caching.
They temporarily store frequently requested data, such as:
Caching helps in reducing latency and enhancing user experience while lowering the operational cost of backend services.
In systems with diverse clients and backend services, request transformation is essential for compatibility.
An API Gateway can:
For instance, it might convert XML responses from a legacy service into JSON for modern frontend applications.
Modern systems often involve microservices that scale dynamically.
The service discovery feature of an API Gateway dynamically identifies the appropriate backend service instance to handle each request.
This ensures seamless request routing even in environments where services frequently scale up or down.
Circuit breaking is a mechanism that temporarily stops sending requests to a backend service when it detects persistent failures, such as:
The API Gateway continuously monitors the health and performance of backend services and uses circuit breaking to block requests to a failing service.
API Gateways provide robust monitoring and logging capabilities to track and analyze system behavior.
These capabilities include:
This data helps system administrators detect anomalies, troubleshoot issues, and optimize the system’s performance. Many API Gateways also integrate with monitoring tools like Prometheus, Grafana, or AWS CloudWatch.
Imagine you're using a food delivery app to order dinner. When you tap "Place Order" your phone makes an API request. But instead of talking directly to various backend services, it communicates with an API Gateway first.
When you tap "Place Order," the app sends a request to the API Gateway, asking it to process your order.
This request includes things like:
The API Gateway receives the request as the single entry point to the backend system.
Before forwarding the request, the API Gateway validates it to ensure:
If any information is missing or incorrect, the gateway immediately rejects the request and notifies the app with an appropriate error message.
The gateway now verifies your identity and permissions to ensures only legitimate users can place orders:
If authentication or authorization fails, the API Gateway sends a 401 Unauthorized or 403 Forbidden error back to the app.
To prevent abuse, the API Gateway checks how many requests you’ve made recently. For example:
429 Too Many Requests response.This ensures the system remains stable and fair for all users specially during traffic spikes or malicious attacks, such as distributed denial-of-service (DDoS) attempts.
If any of these backend services require specific data formats or additional details, the API Gateway transforms the request.
For example:
The API Gateway now needs to coordinate several backend services to process your order.
Using service discovery, it identifies:
The gateway dynamically routes the request to these services using a load balancing algorithm, ensuring it connects to available and healthy service instances.
Once the API Gateway receives the response(s) from the backend service(s), it performs the following tasks:
Finally, the API Gateway sends the processed response back to the client in a format they can easily understand.
Throughout this process, the gateway records important metrics to track each request: