Skip to content

feat: add active cdn purge on file changes to support smart cdn v2 - #1772

Open
itslenny wants to merge 1 commit into
masterfrom
lenny/active-cdn-cache-purge
Open

itslenny wants to merge 1 commit into
masterfrom
lenny/active-cdn-cache-purge

Conversation

@itslenny

@itslenny itslenny commented Nov 28, 2026 •

Copy link
Copy Markdown
Contributor

What kind of change does this PR introduce?

Feature

What is the current behavior?

  • Storage sends a webhook on every object create, update, delete, move, and promotion
  • That webhook is the only signal the CDN layer has that something changed
  • No explicit purge events are sent on object modifications
  • There is no way disable webhooks

What is the new behavior?

  • Storage can now also purge the CDN cache directly for objects affected by a write, overwrite, delete, move, or promotion.
  • This runs alongside the existing webhook, not instead of it. A purge only fires when the change could leave stale cached bytes behind. A new object at a brand new path does not trigger a purge, since nothing was cached there yet.
  • Webhooks can now be disabled with a flag. They stay on by default so there is no breaking change to the default behavior.
  • Supports purging individual object version (depends on cdn-cache-manager deploy first)

Together, this lets a project move from the old webhook-driven cache (Smart CDN V1) to the new active-purge cache (Smart CDN V2) without a hard cutover. Both behaviors run side by side during the migration. Once every project is on V2, webhooks can be turned off.

If no purge endpoint is configured, the purge step does nothing, so this has no effect without that extra configuration.

@itslenny
itslenny requested a review from a team as a code owner November 28, 2026 00:09
@blacksmith-sh

This comment has been minimized.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔴 Smart CDN V2 tenants can keep serving stale bytes when an object changes again while an earlier purge for the same key is still in flight. The purgeCdnCache queue uses pg-boss policy exclusive (registry.ts:238), keyed by type/tenant/bucket/objectName. That policy silently drops a new job while one with the same key is queued or active. Fix: keep each per-object purge sent after a commit from being dropped by a purge already in flight, e.g. use a non-dropping policy for this queue (or a key that is unique per write) and accept duplicate purges as cheap.

Why this was flagged

Overwrite write1 commits and notify enqueues purge P1 with the key object/<tenant>/<bucket>/<name>. While P1 is active in cdnCacheManager.purge (up to the 10s abort budget), a second upsert or a delete of the same object commits and sends P2. pg-boss exclusive (registry.ts:238, options.ts:14-15) skips P2 silently because P1 holds the key. If P1 reaches the edge before write2 commits, a read in that gap caches write1's bytes, and no later purge ever clears them. The new comment at purge-cdn-cache.ts:51-52 says V2 has no other invalidation signal. On the base branch, object writes did not depend on this queue: V1 invalidation went through webhooks, which do not have this policy. The idempotency key was built for v1 manual or bucket purges, where dropping a duplicate was harmless.

Verification: run-migrations.ts:194 confirms that "v12's exclusive unique index spans created..active". So an enqueue is dropped while the earlier job is active, not only while it is queued. This change routes per-object writes into that queue. Before it, the only producer was the manual admin purge at storage.ts:325. V2 tenants then serve stale content until the edge TTL runs out.

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Beyond the inline findings, I checked whether billing/audit logging could be lost now that logLifecycleEvent has moved out of sendWebhook. It isn't lost. Outside tests, the only callers of sendWebhook/sendWebhookBatch are the new notify/notifyBatch in lifecycle-events.ts, and both log before sending. The lifecycle executor already calls logLifecycleEvent directly.

Extended reasoning...

Searching src showed that outside tests, the only callers of the class-level sendWebhook/sendWebhookBatch are notify/notifyBatch (lifecycle-events.ts:57 and :72). Both call logLifecycleEvent before sending, so turning off webhooks does not stop audit logging.

Findings marked 🟡 are optional suggestions and need no follow-up push.

Comment thread src/storage/object.ts Resolved
*/
export async function purgeObjectCache(object: PurgeableObject): Promise<void> {
try {
await PurgeCdnCache.send(toPurgeOptions(object))

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚪ Severity: LOW

The purge queue is exclusive and keys jobs by tenant, bucket, and object, so a repeat send while the first job is queued or running is silently dropped. If another write or delete lands after the first purge but before its job settles, the second invalidation is lost and stale bytes can remain served.
Helpful? Add 👍 / 👎

💡 Fix Suggestion

Suggestion: The exclusive queue policy with a tenant/bucket/object idempotency key silently drops a second purge that arrives while the first job is queued or running. Change how purge jobs are deduplicated so a later write or delete still triggers a purge. Options: (1) switch the queue policy to one that allows one queued job plus one active job, such as pg-boss stately or short, so a new purge can queue behind the running one. (2) Add a time-bucket or version component, such as the object version or an updated-at timestamp, to the idempotency key. (3) Use a short singleton window so purges that arrive after the first job starts are not dropped. This needs changes to the queue registration in src/storage/events/registry.ts and to the idempotency key logic in the event class. It is not a fix at the send call on line 58.

@coveralls

coveralls commented Nov 28, 2026 •

Copy link
Copy Markdown

Coverage Report for CI Build 46296465498

Coverage increased (+0.03%) to 88.712%

Details

  • Coverage increased (+0.03%) from the base build.
  • Patch coverage: 2 uncovered changes across 1 file (68 of 70 lines covered, 97.14%).
  • No coverage regressions found.

Uncovered Changes

File Changed Covered %
src/storage/events/cdn/purge-cdn-cache.ts 11 9 81.82%
Total (11 files) 70 68 97.14%

Coverage Regressions

No coverage regressions found.


Coverage Stats

Coverage Status
Relevant Lines: 19334
Covered Lines: 17667
Line Coverage: 91.38%
Relevant Branches: 12212
Covered Branches: 10318
Branch Coverage: 84.49%
Branches in Coverage %: Yes
Coverage Strength: 1290.89 hits per line

💛 - Coveralls

@itslenny
itslenny force-pushed the lenny/active-cdn-cache-purge branch 2 times, most recently from a2d9cc3 to 9f31910 Compare November 29, 2026 18:01
@itslenny
itslenny force-pushed the lenny/active-cdn-cache-purge branch from 9f31910 to aa5bcb5 Compare November 29, 2026 18:39

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants