DEV Community

Kelwin Dsouza
Kelwin Dsouza

Posted on

API Rate Limiting with Kong: How to Secure & Optimize High-Traffic APIs

πŸ”Ή Introduction

APIs power modern applications, but without proper control, they can be exploited by malicious actors or overwhelmed by high traffic. Kong API Gateway provides powerful rate-limiting capabilities to:

βœ… Prevent API abuse & DDoS attacks
βœ… Ensure fair API usage among consumers
βœ… Reduce cloud costs by controlling excessive API requests
βœ… Optimize API performance & protect backend resources

In this post, we'll explore how to implement API Rate Limiting using Kong API Gateway with both Kong Gateway (Open-Source) and Kong Enterprise.

1️⃣ Why is API Rate Limiting Important?

APIs must handle varied traffic loads, but without restrictions, they can become unreliable. Here’s why Rate Limiting is crucial:

πŸ“Œ 1. Protect APIs from DDoS Attacks & Abuse
πŸ”Ή Hackers can send millions of API requests per second, causing server overload and downtime.
πŸ”Ή Kong’s Rate Limiting Plugin helps block excessive traffic & prevent API abuse.

πŸ“Œ 2. Ensure Fair Usage
πŸ”Ή Multiple API consumers compete for resources.
πŸ”Ή Rate Limiting ensures one user doesn’t monopolize API capacity.

πŸ“Œ 3. Optimize API Performance
πŸ”Ή Traffic spikes slow down API responses.
πŸ”Ή Rate Limiting ensures consistent API availability.

πŸ“Œ 4. Reduce Cloud & Infrastructure Costs
πŸ”Ή APIs running on AWS, GCP, or Azure cost money per request.
πŸ”Ή Controlling excessive API calls saves costs & prevents overuse.

2️⃣ Understanding Kong API Rate Limiting Strategies

πŸ“Œ 1. Rate Limiting by IP Address
πŸ”Ή Controls traffic based on the client's IP.
πŸ”Ή Best for public APIs & protecting from bot attacks.

πŸ“Œ 2. Rate Limiting by API Key / Consumer
πŸ”Ή Limits API requests based on unique API keys or authentication tokens.
πŸ”Ή Ideal for subscription-based APIs.

πŸ“Œ 3. Rate Limiting by Service or Route
πŸ”Ή Restricts traffic to specific API endpoints.
πŸ”Ή Useful when some API routes are more resource-intensive.

πŸ“Œ 4. Rate Limiting by Request Headers
πŸ”Ή Limits API calls based on custom headers (e.g., User-Agent, Geo-Location).
πŸ”Ή Best for dynamic Rate Limiting based on client type.

3️⃣ Setting Up Rate Limiting in Kong API Gateway

πŸ“Œ Step 1: Install Kong API Gateway (If Not Already Installed)
If you haven’t set up Kong yet, you can install it using Docker:

docker pull kong
docker network create kong-net
docker run -d --name kong-database --network=kong-net \
   -p 5432:5432 \
   -e POSTGRES_USER=kong \
   -e POSTGRES_DB=kong \
   -e POSTGRES_PASSWORD=kong \
   postgres:13
docker run -d --name kong \
   --network=kong-net \
   -e KONG_DATABASE=postgres \
   -e KONG_PG_HOST=kong-database \
   -e KONG_PROXY_ACCESS_LOG=/dev/stdout \
   -e KONG_ADMIN_ACCESS_LOG=/dev/stdout \
   -e KONG_PROXY_ERROR_LOG=/dev/stderr \
   -e KONG_ADMIN_ERROR_LOG=/dev/stderr \
   -e KONG_ADMIN_LISTEN=0.0.0.0:8001 \
   kong
Enter fullscreen mode Exit fullscreen mode

πŸ“Œ Step 2: Enable the Rate Limiting Plugin
Once Kong is running, enable the Rate Limiting Plugin using the Kong Admin API.

curl -X POST http://localhost:8001/services/{service-name}/plugins \
  --data "name=rate-limiting" \
  --data "config.minute=100" \
  --data "config.policy=local"
Enter fullscreen mode Exit fullscreen mode

πŸ”Ή Breakdown:

config.minute=100 β†’ Allows 100 requests per minute.
config.policy=local β†’ Rate limit is enforced per Kong instance.

πŸ“Œ Step 3: Apply Rate Limiting by Consumer (API Key-based Limiting)
If you want to apply rate limiting per API consumer, use:

curl -X POST http://localhost:8001/consumers/{consumer-id}/plugins \
  --data "name=rate-limiting" \
  --data "config.minute=50" \
  --data "config.policy=redis" \
  --data "config.redis_host=redis"
Enter fullscreen mode Exit fullscreen mode

πŸ”Ή Breakdown:

config.minute=50 β†’ Allows 50 requests per minute per API Key.
config.policy=redis β†’ Uses Redis for distributed rate limiting.

πŸ“Œ Step 4: Apply Rate Limiting by Route (Endpoint-Based Limiting)
If you want to limit traffic to a specific API route, use:

curl -X POST http://localhost:8001/routes/{route-id}/plugins \
  --data "name=rate-limiting" \
  --data "config.hour=500" \
  --data "config.policy=cluster"
Enter fullscreen mode Exit fullscreen mode

πŸ”Ή Breakdown:

config.hour=500 β†’ Allows 500 requests per hour per route.
config.policy=cluster β†’ Rate limit is enforced across Kong nodes.

4️⃣ Monitoring Rate Limits in Kong

To check active Rate Limits, use:

curl -X GET http://localhost:8001/plugins?name=rate-limiting
Enter fullscreen mode Exit fullscreen mode

To view logs & analytics, integrate Prometheus & Grafana:

docker run -d --name kong-grafana --network=kong-net -p 3000:3000 grafana/grafana
Enter fullscreen mode Exit fullscreen mode

5️⃣ Handling Rate Limit Exceeded Responses

When an API user exceeds their rate limit, Kong returns:

{
  "message": "API rate limit exceeded. Try again later."
}
Enter fullscreen mode Exit fullscreen mode

To customize this response, modify Nginx templates in Kong.

6️⃣ Key Takeaways

βœ” Kong API Gateway provides robust Rate Limiting capabilities.
βœ” Different Rate Limiting strategies (IP, API Key, Route-based) can be applied.
βœ” Using Redis or Cluster mode enables distributed Rate Limiting.
βœ” Monitoring via Kong Admin API & Grafana helps track API usage.

Top comments (0)