API Gateway Tasarımı: Rate Limiting, Caching ve Güvenlik

z

zafer ak

Yazar

02 November 2025 11 dakika okuma 440 görüntülenme
API Gateway Tasarımı: Rate Limiting, Caching ve Güvenlik
Mikroservis mimarisinde API Gateway rolü. Kong, AWS API Gateway ve custom çözümler.

API Gateway Nedir?

API Gateway, tüm API isteklerinin geçtiği tek giriş noktasıdır. Routing, authentication, rate limiting ve monitoring gibi cross-cutting concerns'leri merkezi olarak yönetir.

API Gateway Görevleri

  • Request Routing: İstekleri doğru servise yönlendirme
  • Authentication: JWT, API Key doğrulama
  • Rate Limiting: İstek sınırlama
  • Caching: Yanıt önbellekleme
  • Load Balancing: Yük dengeleme
  • Request/Response Transform: Veri dönüşümü

Rate Limiting Stratejileri

// Laravel Rate Limiting
// routes/api.php
Route::middleware([
    "throttle:api"
])->group(function () {
    Route::get("/users", [UserController::class, "index"]);
});

// Custom Rate Limiter
// app/Providers/RouteServiceProvider.php
RateLimiter::for("api", function (Request $request) {
    return Limit::perMinute(60)->by(
        $request->user()?->id ?: $request->ip()
    );
});

// Tier-based Limiting
RateLimiter::for("premium", function (Request $request) {
    $user = $request->user();

    return match($user?->plan) {
        "enterprise" => Limit::none(),
        "premium" => Limit::perMinute(1000),
        "basic" => Limit::perMinute(100),
        default => Limit::perMinute(10),
    };
});

Response Caching

// Cache Middleware
class CacheResponse
{
    public function handle($request, Closure $next, $ttl = 60)
    {
        $key = "response:" . md5($request->fullUrl());

        if (Cache::has($key)) {
            return response()->json(
                Cache::get($key),
                200,
                ["X-Cache" => "HIT"]
            );
        }

        $response = $next($request);

        if ($response->isSuccessful()) {
            Cache::put($key, $response->getData(), $ttl);
        }

        return $response->header("X-Cache", "MISS");
    }
}

Kong Gateway Örneği

# docker-compose.yml
services:
  kong:
    image: kong:latest
    environment:
      KONG_DATABASE: "off"
      KONG_DECLARATIVE_CONFIG: /kong/declarative/kong.yml
      KONG_PROXY_ACCESS_LOG: /dev/stdout
      KONG_ADMIN_ACCESS_LOG: /dev/stdout
    ports:
      - "8000:8000"
      - "8001:8001"

# kong.yml
_format_version: "3.0"
services:
  - name: user-service
    url: http://user-api:3000
    routes:
      - name: user-route
        paths:
          - /api/users
    plugins:
      - name: rate-limiting
        config:
          minute: 100
          policy: local
      - name: jwt

AWS API Gateway

# serverless.yml
service: my-api

provider:
  name: aws
  runtime: nodejs18.x

functions:
  hello:
    handler: handler.hello
    events:
      - http:
          path: hello
          method: get
          cors: true
          throttling:
            maxRequestsPerSecond: 100
            maxConcurrentRequests: 50

Güvenlik Best Practices

  • HTTPS zorunlu kılın
  • API Key rotation uygulayın
  • IP whitelist/blacklist kullanın
  • Request validation yapın
  • WAF entegrasyonu ekleyin

Sonuç

API Gateway, mikroservis mimarisinin kritik bileşenidir. Doğru yapılandırma ile güvenlik, performans ve ölçeklenebilirlik sağlar.

İlgili Yazılar