Skip to content

Caching — Complete Gateway-Level Caching Guide

DodaTech Updated 2026-06-28 1 min read

In this tutorial, you will learn about Caching. We cover key concepts, practical examples, and best practices to help you master this topic.

Caching at the API Gateway stores responses to frequently requested resources so that repeated requests can be served without hitting backend services.

What You'll Learn

You'll learn caching strategies, cache invalidation, and how to configure caching in popular gateways.

Why It Matters

Caching at the gateway reduces backend load by 40-80% for read-heavy APIs, improves response times from milliseconds to microseconds, and reduces infrastructure costs.

Real-World Use

A news API caches article responses for 60 seconds. During peak traffic, 95% of requests are served from the gateway cache. The backend handles only 5% of requests, reducing server costs by 90%.

Implementation

# Kong proxy cache plugin
plugins:
  - name: proxy-cache
    config:
      response_code:
        - 200
        - 301
        - 404
      request_method:
        - GET
        - HEAD
      content_type:
        - application/json
        - text/plain
      cache_ttl: 60
      strategy: memory
      memory:
        dictionary_name: kong_cache
      vary_by:
        - request.headers["Accept"]
        - request.args["page"]
# NGINX caching configuration
proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=api_cache:10m max_size=1g inactive=60m;
proxy_cache_key "$scheme$request_method$host$request_uri";

server {
    location /api/ {
        proxy_cache api_cache;
        proxy_cache_valid 200 60s;
        proxy_cache_valid 404 10s;
        proxy_cache_use_stale error timeout updating;
        proxy_cache_background_update on;
        proxy_cache_lock on;
        proxy_pass http://backend:3000;
    }
}

Common Mistakes

| Mistake | Fix | |---------|-----| | Caching authenticated responses | User-specific data served to wrong user | Vary cache by user ID or disable for auth endpoints | | No cache invalidation | Stale data served indefinitely | Use TTLs and purge API | | Caching POST requests by default | POST is rarely idempotent | Only cache GET and HEAD | | Large cache with no eviction policy | Cache fills disk | Set max_size and eviction policy | | Not varying cache key by important headers | Wrong content served to different clients | Include Accept, Accept-Language in cache key |

What's Next

Learn about IP whitelisting at the API gateway.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro