Pre-flight checklist
Problem to solve
API responses carry no Cache-Control, ETag, or Last-Modified headers. Every request hits Cloud Run and the database even when the underlying data hasn't changed since the last ETL run. This wastes Cloud Run compute and increases latency for repeat callers (browser, frontend SWR hooks, any CDN in front of the service).
Proposed solution or API
Add a FastAPI middleware (or per-router dependency) that sets:
Cache-Control: public, max-age=<ttl> — TTL should match the ETL cadence (e.g. 1 hour or until next refresh)
ETag based on a hash of the response body or the last etl_run_id in the DB
Last-Modified from the most recent materialized view refresh timestamp
Discovery endpoints (lists of crops, resources, geoids) and data endpoints can use the same TTL since they all update only on ETL refresh.
Alternatives considered
Redis/in-memory cache at the application layer (tracked separately in #135). HTTP headers are cheaper and work at the CDN/browser level with no infrastructure changes.
Additional context
Ref: #135 (caching sub-task). Related to Cloud Run cost optimization — cached responses don't consume request CPU.
Pre-flight checklist
Problem to solve
API responses carry no
Cache-Control,ETag, orLast-Modifiedheaders. Every request hits Cloud Run and the database even when the underlying data hasn't changed since the last ETL run. This wastes Cloud Run compute and increases latency for repeat callers (browser, frontend SWR hooks, any CDN in front of the service).Proposed solution or API
Add a FastAPI middleware (or per-router dependency) that sets:
Cache-Control: public, max-age=<ttl>— TTL should match the ETL cadence (e.g. 1 hour or until next refresh)ETagbased on a hash of the response body or the lastetl_run_idin the DBLast-Modifiedfrom the most recent materialized view refresh timestampDiscovery endpoints (lists of crops, resources, geoids) and data endpoints can use the same TTL since they all update only on ETL refresh.
Alternatives considered
Redis/in-memory cache at the application layer (tracked separately in #135). HTTP headers are cheaper and work at the CDN/browser level with no infrastructure changes.
Additional context
Ref: #135 (caching sub-task). Related to Cloud Run cost optimization — cached responses don't consume request CPU.