API Gateways and API Management Questions
The management layer in front of APIs: gateways for routing, authentication, rate limiting, request/response transformation, and observability. Covers API management concerns such as developer portals, key provisioning, plan/tier enforcement, analytics, and centralized policy. The operational and productization surface of an API program.
Design an API gateway and internal layered services to support A/B testing, traffic shaping, and request-level rewriting. Discuss where experiment assignment occurs, how services stay decoupled from experiment logic, the impact on caching and CDN behavior, and how to measure experiment impact while minimizing measurement bias.
Sample Answer
Goal: Support A/B testing, traffic shaping, and request rewriting while keeping services decoupled and minimizing bias.
Where Assignment Occurs:
- Do experiment assignment at the edge/API gateway or a dedicated experiment service. Use deterministic bucketing (client id + experiment id) to ensure sticky assignments.
Decoupling Experiment Logic:
- Gateway injects assignment metadata (headers) but services read only these headers and remain agnostic to experiment internals. Experiment rules live outside services in a feature-management system.
- Services implement hooks for behavior based on header flags; no direct feature code branching on experiment config.
Traffic Shaping & Rewriting:
- Perform request-level rewriting at gateway or edge workers. For rewrites that affect caching keys, include experiment id in cache key to avoid contamination.
Impact on Caching/CDN:
- Use cache key partitioning: append experiment id or variant tag to cache key only for experiments that change response semantics.
- Where experiments don't change response, avoid adding noise to cache keys to preserve cache efficiency.
Measurement & Minimizing Bias:
- Randomize assignment at edge before routing; ensure logging of assignment at source and consistent user identity across layers.
- Use holdout groups, traffic-split controls, and differential logging to detect instrumentation bias. Avoid sampling skew by ensuring experiment-enabled traffic distribution mirrors production.
- Instrument telemetry for feature exposure, downstream effects, latency, and success metrics; use denoising techniques (covariate adjustment) in analysis.
Operational Controls:
- Offer kill-switch at gateway, automated rollback thresholds, and canary ramps. Keep experiment configs versioned and audited.
This design supports sales conversations by enabling experiments safely across customers with clear metrics and rollback capabilities.
As a solutions architect, design a high-level API gateway layer for a microservices platform handling 200k RPS across multiple service domains. Include routing, authentication, rate limiting, caching, circuit breakers, request/response transformations, observability, and multi-region considerations. Explain how the gateway handles schema changes and integrates with CI/CD.
Sample Answer
Requirements & constraints:
- 200k RPS global, multiple service domains, <100ms P95 latency target, zero-trust auth, per-tenant/fine-grained rate limits, multi-region active-active, graceful degradation.
High-level architecture:
- Edge layer: CDN (e.g., CloudFront) + WAF for static filtering and DDoS.
- API Gateway cluster per region (managed like Kong/Envoy/Gloo or AWS API GW + Lambda@Edge for custom logic) behind autoscaling LB.
- Service Mesh (Envoy + control plane like Istio) inside regions for inter-service routing, mTLS, observability.
- Central control plane for global configs (routing, auth policies, quotas).
- Data plane: gateway instances handle routing, auth, caching, transforms; persistent stores for policies (etcd/consul) and distributed cache (Redis/ElastiCache/KeyDB) with replication.
Core responsibilities:
- Routing: path/host-based + header/weight-based traffic split; support canary via header flags or traffic shadowing.
- Authentication & Authorization: JWT/OAuth2 token validation at gateway (introspect with auth service / JWKS caching); mTLS for service-to-service.
- Rate limiting: hybrid model — token bucket local limiter per instance + global quota enforced via distributed counters (Redis/CRDTs) for strict per-tenant limits.
- Caching: response caching at edge + gateway for idempotent GETs with configurable TTL and cache invalidation hooks (event-driven).
- Circuit breakers & bulkheads: per-route circuit breakers in gateway (Envoy), backoff and fallback responses; thread/process isolation for noisy consumers.
- Request/Response transformations: programmable filters (Lua/WASM) for header mapping, protocol translation (REST <> gRPC), schema versioning adapters.
- Observability: structured logs (JSON), distributed tracing (W3C traceparent → Jaeger), metrics (Prometheus), alerting (PagerDuty). Correlate traces across gateway+mesh.
- Multi-region: active-active with geo-routing at DNS + global config via control plane. Use global cache warming and read-after-write coordination with async replication; design for eventual consistency on non-critical data.
Schema changes & versioning:
- Use backward-compatible design: support v1/v2 routes simultaneously via path/header; transformations at gateway translate older client schema to new internal schema.
- API contracts stored in a schema registry (OpenAPI + protobuf). Gateway validates incoming payloads against schema and emits warnings.
- Enforce deprecation windows, automated compatibility checks in CI (contract tests).
CI/CD & governance:
- Infrastructure-as-code (Terraform) for gateway infra; gateway config stored in Git (GitOps). PRs trigger:
- Static validations (OpenAPI lint, auth/policy checks)
- Integration tests with mock services (contract tests)
- Canary deploy: push config to subset of gateways, run traffic shadowing and health checks, promote on success
- Automated rollback on SLA/metric regressions.
- Blue/Green for major gateway binary changes, with seamless config sync from control plane.
Trade-offs:
- Centralized gateway simplifies cross-cutting concerns but is a scaling/resilience risk; mitigate by distributed regional gateways + service mesh.
- Strong global rate limiting adds latency — use hybrid local-first approach to keep fast-path low-latency.
This design balances high throughput, low latency, security, and progressive deployment while enabling safe schema evolution and automated CI/CD.
How would you design a test harness to validate end-to-end behavior of an API gateway policy chain (auth, rate-limit, transformation, routing) in CI prior to deployment? Describe test types, stubbing, and expected outcomes.
Sample Answer
Requirements/goal: validate the API Gateway policy chain (auth, rate-limit, transformation, routing) in CI so regressions are caught before deployment, with fast feedback and high confidence.
High-level approach:
- Build a reusable test harness that runs in CI against an isolated test environment (containerized gateway + lightweight stubbed dependencies) and exercises policy behavior end-to-end.
Test types and purpose:
- Unit-style policy tests (fast): load individual policy modules (or config snippets) and assert inputs→outputs deterministically. Use frameworks or gateway SDKs where available.
- Expected outcome: policy logic transforms/blocks as defined.
- Integration tests (medium): deploy gateway with full policy chain, but stub downstream services and auth providers. Send requests and assert full flow (auth checked, headers modified, rate-limit applied, transformed payload, correct upstream chosen).
- Expected outcome: correct status codes, headers, body, route target chosen; rate-limit counters increment.
- Contract tests (Pact-style) for gateway↔downstream: validate expected request shape and required responses for edge cases.
- Expected outcome: downstream stubs will fail contracts if gateway changes request shape unexpectedly.
- End-to-end tests (slower): run against a staging environment closer to production with real downstreams (or heavy-weight integration stubs) to catch infra issues.
- Expected outcome: policy chain behaves under realistic latencies and auth flows.
- Performance/load tests for rate-limiting correctness under concurrent traffic.
- Expected outcome: rate limits are enforced, downstream protected, SLA metrics within bounds.
- Fault-injection/chaos tests: simulate auth provider timeouts, downstream 5xx to validate fallback/error handling and metrics.
- Expected outcome: graceful failures, circuit-breakers or retries behave as designed.
Stubbing strategy:
- Auth provider: a configurable stub that returns success/401/expired tokens and supports token introspection; can also simulate latency and jitter.
- Rate-limit store: use an in-memory Redis mock or a namespaced test Redis to verify counters; allow deterministic resets per test.
- Downstream services: lightweight HTTP stubs (WireMock, mountebank, or dockerized mock services) that record requests and return parametrized responses including errors and slow responses.
- Service discovery / routing backend: stub registry to assert chosen upstream host/port.
- Use feature flags or an overlay config in CI to point gateway to stubs.
Test harness implementation notes:
- Harness orchestration: Docker Compose / Kubernetes test namespace to bring up gateway and stubs, with health checks and deterministic startup order.
- Test runner: use API test frameworks (pytest+requests, Karate, Postman/Newman, or contract-test frameworks). Tests should be idempotent and clean state between runs.
- Assertions: HTTP status, response body schema (JSON Schema), header presence/values, logs or metrics (prometheus or gateway logs) for rate-limit increments and auth events, recorded requests at mocks to verify routing and transformation.
- Data seeding & teardown: reset rate-limit counters, auth state, and mock recordings between tests.
CI integration and gating:
- Fast suite (unit + critical integration) runs on every push; failures block merges.
- Extended suite (contract + perf + chaos) runs on nightly or pre-deploy pipeline.
- Provide clear failure signals: test name, request/response diffs, recorded mock expectations.
- Golden-file diffs for transformation outputs; failing diffs require review.
Why this works:
- Stubbing isolates policy semantics from flaky external systems, giving deterministic tests.
- Multi-layer testing (unit→integration→e2e) balances speed vs confidence.
- Contract tests prevent accidental breaking changes to downstreams.
- Performance/chaos tests validate non-functional behavior like rate-limit correctness and resilience.
Example expected outcomes (sample assertions):
- Auth: token missing → 401; expired → 401; valid token → pass through and user-id header populated.
- Rate-limit: 101 requests in window → 429 returned on 101st, backend receives 100 requests only.
- Transformation: incoming JSON with x → transformed to schema Y; specific header added/removed.
- Routing: path /payments → upstream payments-service (stub A); /orders → orders-service (stub B).
- Metrics/logs: prometheus counters increment for auth_success/auth_fail, rate_limited events logged.
This harness provides deterministic CI feedback, supports rapid iteration of policy changes, and reduces risk of deploying policy regressions to production.
Design an API Gateway for a multi-tenant B2B SaaS platform that supports per-tenant routing, per-tenant rate limits, OAuth2 authentication, and tenant isolation. Describe key components, how to implement tenant-aware policies, caching, failover, and how to ensure high performance under peak load while preventing noisy-tenant impact.
Sample Answer
Requirements (clarify): per-tenant routing to different backend clusters/features, per-tenant OAuth2 auth, per-tenant rate-limits and quotas, strict tenant isolation (security + noisy-tenant protection), high availability and low latency at peak.
High-level architecture:
- Edge layer: Global load balancer (Anycast/Cloud LB) → API Gateway fleet (stateless, autoscaling).
- Auth & policy service: OAuth2 token introspection/authorization, tenant metadata store.
- Control plane: Tenant config store (consul/etcd/DB), policy management UI/CI.
- Data plane: Gateway plugins (routing, rate-limit, caching, observability) → Tenant-specific backend clusters (or shared with logical isolation).
- Observability/Quota DB: high-throughput time-series DB and Redis for counters.
Key components & implementation:
- API Gateway (Envoy/NGINX/Traefik or managed APIGW) with dynamic config; use xDS or service mesh for pushing per-tenant routes.
- Tenant metadata: store tenant_id, routing rules, rate-limit tiers, OAuth client config, isolation level.
- OAuth2: gateway validates access tokens via introspection or JWT verification using tenant-specific keys; support token audience/claims checks.
Tenant-aware policies:
- At request arrival, gateway extracts tenant_id (subdomain, header, token claim).
- Lookup policy cache (local LRU + warm-up) for routing, rate-limits, feature flags.
- Apply per-tenant rate limiter (Leaky Bucket/Token Bucket implemented in Redis or local + Redis sync for distributed limits). For strict isolation, route to dedicated backend pool.
Caching:
- Edge-level CDN for static/responses; gateway-level response cache keyed by tenant+URI+auth-scope.
- Use per-tenant cache TTLs and size limits; cache partitioning by tenant namespace to prevent eviction storms.
Failover & resilience:
- Circuit breakers per backend and per-tenant to prevent cascading failures.
- Fallback responses or degraded feature routes.
- Multi-AZ deployment, active-active; health checks and quick failover.
High performance & noisy-tenant mitigation:
- Autoscale gateway pods based on RPS and latency; pre-warm capacity for major tenants.
- Token bucket limits per tenant with “guaranteed/borrowing” model: reserve baseline capacity, allow burst capacity if unused, and enforce hard caps.
- Prioritization: assign priority queues; throttle low-priority tenants under overload.
- Resource isolation: for top-tier tenants consider dedicated gateway/compute pools.
- Instrumentation + alerts for noisy behavior; automated quarantine: reduce burst window, move to dedicated backend, or require upgrade.
Trade-offs:
- Strong isolation (dedicated infra) increases cost; shared infra with strict soft/hard limits balances cost and fairness.
- Use of Redis for distributed counters gives accuracy but adds latency; consider local counters with periodic sync for lower latency.
Operational considerations:
- CI/CD for policy updates with validation tests, canary rollout of tenant rules.
- RBAC in control plane so tenant admins can manage configuration without affecting others.
- Regular load testing and chaos drills to validate noisy-tenant protections.
As a Solutions Architect, list the responsibilities and typical features implemented at an API Gateway layer. Provide a short example configuration (routes, authentication method, rate-limiting rules, and a cache policy) and explain which concerns you would intentionally keep out of the gateway to avoid centralization risk.
Sample Answer
Responsibilities and typical features at the API Gateway layer:
- Responsibilities: request routing, protocol translation (HTTP↔gRPC), authentication & authorization enforcement, ingress rate-limiting/throttling, request/response transformation, TLS termination, observability (metrics/tracing/logging), caching, circuit-breaking and resiliency glue, API versioning, and centralized policy enforcement.
- Features usually implemented: path/host routing, JWT/OAuth2 validation, IP and user-based rate limits, response caching, header enrichment, request size limits, CORS, health checks, and analytics hooks.
Example configuration (concise):
# Example gateway fragment (pseudo-YAML)
routes:
- id: orders-service
match: { host: "api.example.com", path_prefix: "/v1/orders" }
upstream: "orders-cluster"
auth:
type: jwt
jwks_uri: "https://auth.example.com/.well-known/jwks.json"
scopes: ["orders.read","orders.write"]
rate_limit:
unit: minute
requests: 120 # per API key / user
burst: 40
cache:
enabled: true
key: "user.id || path || query"
ttl: 60 # seconds
- id: public-status
match: { path: "/status" }
upstream: "status-service"
auth: none
rate_limit: { requests: 1000, unit: minute }
Why these choices:
- JWT at gateway enforces identity early and rejects unauthenticated traffic before hitting backends.
- Rate-limiting at edge protects backend capacity and mitigates abuse.
- Short TTL caching improves latency for repeated reads while limiting stale data risk.
Concerns to keep out of the gateway (avoid centralization risk):
- Business logic or coarse-grained authorization decisions (e.g., row-level ACLs, tenant isolation rules) — keep in service.
- Heavy data validation or large payload transformations — push to microservices to avoid bloated gateway.
- Long-running jobs, file processing, or synchronous stateful workflows.
- Complex policy engines that require frequent customizations per service.
- Persistent per-user state and session stores.
Rationale: Gateway should be a thin, fast, secure edge — enforce cross-cutting concerns and protect services, but avoid becoming a monolith of business rules or state that increases coupling and operational risk.
Unlock Full Question Bank
Get access to all 10 API Gateways and API Management interview questions and detailed answers.
Sign in to ContinueJoin thousands of developers preparing for their dream job.