Engineering note · AuraRisk
Keeping flood risk lookups up when the weather API isn't
- Project
- AuraRisk — Personal project
- Issue
- #19 Resiliency, caching, and compliance for Weather APIs
- My role
- Author of PR #41
- Stack
- Go · TypeScript · Open-Meteo · Nominatim · Prometheus
- Status
- Merged 8 Oct 2026
The problem
Every call to AuraRisk's /api/risk made a direct, synchronous request to Open-Meteo for weather data. There was one attempt with a 10-second timeout and no cache. If Open-Meteo was slow, down or rate-limiting us, risk lookups failed with it, and the push notifier depended on the same path.
There was also a terms-of-use side. The free Open-Meteo API is for non-commercial use. On the client side, the web search box called Nominatim on every keystroke. Nominatim's usage policy forbids search-as-you-type, asks for at most one request per second, and expects repeated queries to be cached.
What I did
On the backend I split the work in two. An OpenMeteoClient handles single requests. A WeatherService sits in front of it and decides when to call it at all.
- Grid cache. Weather is cached per cell of about 2 km for 15 minutes, and fetched at the cell's centre. Concurrent requests for the same cell share one fetch. That fetch runs detached, so one cancelled caller doesn't fail the others.
- Timeouts and retries. Each attempt has a 5-second timeout. Network errors and 5xx responses are retried up to 3 attempts with jittered backoff. A 429 or other 4xx is not retried.
- Circuit breaker and call budget. After 5 consecutive failures, calls pause for 30 seconds and then one probe is let through. A per-instance budget, 300 calls a minute by default, keeps us under the provider's limit.
- Stale-data policy. While the provider fails, cached data up to 3 hours old is served with
stale: true. Past that, the API returns 503 withRetry-After: 60. Responses now carrysource_timestamps. - No alerts from old data. The notifier skips a subscription when its weather is stale and tries again on the next run.
- Paid plan support. Setting
OPEN_METEO_API_KEYswitches to the customer host. The key is kept out of error messages, and production logs a warning when it is unset.
On the web and mobile side, search now runs on submit, not per keystroke. The shared searchPlaces function spaces requests at least one second apart and caches the last 50 queries. The web dropdown gained OpenStreetMap attribution and a "no matches" message.
I also added two metrics, aurarisk_weather_lookups_total{result} and aurarisk_provider_circuit_open{provider}, and updated the runbook and alert text.
How I checked it
I ran go vet and go test -race. The new unit tests cover grid caching, fetching at the cell centre, shared fetches, a waiter giving up, the stale window, disabling stale serving, the breaker, the call budget and eviction. Client tests cover retries on 5xx, no retry on 4xx, the request shape, keeping the API key out of errors, and decoding.
A database integration test, TestNotifierSkipsStaleWeather, needed a Postgres instance, so it was left to CI for this PR. On the client side, I ran the mobile tsc and jest, with a new test for the place-search cache, uncached failures and one-second spacing. The web build passed.
What I learned
Most of the resilience came from not calling the provider: caching by area and sharing in-flight fetches. Retries and the breaker matter less if the cache does its job.
Stale data is useful for showing a map, but not for sending a warning. Making that distinction explicit, with a flag the notifier checks, felt more honest than one fallback for everything. Some work is still open. The clients don't yet show stale or source_timestamps, which is tracked in issue #25, and commercial use will need a paid Open-Meteo plan.