Skip to main content

Caching responses at the gateway with proxy-cache

Answer repeated GETs from the gateway with proxy-cache: strategy, TTL, cache key, methods, a bypass rule, and the cache status header on each response.

By Efsun Balcı Updated Gateway

What you will have at the end

A Route, catalog-api, whose GET responses are stored on the gateway node for 60 seconds. The first request for a path reaches the backend; the next ones for the same path are answered from the cache without a backend call, which the backend’s own log confirms. A query parameter lets a client skip the cache, a POST is never served from it, and once the 60 seconds pass the next request fetches a fresh copy. Every answer carries a cache status header that says which of these happened, and Traffic Explorer shows a cached answer with gateway as the Upstream node and no backend time.

Prerequisites

  • A Route to a backend that answers GET. Here catalog-api serves /catalog/* from the Upstream httpbin-backend, an echo service at httpbin:8080 whose log prints every request it receives, so the article can count backend calls. The Route rewrites /catalog/… to the service’s /anything/… path with proxy-rewrite.
  • Traffic logging turned on under Traffic Explorer › Settings, and a role that may manage Routes. The screenshots below were taken as nora.becker.

Where the copy lives and for how long

Open the Route, select the Plugins tab, Add plugin, and choose proxy-cache from the catalog. Under Basics:

  • Cache strategy: memory. The other choice, disk, keeps the copies on the node’s disk; the note under the field says what else changes with it: under disk the lifetime of a stored response comes from the gateway’s own cache configuration, not from this Plugin, which is why the lifetime field is drawn under memory alone.
  • Cache TTL: 60 seconds. A catalog that changes a few times a day could hold its copies much longer; the article keeps it short so the expiry can be shown.
  • Cache control stays off. On, the Plugin obeys the backend’s own Cache-Control and Expires headers instead of the TTL, and a response without them is never stored.
  • Cache set cookie stays off: a response that carries Set-Cookie is never stored, which keeps one user’s session out of another user’s answer.
  • Cache zone: memory_cache, the zone the gateway declares for the memory strategy. The note under the field says the zones that exist as shipped, disk_cache_one for disk and memory_cache for memory, and that any other name is refused on save.
  • Cache key: $host and $request_uri when new, so one copy per host and full path including the query string. A header or a query parameter can be added as a further part, $http_accept_language for a catalog that answers in several languages; the note says a variable the request lacks adds nothing to the key.

The Basics section of the proxy-cache form on catalog-api: Cache strategy memory, Cache TTL 60 s, Cache control off, Cache set cookie off, Response body limit at its default and the Cache zone field Memory strategy, 60-second lifetime. The two switches say in a sentence each what they change.

One thing the form does not say and the operator should know: the cache lives on each gateway node, in that node’s memory or on that node’s disk. An Environment with two nodes warms two caches, and a copy stored on one node is not served by the other.

Which requests are cached, and how to skip the cache

Under Behavior:

  • Cached request methods: GET and HEAD when new. POST can be ticked for a backend whose POSTs are reads; it is not here.
  • Cached response statuses: 200, 301 and 404 when new. A 404 for a product that does not exist is a valid thing to cache for 60 seconds.
  • Cache bypass: $arg_refresh. The note explains the rule: the cache is skipped when the parts join into anything but empty or 0. A request with ?refresh=1 therefore goes to the backend; a request without the parameter reads the cache.
  • No cache is the other side: the response is not stored when its parts are set. It stays empty here.
  • Consumer isolation stays on: each Consumer gets its own entries, so an answer cached for one caller is not served to another.

The Behavior section of the proxy-cache form: Cached request methods GET, HEAD and POST unticked with "GET, HEAD when new" under them, Cached response statuses empty with "200, 301, 404 when new", Cache bypass holding $arg_refresh, No cache empty, Hide cache headers off The defaults are written under the fields, so an empty field is not an unknown. One bypass rule, by query parameter.

Select Add plugin. The Route update is in the audit log with the proxy-cache block as the difference.

What a client sees

Three GETs for the same path, one after the other:

curl -i http://localhost:9281/catalog/items
GET 1 → Apisix-Cache-Status: MISS
GET 2 → Age: 0  cache status: HIT
GET 3 → Age: 0  cache status: HIT
backend calls for /anything/items: 1

The cache status header, shown above under the name the engine gives it, reads MISS on the first answer and HIT on the next two, and the backend’s log has one line for the three requests. Age is how old the copy is, in seconds.

The bypass parameter and a POST both reach the backend:

GET ?refresh=1 → cache status: BYPASS
POST           → cache status: MISS

A POST is MISS every time, because the method is not in the cached list and nothing is stored for it. A HEAD got a 404 from the gateway in this measurement, with no route matched in Traffic Explorer: the Route accepts GET and POST only, which the cache settings cannot override.

After the TTL:

GET after 60 s → cache status: EXPIRED

EXPIRED means the copy was there, too old, and replaced by a fresh answer from the backend; the backend’s log shows the call. Over the whole run the backend answered three GETs: the first one, the bypass, and the one after the expiry.

What Traffic Explorer shows

Every request above is a record, cached or not. Open Observe › Traffic Explorer and search for the path. The two cached answers carry gateway as the Upstream node and 0 ms, because no backend was asked; the first GET, the bypass, the POST and the GET after the expiry carry the backend’s address and its time.

Traffic Explorer filtered to the path /catalog/items: the first GET with the backend address and 24 ms, two GETs with gateway as the Upstream node and 0 ms, a HEAD answered 404 with no route matched, the GET with ?refresh=1 and the POST with the backend address, and the GET after the TTL with the backend address again A cached answer is a request the backend never saw: Upstream node gateway, 0 ms. The rest name the backend.

The Upstream’s Analytics counts only the requests handed to a node, so a cache in front of it shows as the gap between the Route’s count and the Upstream’s.

What the audit log keeps

Adding proxy-cache is a Route update: who, when, which Route, and the Plugin block as the difference, with the TTL, the zone, the bypass rule and the strategy on the after side. There is no secret in this Plugin, so nothing is masked.

The Change section of the audit record of the catalog-api Route update: the proxy-cache block added on the after side with cache_ttl 60, cache_zone memory_cache, cache_bypass $arg_refresh and cache_strategy memory, next to the unchanged proxy-rewrite block The cache as the engine received it. A later change to the TTL is a second record with the old and the new value.

What to do next

Written by
Efsun Balcı
Engineering intern, G8
Was this helpful?
Next step

See G8 in a demo

We’ll show you the console with APIs and AI traffic like yours, and answer the questions these articles leave open.

  1. 01Demo
  2. 02Proof of conceptOn Docker for a quick start, or on the infrastructure you plan to run G8 on.
  3. 03LicenseWe prepare a quote for your edition and nodes, and you receive a signed license file.