liveBook · Manning

Caching

As explained in Learn Docker in a Month of Lunches, Second Edition

A cache is a temporary storage area containing previously retrieved data so that repeated requests can be served more quickly.

Caching stores a copy of an upstream response so repeated requests can be served by the proxy instead of regenerating the response in the application. This reduces response time and application traffic, but only non-user-specific content should be cached. section 20.3

Configure a reverse-proxy cache by deciding:

  • Which responses to cache, such as a public web page or an API response. section 20.3
  • How long responses remain fresh: the example uses one minute for the API and six hours for the web application. section 20.3
  • Whether stale content can be served when the upstream has errors, timeouts, invalid headers, or server failures. section 20.3

Verify a cache hit

Reload the nginx configuration, then request the same URL twice. The first response reports X-Cache: MISS and includes X-Upstream, while the repeated request reports X-Cache: HIT and omits X-Upstream because nginx serves the cached response. section 20.3

Listing 20.3 Nginx as a caching reverse proxy for HTTP content
    ...
    location = /api/image {
        proxy_pass             http:/./iotd/image;
        proxy_set_header       Host $host;
        proxy_cache            SHORT;
        proxy_cache_valid      200  1m;
        ...
    }
    location / {
        proxy_pass             http:/./image-gallery;
        proxy_set_header       Host $host;
        proxy_cache            LONG;
        proxy_cache_valid      200  6h;
        proxy_cache_use_stale  error timeout invalid_header updating
                               http_500 http_502 http_503 http_504;
        ...
    }

Use the nginx configuration listing to compare short-lived API caching with longer web-content caching and stale-response handling. section 20.3

Practice with expensive computation

Place a pi-calculation application behind a reverse proxy. Repeating a request for 50,000 decimal places should be much faster from the cache, and cached responses should remain available after the application container is stopped. section 20.6

How Learn Docker in a Month of Lunches, Second Edition covers it

How deep it goes
Explains cache hits, freshness periods, stale fallbacks, and nginx directives, but does not detail cache storage internals.
Examples
Uses an image gallery, API responses, curl headers, nginx configuration, and a pi-calculation application.
What it assumes
Assumes a Dockerized application and reverse-proxy setup while demonstrating the caching configuration incrementally.
What you can do after
Operate/configure nginx as a caching reverse proxy and verify misses, hits, freshness, and resilience with curl.
How it connects
Connects caching with reverse proxies, nginx, upstream applications, HTTP responses, authentication cookies, and load balancing.

Also explained in

Compare how 38 books explain caching →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Docker in a Month of Lunches, Second Edition. The section links point to the parts of the book it draws on.