Projects / Nimbus
Cache coherence across Cloud Run instances
Nimbus is an open-source caching library for Go built for ephemeral, autoscaling instances: an in-process L1, a versioned Redis L2 as the source of truth, and a Pub/Sub bus that broadcasts invalidations. It exists because no Go cache answered the coherence question on Cloud Run.
Context
Cloud Run runs a service as many short-lived instances that share nothing, autoscale with traffic and scale to zero. A read-heavy service wants an in-process cache, and the Go ecosystem has excellent ones: Ristretto and Otter are single-instance caches tuned for eviction throughput, and groupcache replicates across a fixed set of long-lived peers. None of them addresses a fleet of replicas that appear and disappear by the minute.
Problem
Three things break at once. In-memory caches vanish on every scale-down, and a burst from zero spins up a fleet of cold instances that stampede the database at the worst moment. There is no shared memory and no stable addressing: a request lands on any instance, caches diverge, and you cannot name an instance to invalidate it. And the external alternative, Memorystore, buys coherence with a network hop, a VPC connector and a standing bill.
Underneath sits a subtler bug, the fill-after-invalidate race. Instance B misses on key k and calls the loader, which reads the old value. Instance A writes a new value and invalidates k, which evicts nothing on B because B has nothing cached yet. B's loader returns and B caches the stale value. No future event will evict it: the only invalidation that could have caught it already fired and found nothing.
Constraints
Correctness could not depend on any instance receiving a message: push delivery is load-balanced, so each event reaches only one instance, and instances die without notice. Cloud Run allocates CPU only while a request is in flight, so background work stalls between requests. The core had to stay light, with no GCP or OpenTelemetry in the module graph of a consumer who does not use them. Eventual consistency with bounded staleness was acceptable; read-your-writes across instances was not the goal.
Decisions
One rule organises everything: L2 is the source of truth, L1 is a best-effort per-instance accelerator, and the bus is a latency optimisation, never the sole coherence mechanism. An instance that misses a broadcast still converges on its next L2 read.
- The fill invariant. Nothing enters L1 without a version minted by L2, decided atomically against concurrent invalidations: a fill reads the current version, runs the loader, then writes with a compare-and-swap on that version. If anything bumped it meanwhile, the loaded value is discarded, not cached. Versions are monotonic across a key's whole history, seeded from the server clock after an expiry, so a slow fill can never win against a key that expired and came back.
- Evictions on the bus, never values. Applying an eviction is idempotent, so delivery order does not matter and at-least-once delivery is safe. Tag events carry the resolved key list, so a receiver evicts the right keys without a local index.
- Pluggable transports. Pub/Sub pull gives true fan-out but needs always-on CPU; push is throttle-safe for request-only CPU, the common Cloud Run case. A Redis Pub/Sub transport reuses the client the L2 already holds, which makes the whole stack cloud-agnostic.
- Stale-while-revalidate under request-bound CPU. The refresh runs on a detached goroutine that makes progress whenever the instance is handling a request, with fan-out capped per instance so a synchronised stale wave after a deploy cannot become a thundering herd. A background worker pool is opt-in and documented as requiring always-on CPU.
- Fail open, never cache unversioned. When L2 is unreachable, reads go to the origin and the result is served but not written to L1, so the invariant holds through the outage. Writes still hard-fail: advertising an invalidation for a write that never landed would be worse.
- Dependency hygiene. The core depends on rueidis and
golang.org/x/synconly; the GCP transport and the OpenTelemetry adapter are separate modules.go list -m allon the core resolves to roughly a dozen modules instead of two hundred.
Results
v0.1.0 ships the L1, the Redis L2, stampede protection, stale-while-revalidate, the cross-instance bus, OpenTelemetry metrics and a deployable Cloud Run example with Dockerfile and Terraform. The race above is closed by a test, TestFillInvariantUnderInvalidate, that blocks a loader mid-fill, invalidates the key, releases the loader and asserts the stale value is never observable.
Integration tests run against real Redis and the Pub/Sub emulator through testcontainers, and a toxiproxy in front of Redis is cut and healed to prove that reads fail open while L2 is down and resume versioned fills on recovery. Hot paths make zero allocations per operation for string keys: an L1 hit takes around 95 ns and a GetOrLoad hit around 181 ns. Both were measured under emulation, so read them as indicative rather than absolute.
What the harness deliberately does not reproduce: Cloud Run CPU throttling, cold-start and subscription-creation latency, partial partitions with redelivery, and push load-balancer distribution. Those are validated only on a real deployment.
Next
Tag invalidation today walks a tag's members and tombstones each one in pipelined batches. The post-1.0 path is a per-tag epoch, which makes it O(1) and removes the reverse-cleanup problem. The hand-written L1 sits behind a store interface so a faster engine can be dropped in, and minor releases may still change the API until 1.0.