envoy-guide
Module 6 · Real-world Recipes·14 min·intermediate

Rate limiting (local + global)

Cap request volume per pod and per tenant across the fleet.

Two flavors, different jobs

Envoy gives you local rate limiting (cheap, per-pod) and global rate limiting (a shared service that all your Envoys consult). They serve different purposes — and you usually want both.

  • Local is a cheap circuit breaker. "Don't let a single client melt this Envoy pod."
  • Global is fleet-wide enforcement. "This tenant can use 1000 req/min total, no matter how many Envoys are running."

Local rate limiting

A token bucket per Envoy worker. No external service. Returns 429 when the bucket is empty.

YAML
          http_filters:
          - name: envoy.filters.http.local_ratelimit
            typed_config:
              "@type": type.googleapis.com/envoy.extensions.filters.http.local_ratelimit.v3.LocalRateLimit
              stat_prefix: http_local_rl
              token_bucket:
                max_tokens: 100
                tokens_per_fill: 100
                fill_interval: 60s          # 100 req / minute
              filter_enabled:
                default_value: { numerator: 100, denominator: HUNDRED }
              filter_enforced:
                default_value: { numerator: 100, denominator: HUNDRED }
              response_headers_to_add:
              - append_action: OVERWRITE_IF_EXISTS_OR_ADD
                header: { key: x-local-rate-limit, value: "true" }

filter_enabled and filter_enforced are intentionally separate — you can run the filter in observe-only mode (enabled: 100, enforced: 0) to see who'd be denied without actually denying. Useful before tightening limits.

Per-tenant local limits

Define descriptors on the route, then per-descriptor token buckets in the filter:

YAML
  routes:
  - match: { prefix: "/v1/" }
    route:
      cluster: api
      rate_limits:
      - actions:
        - request_headers:
            header_name: x-tenant-id
            descriptor_key: tenant
YAML
- name: envoy.filters.http.local_ratelimit
  typed_config:
    "@type": ...LocalRateLimit
    stat_prefix: per_tenant
    token_bucket: { max_tokens: 1000, tokens_per_fill: 1000, fill_interval: 60s }  # default
    descriptors:
    - entries: [{ key: tenant, value: free }]
      token_bucket: { max_tokens: 60,   tokens_per_fill: 60,   fill_interval: 60s }
    - entries: [{ key: tenant, value: pro }]
      token_bucket: { max_tokens: 6000, tokens_per_fill: 6000, fill_interval: 60s }

Free tier: 60/min. Pro tier: 6000/min. Default (unknown tenant): 1000/min.

Local buckets don't share state

If you run 5 Envoy replicas with a 1000-req/min local limit, the combined capacity is 5000 req/min — and a hot pod that gets unlucky load balancing might still 429 at 200, while another pod sits idle. For true fleet-wide caps you need the global filter.

Global rate limiting

The ratelimit filter delegates to an external rate-limit service (RLS) over gRPC. The standard implementation is envoyproxy/ratelimit, a small Go service backed by Redis.

YAML
          - name: envoy.filters.http.ratelimit
            typed_config:
              "@type": type.googleapis.com/envoy.extensions.filters.http.ratelimit.v3.RateLimit
              domain: mcp_api                       # matches RLS config
              transport_api_version: V3
              rate_limit_service:
                transport_api_version: V3
                grpc_service:
                  envoy_grpc: { cluster_name: ratelimit_cluster }

The domain matches a config on the RLS side:

YAML
# ratelimit-config.yaml
domain: mcp_api
descriptors:
- key: tenant
  descriptors:
  - key: tenant_id
    value: acme
    rate_limit: { unit: minute, requests_per_unit: 1000 }
  - rate_limit:   { unit: minute, requests_per_unit: 60 }   # default

Every Envoy in the fleet sends Should I allow? for each request, including the descriptors. Redis tracks the global counters. One source of truth, atomic.

Hands-on: see local rate limiting kick in

Hands-on · 429 in five seconds

Take any envoy.yaml from earlier modules. Add the local_ratelimit block above with a tighter bucket so it's easy to trip:

YAML
              token_bucket:
                max_tokens: 5
                tokens_per_fill: 5
                fill_interval: 60s

Restart, then:

Shell
for i in 1 2 3 4 5 6 7; do
  curl -s -o /dev/null -w "%{http_code}\n" localhost:10000/get
done

You should see 200 200 200 200 200 429 429. The sixth request and onwards get a fast 429 with the x-local-rate-limit: true header from your config.

Check the stats:

Shell
curl -s localhost:9901/stats | grep http_local_rl

You'll find counters like http_local_rl.rate_limited incrementing — the same data you'd graph in Grafana.

Local + global, stacked

The pro setup uses both filters in this order:

  1. local_ratelimit first — cheap, per-pod, catches obvious abuse before you spend a network round-trip on the RLS.
  2. ratelimit second — authoritative, fleet-wide, per-tenant.

If the local filter trips, you save the global lookup. If it passes, the global filter enforces real fairness across the fleet.

Don't 429 on health checks

Always exclude /healthz, /readyz, and similar operational endpoints from rate-limit routes. K8s probes can hammer them faster than your bucket allows, and you'll mark pods unhealthy for no reason.

Real-world example · A SaaS API platform with tiered pricing
Problem
Free-tier users were occasionally bursting hard enough to slow paid customers' requests. Per-tenant quotas needed to be honored across a fleet of Envoy pods, with friendly 429 responses (not 503s) and accurate retry-after hints.
Envoy solution
Local local_ratelimit per pod as a fail-fast safety net. Global ratelimit filter calling out to envoyproxy/ratelimit + Redis with descriptors keyed on the tenant ID extracted from the JWT (via request_headers action). Per-tier limits live in one YAML on the RLS, so plan changes don't require an Envoy reload.

What's next

You now have the four production essentials: gRPC routing, JSON transcoding, auth, and rate limiting. Lesson 21 stacks them all into the MCP gateway recipe — the complete config for the architecture you sketched.

Quick check

You run 5 Envoy replicas and want a tenant to be capped at 1000 req/min total. Which approach works?

Practical task
Finish the quick check and tick off the task to unlock.
Quick checkTasks 0/3