Common Amazon API Gateway Patterns for microservice architecture:
API Gateway:
API Gateway is a fully managed service that allows to create RESTful and WebSocket APIs at any scale. API Gateway handles all the tasks involved in accepting and processing up to hundreds of thousands of concurrent API calls, including traffic management, CORS, authorization, throttling, monitoring, and version management.
It provides end users with the lowest possible latency for API requests and responses by taking advantage of AWS global network of edge locations using CloudFront.
API Gateway Pattern:
The API gateway pattern is recommended if you want to design and build a microservices-based applications with multiple client applications. API gateway provides a single endpoint or URL for the client applications, and it internally maps the requests to internal microservices.
This can be achieved in multiple ways:
One API gateway for all microservices
Main API gateway, which routes traffic to specific API gateways of each service
One main Load Balancer in front of multiple API gateways
Use Case:
A customer makes a payment in a system that consist of 4 microservices deployed as Lambda functions ("Customer","Communication","Payment" and "Sales").
Customer service updates the customer database with the payment details.
Sales microservice updates the sales database.
Communication service sends a communication email to the customer after payment is successfully processed.
Payment microservice is the overall system that the customer uses to make payment.
Unified API gateway:
In the following illustration, each microservice is deployed as a Lambda function but all microservices are connected by the same API gateway.
Main API Gateway with gateway per service:
In the following illustration, each microservice has its own API gateway. The "Payments" microservice calls out individual systems and implements the API gateway pattern.
Load Balancer in front of multiple API gateways:
This solution allows you also to have other targets along with API gateway, like EC2, Load Balancers, etc.
Challenges:
Synchronous calls are made to downstream systems, therefore latency caused by these subsystems affects the overall response time.
Running costs are higher because the main system is waiting for responses from the other microservices before responding to the calling system. The total running time is higher compared with an asynchronous system.
Summary:
This article explored three approaches for using an API Gateway in a microservices architecture:
Deploying a single, unified API Gateway for all services.
Using a central API Gateway that routes to individual gateways for each service.
Implementing a Load Balancer to manage multiple API Gateways.
Each method has its advantages and drawbacks. The unified API Gateway is the most cost-effective, while the Load Balancer setup offers the highest level of robustness.