Skip to content

[Feature]: Add HTTP caching headers to API responses #230

@Vraj1234

Description

@Vraj1234

Pre-flight checklist

  • I have searched the existing issues

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions