Course contentsShow
← Docker, Kubernetes & CI/CD
Lesson 386 of 8026. Kubernetes Traffic and ScalingFree lesson

Controlling Traffic Flow with Rate Limiting

Protect your backend services from overload by implementing rate limiting rules at the Ingress controller level for specific endpoints.

In this lesson, you will learn how to shield your backend services from traffic spikes and potential denial-of-service attacks by implementing rate limiting at the Ingress controller level.

Why Rate Limiting Matters

Even with autoscaling mechanisms in place, an unexpected surge of traffic can overwhelm your application pods, leading to increased latency or complete service failure. Rate limiting acts as a traffic police officer at the edge of your cluster. Instead of allowing every request through, the Ingress controller enforces a "speed limit," ensuring that no single client or IP address can monopolize your backend resources.

Implementing Limits with NGINX Ingress

Most modern Ingress controllers, like NGINX, allow you to define these constraints using Kubernetes annotations. By applying these to your Ingress resource, you can restrict the number of requests allowed per second from a specific source.

Consider this configuration:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: api-ingress
  annotations:
    nginx.ingress.kubernetes.io/limit-rps: "5"
    nginx.ingress.kubernetes.io/limit-burst: "10"
spec:
  rules:
  - host: api.example.com
    http:
      paths:
      - path: /data
        pathType: Prefix
        backend:
          service:
            name: data-service
            port:
              number: 80

In this example, we set a limit of 5 requests per second (limit-rps) for the /data endpoint. The limit-burst setting provides a "buffer," allowing for short bursts of up to 10 requests before the controller starts returning a 503 Service Temporarily Unavailable status code to clients exceeding the threshold.

Strategic Application

You shouldn't apply the same rate limit to every endpoint. Public-facing login endpoints often require much tighter limits (e.g., 2 requests per second) to prevent brute-force attacks, while static asset endpoints or public API data might handle higher volumes. By configuring these rules directly at the Ingress layer, you offload the burden of traffic management from your application code, keeping your services lean and resilient.

Key Takeaway: Rate limiting at the Ingress level preserves your backend's stability by dropping excessive requests before they ever reach your application containers.