
My developer friends, how are we all doing? 👋 I am certain that just like all of us, the management of microservices was a headache for you. After all, it is an unparalleled endeavor. Congratulations, you’ve accomplished the arduous task of tearing your monolith into separate services. That’s awesome! Unfortunately, we have a web of disparate API calls, authentication problems, and traffic storms. Do you relate? In these situations, API gateways are useful. You can think of them as fences around your microservices world. They manage the chaos, letting your services concentrate on their assigned jobs.
In this post, I will cover the most common patterns, tools, and challenge areas, so you don’t have to spend time tackling them yourself. Let’s get right to it!
Imagine this for a second: You have 20 microservices to deal with, each having their very own API, authentication logic and rate limits. Now, coordinating all of this with web, mobile and third-party clients? That's downright nightmare inducing, huh?
An API gateway functions as the main entry point through which all requests are supposed to go. It manages:
But here’s what is important, not all gateways are the same. Let’s analyze the patterns that will not only preserve your wellbeing, but also the ones that will drain your time and energy.
What it solves: How many times did you have to change the API because the mobile team wanted one form while the web team wanted another one? The BFF pattern builds specialized gateways for every sort of client.
How It Works:
For example, the mobile version of your e-commerce app requires productId, name and price. The web version needs reviews, stock status, and related items. Instead of making a single API to cater all, create two BFFs.
# Mobile BFF route
GET /mobile/products/{id} → Product Service (basic fields)
# Web BFF route
GET /web/products/{id} → Product Service + Reviews Service + Inventory Service
BFF Shared Library - some gotchas
What it Solves: Prevents the over abuse of your services, like someone using your ‘end-point’ for ‘/checkout’ 1000 times/minute, as well as protecting your bank account from the surprise of the cloud bill.
Implementation:
Tools in Action:
Pro Tip: Fixed intervals are less preferable than sliding windows. If, for example, your limit is 4 requests to make within 60 seconds, let people make short bursts of up to 120 requests within 30 seconds, with the average during the 60 seconds being below 100.
Example Configuration (Kong):
# Limit to 10 requests/minute per user
curl -X POST http://localhost:8001/plugins
--data "name=rate-limiting"
--data "config.minute=10"
--data "config.policy=local"
Be Mindful Of:
How It’s Done: What is the strategy for ensuring only authorized people and automated accounts invoke your endpoints?
Best Practices:
Example Flow:
Client → API Gateway → [Auth Service] → "Is this token valid?"
↓
If yes: Route request to Service A with `X-User-ID=123`
If no: Return 401 Unauthorized
Tools:
Istio: Apply policies at the service mesh level using AuthorizationPolicy.
AWS Cognito + API Gateway: Serverless scalable auth.
Be Mindful Of:
Token Hijacking: Implement short token lifetimes and enforce HTTPS.
Performance: Tokens should be validated in RAM (e.g. validating in Redis’ token cache).
Challenge Yourself: “Do I have to hit the auth service for every request, or is there a way to cache the auth results?"
Kong. The Swiss Army Knife. Best for: Customizability. You need a Kafka pluging? Get it here. Example: A startup uses Kong to implement OAuth2, rate limiting, and logging features without any coding. Watch out: What about scaling and security if you have to maintain your own Kong nodes?
Istio: The Kubernetes Maestro Best for: Employees of already existing Kubernetes ecosystems get the most out of this service mesh + API Gateway. This is literally the best. A chef's kisss. Example: Splitting incoming traffic between v1 and v2 of a service is a great way to speed up A/B testing. Watch out: This is out of the question if you not entirely into Kubernetes.
NGINX: The Reliable Workhorse Best for: Easiest to use for people who want great results without too much hassle. Example: Iterating on an existing chai tea microservices application where the NGINX does reverse proxying. Watch out: Better get an Ansible or Terraform because those configuration files will rot away without clean up automation.
AWS API Gateway: The Serverless Sidekick Best for: Kong helps Serverless stacks with example application implementations. (Lambda + API Gateway = ❤️ ). Example: Serverless backend for an ecommerce project that uses auto-scaling. Watch out: You really need to carefully monitor usage or else, boom, cost explosion.
Over–Engineering The Sin: Implementing a service mesh for a 3-service app is wasted.
The Solution: Begin with the basics. Use NGINX or a managed gateway and increase the complexity based on your requirements.
Not Accounting Observability The Sin: Absence of measurements on lag, faults, and rate limiting.
The Fix: Implemented Grafana + Prometheus for monitoring: Rates of 4xx/5xx responses Average response time Rate limit requests
Vendor Lock In The Sin: Over Arching everything around the AWS API Gateway, then needing to change it becomes extremely expensive.
The Fix: Use a Serverless Framework or Terraform to abstract the gateway logic.
Q: When should I implement a Backends For Frontends (BFF) compared to one single Gateway?
A: Implement BFFs if your clients web and mobile, or any external client require exceptionally different information otherwise it is best kept simple.
Q: Can I deploy several API gateways?
A: Absolutely! Internal services can utilize Kong while external services can use AWS API Gateway. Just be cautious of complexity.
Q: What if the gateway fails?
A: Gateways should be deployed in a cluster with load balancing; have circuit breakers in place, such as Hystrix, to fail softly.
Q: Can I use an API gateway in place of a Service Mesh?
A: Certainly not! A gateway manages traffic from north to south (from the client to the service) while a mesh handles east-west traffic (from one service to another).
API gateways, are not some magic tool, they are something you create and control. Just like any other tool they need to be utilized to their full potential in order to see desired outcomes.
Prior to Beginning Construction:
Identify Your Requirements: Evaluate whether you require authentication, rate limiting, or protocol translation assistance.
Get Familiar With The Tools: Launch Kong and AWS API Gateway in a sandbox. Which of the two is more suitable?
Plan for Future Growth: Will your gateway be capable of accommodating 10 times the traffic next year?
It is critical to note that the goal is not to construct the ultimate gateway; instead, it is to address present issues while avoiding hindering future solutions. Now, go untangle that microservices spaghetti. 🍝