← Back to System Design, page 4

system-design-0064 · Easy fresher

Design an API gateway that sits in front of 200 microservices and handles authentication, rate limiting and routing for 200,000 requests per second at peak.

Run a fleet of stateless gateway nodes behind a load balancer. Each request goes through a chain of filters: TLS termination, authentication (verify a JWT locally or check a cached session), rate limiting against a shared counter store, routing by path to the right service, and logging. Routes and policies are loaded from a configuration store and refreshed without restarts. At 10,000 requests per second per node, 200,000 requests per second needs 20 nodes, or 30 with headroom.

Full answer

Requirements

Functional:

  • Route /v1/orders/* to the orders service, /v1/users/* to users, and so on.
  • Authenticate callers, apply rate limits per key or user, and add request IDs.
  • Transform or aggregate in simple cases (for example strip internal headers).

Non-functional:

  • Adds only a few milliseconds of latency.
  • Highly available: if the gateway is down, everything is down.
  • Configuration changes take effect within seconds without restarts.

Capacity estimates

Assumptions: 200,000 requests per second at peak, 10,000 requests per second per gateway node, 95% of token checks served from a local cache when tokens are opaque.

  • Nodes: 200,000 / 10,000 = 20 nodes; 30 with 50% headroom for failures and spikes.
  • Auth service load with opaque tokens: 5% misses x 200,000 = 10,000 lookups per second. With signed JWTs verified locally, this drops to almost zero.
  • Rate-limit store: one counter update per request, 200,000 per second, which a Redis cluster with several shards handles.

API

The gateway exposes the services' APIs. Its own admin API:

PUT /admin/routes/orders {"match": "/v1/orders/*", "upstream": "orders.svc:8080", "auth": "jwt", "rate_limit": {"per_user": "100/m"}, "timeout_ms": 2000} GET /admin/routes

Every proxied response carries X-Request-Id; limited requests get 429 with Retry-After.

Data model

  • Route table: path pattern, methods, upstream service, auth policy, limits, timeouts, retry policy. Stored in a configuration store (etcd, a database) and cached in every node's memory.
  • Rate-limit counters in Redis, keyed by (policy, caller, window).
  • Public keys for JWT verification fetched from the identity provider and cached.

High-level design

mermaid
flowchart LR
  C[Clients] --> LB[Load balancer]
  LB --> G1[Gateway node]
  LB --> G2[Gateway node]
  G1 --> RL[(Rate-limit store)]
  G1 --> CFG[(Route config)]
  G1 --> IDP[Identity provider keys]
  G1 --> S1[Orders service]
  G1 --> S2[Users service]
  G1 --> LOG[Logs and metrics]

Request pipeline

  1. TLS termination.
  2. Match the route (a prefix tree on paths gives fast lookup across hundreds of routes).
  3. Authenticate: verify the JWT signature and expiry with cached keys; reject with 401 if invalid.
  4. Authorise coarse rules (for example "admin routes need the admin scope"); fine-grained checks stay in services.
  5. Rate limit; reject with 429 if over.
  6. Forward to a healthy service instance with a timeout; retry only idempotent methods.
  7. Emit access logs, metrics and a trace span.

Bottlenecks and trade-offs

  • Single choke point. Keep nodes stateless, run them across zones and keep the logic small; heavy business logic in the gateway makes every deploy risky.
  • Rate-limit store outage. Fail open with local per-node limits so a Redis problem does not block all traffic, or fail closed for sensitive endpoints.
  • JWT versus opaque tokens. JWTs avoid a lookup per request but cannot be revoked instantly; short expiry times (5 to 15 minutes) limit the risk.
  • One gateway or several. A separate gateway per client type (web, mobile, partners) lets each evolve independently, at the cost of more components.
  • Configuration push must be validated and rolled out gradually; a bad route table is an outage for everyone.

By level

Fresher: Explain that the gateway is the single front door that checks who you are, limits how often you call and forwards you to the right service. A fresher should sketch the route table.

Mid: Size the fleet, design the filter chain, choose JWT verification, and handle rate-limit store failure. Mention timeouts and safe retries.

Senior: Discuss blast radius, staged config rollout, per-client gateways, token revocation and zero-trust between gateway and services. Expect clear limits on what belongs in the gateway.

Key points

  • Stateless gateway nodes behind a load balancer
  • Filter chain: TLS, auth, rate limit, route, observe
  • Verify JWTs locally with cached keys
  • Config store with hot reload and staged rollout
  • Keep business logic out of the gateway

Common mistakes

  • Putting business logic in the gateway
  • Calling the auth service on every request with no caching
  • Retrying non-idempotent requests
  • Having no plan for rate-limit store failure

Follow-ups

  • How would you do canary routing of 5% of traffic?
  • How do you revoke a JWT before it expires?
  • Should services trust headers set by the gateway?

Tips

  • List the filter chain in order
  • Do the node count math
  • Mention fail-open versus fail-closed

Evaluation rubric

  • Clear request pipeline
  • Capacity math for nodes and dependencies
  • Sensible auth and rate limiting
  • Availability and config trade-offs