chore(iam): relationship cache on the cache store (S25 relationship cache plan: 2/3)

Summary

  • Problem: the authorization relationship cache lives in each pod's memory. Every pod fills and expires its own copy, so a read that one pod already paid for is paid again by every other pod, and the cache is lost on each restart.
  • Impact: operators and the IAM service, which sees the repeated reads. Users see nothing change, except that a cached answer is now shared across pods.
  • Change: CachingResolver now reads and writes through the Redis cache purpose client, using the store from the previous step. The entry's lifetime is the key's own expiry (iam.cache_ttl). A store failure or an undecodable value is a miss and falls through to the direct IAM call, so a cache outage never turns into a grant. Concurrent identical reads still share one fill, and the fill's write-back runs detached from the first caller's context so that caller leaving does not cancel it for the others. A caller that has already gone books no miss. The two resolver instances (decision, verdict) get separate key names. iam.cache_ttl: 0s still turns the cache off and takes the store off the read path. The spec, the config reference, config.example.yaml, the config proto comments and the observability catalog describe the shared cache.
  • Downsides: a write-back is written with the time left since the IAM answer less the store deadline (iam.timeout / 10), so it expires with the answer, every entry lives one store deadline less than cache_ttl, and one with under a millisecond left is skipped. A cache_ttl at or below the store deadline therefore never stores. A SET that stalls after the command is sent still starts a fresh cache_ttl at the server, so a stale entry can outlive its answer by that stall; an absolute expiry (PXAT) would close it and is left out. A follower that arrives just after a fill returns makes one extra IAM call. Store failures are counted (relationship_cache_events_total, event="error") but not logged. The Redis integration test and its CI service are the next step.

The first commit adds the tests before the implementation, so on its own it does not pass; the commit after it makes them pass.

Plan and spec amendments

  • Spec docs/specs/S25-iam-relationships-client.md: the Caching section now says the cache is shared across pods through the cache purpose client, and a sentence that no longer applies is removed. Security Considerations gains a line that the cache purpose store is trusted for integrity and its access control is part of the authorization boundary.
  • Configuration reference: the iam.timeout row says a tenth of it bounds each relationship-cache store command.

Size

About 880 added lines of Go, most of them in internal/iam/caching_internal_test.go, which is rewritten from a map-backed fake to the store stub and gains the coalescing, cancellation and write-back tests. The change rewrites one type and its tests, so cutting it would leave a part with a cache that is half on the map and half on the store.

Checklist

  • Ran it: built the real binary and ran it against a fake IAM service and a real Redis. Entries were shared across two pods, expired on the TTL, an invalid stored value was rewritten, IAM errors were not cached, Redis down fell through to the direct call with event="error", and cache_ttl: 0s bypassed the cache. The verdict instance was not exercised because the fake lacks LookupResources.
  • Interfaces and data: no API or schema change. A new Redis key family, ar:iam_relationships:<name>_v1:<sha256>, holds JSON-encoded relationship tuples; a value that does not decode is treated as a miss. iam.cache_ttl keeps its default, cap and 0s disable; its description and the configuration reference now say the cache is shared. The spec says so too.
  • Failure: a Redis error, timeout or bad value is a miss, so the call goes to IAM and the client sees the same answer it would get uncached. A caller that cancels gets its context error, and the shared fill continues for the others. Store failures show up as event="error" counts.
  • Rollout: old pods keep their in-memory cache and new pods use Redis, so during a deploy both exist and neither reads the other's entries. Nothing in the schema or configuration needs a matching release.
  • Hot path: one Redis GET per cache lookup, under a deadline of iam.timeout / 10, and one detached SET per fill. This replaces a map lookup. Fills stay coalesced, and the request slices are cloned once before the fill.
  • Database: no migration or query change.
  • Security: this is an authorization decision cache, so a cache that wrongly returns tuples is the risk. The key derivation is the previous step's store, unchanged here; the key is a SHA-256 of the request. Unreachable, empty or undecodable cache state falls through to the direct IAM call and never grants. A security review of the diff found nothing. A hit still skips IAM's JWT and subject checks for up to one cache_ttl, as before, now across the fleet rather than per pod.
  • ADRs: none govern the cache itself; the change keeps the spec's caching rules.
  • E2E: no scenario rows; the behavior sits behind the IAM client and is covered by unit tests with the store stub. The Redis integration test is the next step of the plan.
  • Simplicity: reuses the previous step's store and key derivation with no new abstraction, and deletes the in-process map, its mutex and its size cap. The map is not kept as a first-level cache in front of Redis, since that would leave a second copy that can go stale on its own.

Related to #1279

Edited by David Fernandez

Merge request reports

Loading
Loading