plugins

BareProxy Response Cache Plugin: Micro-Caching for Dynamic Sites

Status: planned, number 18 of 21 in BareProxy’s build order. The plugins are built easiest first, and this one is about three or four days of coding: a cache has to be exactly right about what it may keep and share. It comes after the Auth gate plugin. This post describes what it will do, and it will be updated as it is built.

A dynamic site often renders the same page for thousands of visitors in a row. A news front page, a product listing, an API endpoint that changes once a minute. Each request still goes to the app, and the app builds the same answer again. When a link gets shared widely, that is how a small server falls over.

Micro-caching keeps each answer for a few seconds, or a minute, and serves it from the proxy in the meantime. Nobody sees stale content in any way that matters, and the app does a fraction of the work. A one-second cache on a page that gets a thousand requests a second turns a thousand renders into one.

What It Will Do

  • Cache what may be cached. The plugin follows the response’s own Cache-Control headers, and a rule in its config can set a time for paths that don’t send any. Responses that set cookies or say private are never shared between visitors.
  • Key carefully. The method, host, path and query make the key, plus any request headers the config names, such as Accept-Language.
  • Purge by path prefix. A purge request, allowed only from addresses the config lists, clears everything under a prefix, such as /blog/ after a new post.
  • Serve stale on error. When the app is down or slow, the last good copy can go out for a while, which turns an outage into a slightly old page.

The cache lives in the plugin’s own store on disk, with a size cap the config sets:

plugin cache /etc/bareproxy/plugins/response-cache.wasm
  config /etc/bareproxy/plugins/cache.json
  store 1GB
  body response

site example.com
  use cache
  route /* -> app

Seeing What It Did

A cache that can’t be inspected is a source of mystery bugs. Every response the plugin touches carries a note in its record: a hit and the copy’s age, a miss and why, or a bypass because of a cookie. why on any request will say whether the app was asked at all.

This plugin replaces the “Cache” module of BareProxy’s earlier plan. The whole program is on the plugins page.