HARP logo

Caching

HARP caches API responses the way the upstream allows, cutting latency and the number of billed or rate-limited calls. It is the lever behind the Cost management and Performance optimization use cases. The cache is on by default, needs no configuration, and you can tune it as you go.

ClientYour appHARPCache-Control?RemoteAPICachestorage

Built on the standard

HTTP caching behavior is defined by the RFC 9111 (opens in a new tab) standard. HARP follows it for freshness, revalidation with ETag and Last-Modified, and the Cache-Control directives of requests and responses. The logic is provided by the Hishel (opens in a new tab) library, which interprets Cache-Control and other related headers, so the cache stores only what the remote API's headers allow.

Vary is honoured: the wrong variant is never served. Variants of one URL overwrite each other in storage, though, so a caller alternating between them gets no hits (HTTP cache documentation (opens in a new tab)).

The cache is shared between callers. Before caching an API that authenticates its callers, make sure it marks per-caller responses private or sends a Vary on the identifying header: a response is then reused only for the same header value. The cost is that several callers of one URL evict each other.

Conservative defaults

  • Only GET and HEAD responses are cached.
  • No stale response is served.
  • No heuristic freshness: a response without max-age, s-maxage or Expires is not stored.

Where the cache lives and its limits

Storage. The cache lives in HARP's blob storage, in the storage database. By default that is in-memory SQLite, so the cache belongs to one process and is lost on restart. PostgreSQL or Redis keep it and share it between instances: the cache key uses the endpoint name, not the remote host.

Size. There is no global size cap and no LRU eviction. A body over proxy.max_recorded_body_size (1 MiB by default) is not stored. An expired entry is never served. The ttl of http_cache.storage is in seconds (86400 by default, 24 hours) and counts from when the entry was stored or last refreshed by a 304. On SQL and in-memory storage, a sweep deletes unread expired entries; it starts from a cache read or write, at most once a minute by default (check_ttl_every), so with no cache traffic nothing is deleted. On Redis nothing deletes cached data: a read past the ttl drops the entry's index, its headers and body stay stored.

Clearing. No purge command exists. A successful POST, PUT, PATCH or DELETE invalidates its target.

Disabling. There is no per-endpoint setting: enabled = false under [http_cache] disables it globally. A client request with Cache-Control: no-store bypasses it, and so does a rule that sets that header on on_request, which keeps one route out of the cache: see Sensitive data.

toml
[http_cache]
enabled = false

Hits are recorded

A cache hit is recorded like any other transaction in the Traffic history, marked "cached" in the dashboard. The client sees X-Cache: HIT or MISS, and Age on hits.

Industry standard test suite

Cache behavior is measured against the Tests for HTTP caches (opens in a new tab) suite, the one behind the published results for Squid, Varnish, nginx and others. HARP passes 56.7% of it (207 of 365 tests), between nginx (55.9%) and Caddy (57.5%). Squid, at the top of the published table, passes 71.8%.

How it was measured. The HARP project recorded the score on 2026-08-18, on a commit before 0.12.0, not on the 0.12.0 tag itself. It used PostgreSQL storage, a cold cache at the start of each run, and 3 runs; a test whose verdict changed during them counts as failed. HARP's own gate fails only on a regression, a test that passed in the baseline and fails twice in a row, so the score is a baseline, not a target. The raw results (opens in a new tab) and the suite README (opens in a new tab) are in the HARP repository.

What the failures are. Of the 158 failed tests, 42 are conformance failures and 116 are tests the suite does not count as conformance: 48 are optimal (not a conformance problem) and 68 are check (informational). The 42 conformance failures fall in these groups:

  • Expires parsing and freshness: 12.
  • Age parsing and update: 8.
  • Stored headers: 8.
  • CDN-Cache-Control: 6.
  • Invalidation after an unsafe method (POST, PUT, DELETE, M-SEARCH): 4. HARP fixed it in 0.12.0, after the measurement; the fix was not measured again.
  • Conditional requests: 2.
  • Others (an interim response, the stale-while-revalidate window): 2.

Customizable at will

HARP is plug-and-play by default, but it also gives you control over APIs that handle caching poorly. If a remote service does not follow HTTP, defines its own headers, or sends Cache-Control headers that cache uncacheable content, you can correct it.

toml
[rules."*"."GET /v2/public/*"]
on_remote_response = """
response.headers["Cache-Control"] = "public, max-age=3600"
"""

In [rules."*"."GET /v2/public/*"], the first "*" matches the endpoint by name, here any endpoint; the second matches the request, here a GET on any path under /v2/public/.

The rules engine runs plain Python. Most caching rules are simple assignments and conditions.

Forcing public is for data that is the same for every caller, as under the /v2/public/ path of this example. By default, the cache does not store the response to a request carrying an Authorization header, unless the response says it may be shared with public, s-maxage or must-revalidate: a rule that adds public lifts that protection.