Caching in Kubernetes Ingress
Caching is a key strategy to reduce backend load and improve response times for clients. In Kubernetes, caching is usually implemented through the Ingress controller — particularly with NGINX.
Why Use Caching?
- Reduce load on backend services
- Improve speed for repeated requests
- Minimize cost and bandwidth
- Smooth handling of traffic spikes
Common Caching Setup: NGINX Ingress
The NGINX Ingress Controller supports proxy caching via annotations.
Step 1: Enable caching in your Ingress resource
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: my-app-ingress
annotations:
nginx.ingress.kubernetes.io/proxy-cache: "my-cache-zone"
nginx.ingress.kubernetes.io/proxy-cache-key: "$scheme$request_method$host$request_uri"
nginx.ingress.kubernetes.io/proxy-cache-use-stale: "error timeout updating http_500 http_502 http_503 http_504"
spec:
rules:
- host: myapp.example.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: my-app
port:
number: 80
Step 2: Define the cache zone in the NGINX ConfigMap
apiVersion: v1
kind: ConfigMap
metadata:
name: nginx-configuration
namespace: ingress-nginx
data:
proxy-cache-paths: |
my-cache-zone keys_zone=my-cache-zone:10m max_size=100m inactive=60m use_temp_path=off;
⚠️ Requires restarting the Ingress controller or reloading its config.
Cache Invalidation
- Change cache key or annotations to force refresh
- Set low TTLs via response headers (
Cache-Control
) - Manually purge cache (advanced)
Gotchas
- Not all responses are cached (e.g., dynamic API, POST requests)
- Ensure proper headers to avoid serving stale data
Conclusion
Using caching in Kubernetes Ingress can drastically improve performance with minimal changes. It’s especially effective for static content or semi-dynamic pages. With proper setup and tuning, you can save compute resources and deliver faster user experiences.