fix(plugin): run cache purge queue writes in transactions

Merged
RI
1h ago
+94-8

Summary

Root cause of the /updates cache purge storm I've been chasing in prod since the #4290 release. #4303 and #4304 were needed, but they couldn't take effect because of this.

The purge queue RPCs were sent as bare SELECTs:

  • claim_updates_cache_purge
  • ack_updates_cache_purge
  • notify_updates_edge_cache_purge

Hyperdrive caches SELECTs and can answer them from its query cache without running them. These functions write (leases, the shared throttle, requeues, pending rows), so a cached answer means the write never happens.

Production evidence (2026-12-01 08:2x UTC, measured together):

  • pg_stat_user_tables.n_tup_upd on updates_cache_purge_state rose by 13 in about 75 s. Every claim that runs updates that row.
  • In the same window, the API worker logged 930 updates cache purged batches.
  • So over 900 batches came from cached claim results. Many drains replayed one cached claim, purged the same 100 apps against Cloudflare, and got 3,000+ 429s every 2 minutes.
  • Cached failed acks never applied #4304's back-off. Enqueues could be dropped the same way.

Fix: the claim, ack and both enqueue calls now run inside withPgTransaction. That's the existing pattern for queue RPCs that write (plugin_state_sync.ts mutate(), with the same comment). A transaction always reaches the database. This doesn't make a read uncacheable to dodge stale data, which AGENTS.md forbids: these calls are writes. They're one round trip per drain batch, not request-path reads.

The same bare-SELECT-of-a-writing-function pattern also exists in:

  • begin_app_storage_cleanup_pass in on_app_delete.ts
  • delta_gc_finish in cron_delta_storage_gc.ts
  • track_onboarding_demo_data in public/app/demo.ts

Not changed here, so this incident fix stays small. They're worth checking next.

Test plan

  • New tests/updates-cache-purge-transaction.unit.test.ts: the drain's claim and the enqueue both run inside a transaction.
  • Existing tests that mock utils/pg.ts get a pass-through withPgTransaction.
  • bun run test:unit (681 files), typecheck:backend, lint:backend, lint:deadcode
  • CI
  • After deploy: real updates_cache_purge_state updates match logged purge batches, 429s drop, and updates_cache_purge_pending drains.

No rendered UI changes, so there are no screenshots.

šŸ¤– Generated with Claude Code