Ana Sayfa / Blog / API gateway pattern: Kong, Traefik, custom yazmak

API gateway pattern: Kong, Traefik, custom yazmak

Microservice sisteminde API gateway kritik. Kong, Traefik gibi hazır tool'lar vs custom gateway. Karar kriterleri.

Birden fazla backend servisi var, client’lar tek endpoint’ten erişecek. Klasik microservice mimarisi sorunu. API gateway bu iletişim katmanını abstract ediyor: authentication, rate limiting, routing, logging.

3 popüler yaklaşım var: Kong (open source + enterprise), Traefik (modern reverse proxy), custom gateway (kendi yazıyorsun). Bu yazıda her birinin ne zaman optimal olduğunu anlatacağım.

API gateway ne yapar?

API gateway request’i internal servis’e iletmeden önce:

  1. Authentication: JWT validation, API key check
  2. Authorization: Bu endpoint’e bu user’ın erişimi var mı?
  3. Rate limiting: Kullanıcının limit’i aşılmadı mı?
  4. Routing: Hangi internal servis’e git?
  5. Transformation: Request/response format değişikliği
  6. Logging: Access log, audit trail
  7. Metrics: Latency, error rate, throughput
  8. Circuit breaking: Downstream servis fail ise upstream’i koru

Bu 8 işi her internal servisin kendisi yapmak yerine gateway’de merkezileştiriyorsun.

Kong

Mature (10+ yıl), Nginx üstüne kurulu, Lua plugin ecosystem.

Avantajlar:

  • Rich plugin ecosystem (100+ plugin). Auth, rate limit, transform, log hepsi hazır.
  • Battle-tested. Many large companies using.
  • Kong OSS ücretsiz, Kong Enterprise (paid) dashboard/GUI/support.
  • Multiple auth mechanism support (JWT, OAuth, API key, LDAP).
  • Declarative config (YAML).
  • Admin API + Konga GUI.

Dezavantajlar:

  • Setup complex. Production cluster (Kong + Cassandra/PostgreSQL) ops heavy.
  • Resource hungry. Nginx + Lua VM each request.
  • Enterprise features paywalled.
  • Learning curve Lua plugin writing için steep.

Ne zaman Kong: Enterprise environment, mature microservice ecosystem, plugin-rich setup needs.

Traefik

Go ile yazılmış modern reverse proxy + API gateway. Cloud-native.

Avantajlar:

  • Auto-discovery (Docker, Kubernetes, Consul). Service register olduğunda gateway auto-configure.
  • Let’s Encrypt integration built-in. SSL cert management automated.
  • Config can be dynamic (K8s CRDs, Consul KV, Docker labels).
  • Lighter resource footprint than Kong.
  • Excellent K8s integration (Traefik is default ingress controller in many K8s distributions).

Dezavantajlar:

  • Plugin ecosystem smaller than Kong. Custom middleware limited.
  • Auth mechanisms less diverse (mostly JWT, basic auth; need plugins for OAuth).
  • Traefik Enterprise (v3+) some features paywalled.

Ne zaman Traefik: Kubernetes-based infrastructure, modern cloud-native setup, simpler routing needs.

Custom gateway (kendi yazıyorsun)

Node.js, Go, Python’da kendi gateway logic’ini yazıyorsun.

Avantajlar:

  • Full control. Kendi business logic.
  • No operational dependency on third-party gateway.
  • Specific optimizations (custom caching, transformation, auth flows).
  • No licensing costs.
  • Integration with existing internal tooling kolay.

Dezavantajlar:

  • Development time. 2-3 ay production-ready için.
  • Maintenance yükü. Edge case’ler, security updates.
  • Feature gap. Rate limiting, circuit breaker, JWT validation hepsini kendi yazıyorsun.
  • Battle-testing zaman alır.
  • Team expertise gerekli.

Ne zaman custom: Very specific business logic, small-scale project, team has expertise, wants no third-party deps.

Pragmatik karşılaştırma

Real scenario’lara göre:

Scenario 1: 3 microservice, 5 developer

Basit routing + JWT auth + rate limit. Kong veya Traefik overkill. Custom Node.js gateway 1-2 haftada.

Scenario 2: 20+ microservice, Kubernetes

Traefik kuvvetli. K8s ingress controller olarak zaten var. Service discovery automatic.

Scenario 3: Enterprise, çoklu tenant, complex auth

Kong. Plugin ecosystem’i eksiksiz. OAuth, LDAP, rate limit per tenant, audit logging. Enterprise dashboard.

Scenario 4: High-throughput (10K+ RPS)

Traefik veya custom Go gateway. Kong biraz heavy olabiliyor. Profiling lazım specific durum.

Specific feature karşılaştırması

| Feature | Kong | Traefik | Custom |
|———|——|———|——–|
| JWT validation | ✓ plugin | ✓ built-in | Kendi implementation |
| Rate limiting | ✓ plugin | ✓ middleware | Kendi Redis-based |
| Circuit breaker | ✓ plugin | ✓ middleware | Kendi |
| Service discovery | Manual config | Auto (K8s/Docker) | Kendi |
| SSL cert auto | ✓ plugin | ✓ built-in (Let’s Encrypt) | Nginx reverse proxy |
| GUI dashboard | Kong Manager (paid) | Traefik Dashboard | Yok, kendi yapmanı |
| Plugin ecosystem | 100+ | 30+ | 0 |
| Cloud-native | Iyi | Exceptional | Kendi tasarım |

Auth implementation

Ben hangi gateway kullanırsam kullanayım auth logic’i şöyle kuruyorum:

Gateway level: JWT signature validation + basic user ID extraction. Invalid JWT → 401 return, stop here.

Service level: Authorization (bu user bu resource’a erişebilir mi?). Business logic.

Bu ayrımla gateway mandatory security check yapıyor, service business rule check yapıyor. Complexity dağılımı doğru.

Rate limiting across services

User’ın rate limit’i servis başına mı, toplam mı?

Total across all services: Gateway’de tutuluyor. User’ın “dakikada 100 request” limit’i tüm servislerden.

Per service: Gateway servis başına ayrı counter. “Product API’ye dakikada 1000, payment’a 10”.

İkisini birden uygulamak için gateway-level global limit + per-service override.

Redis-based distributed counter her yöntemde de çalışıyor.

Circuit breaker strategy

Downstream servis down olduğunda gateway’in davranışı:

1. Fast fail: Hemen 503 return. User instant feedback. Servis iyileşince tekrar dene.

2. Retry with backoff: 1-2 saniye retry, sonra fail. Transient failure’lar için iyi.

3. Cache last response: Servis down ama cached response var. Stale data döndür.

4. Fallback service: Secondary service’e yönlendir.

Her endpoint için hangisini kullanacağını declare et. Kong’da plugin, Traefik’te middleware, custom’da middleware.

Observability importance

Gateway tüm trafik’in görüldüğü nokta. Observability burada altın değerinde:

Access log: Her request log’a. User, endpoint, latency, status code.

Metrics: Request rate, error rate, p50/p95/p99 latency. Prometheus-compatible.

Distributed tracing: Request ID’yi downstream servislere propagate et. Tek bir user request’i tüm servisler boyunca trace.

Error tracking: 5xx response’lar detaylı log’la. Downstream service timeout’ları.

Kong + Datadog entegrasyonu, Traefik + Prometheus, custom gateway + OpenTelemetry. Tool stack değişiyor ama concept aynı.

Migration between gateways

Bir gateway’den diğerine geçmek zor iş:

  1. Audit mevcut config. Kong’daki plugin, route tüm config’i dokümante et.
  2. Target gateway’de replicate. Traefik’e port et. Config differences düzelt.
  3. Parallel run. İkisini birden çalıştır, trafik split. Behavior karşılaştır.
  4. Gradual traffic shift. %5, %10, %25, %50, %100 slowly.
  5. Decommission old. Migration tamamlandığında eski kapat.

2-4 aylık bir iş. Plan et.

Start small, evolve

Yeni bir projede ne yapılmalı?

  1. Phase 1: Direct service access. Her service public endpoint’iyle. Çok early stage için yeter.
  2. Phase 2: Basic reverse proxy (Nginx veya Traefik default). Single entry point.
  3. Phase 3: API gateway (Traefik + middleware). Auth, rate limit, logging.
  4. Phase 4: Full-featured gateway (Kong, custom). Plugin ecosystem, advanced features.

Her phase 6-12 ay arası gelişiyor. Phase 4’ü Phase 1’de kurmak premature optimization.

Sonuç

API gateway tercih’i team size, infrastructure (K8s var mı?), complexity requirement’larıyla belirleniyor.

  • Küçük-orta scale: Traefik veya custom (simple). Hızlı setup, yetersizlik olmadıkça değiştirme.
  • Enterprise / complex: Kong. Plugin ecosystem değerli.
  • K8s-native: Traefik. Service discovery automatic, SSL automated.
  • Özel business logic: Custom. Ama development investment önemli.

Ne seçersen seç, observability (logs, metrics, tracing) baştan kur. Gateway observable değilse tüm sistem opak.

Bu konuda bir projeniz mi var?

Kısa bir özet bırakın, 24 saat içinde size dönüş yapayım.

İletişime Geç