Skip to content

Write cached responses atomically#491

Merged
thomashoneyman merged 1 commit into
masterfrom
atomic-cache-writes
Jul 6, 2026
Merged

Write cached responses atomically#491
thomashoneyman merged 1 commit into
masterfrom
atomic-cache-writes

Conversation

@thomashoneyman

@thomashoneyman thomashoneyman commented Jul 6, 2026

Copy link
Copy Markdown
Member

Every request that renders a cacheable page writes the response body to the same cache file, so concurrent requests for one page race and the losers fail with resource busy (file is locked) and are served a 500.

@flip111 tracked this down in #482 — hitting the homepage with 400 connections caused about a third of responses to produce 500s, each paired with the withBinaryFile: resource busy error. The response is now written to a temporary file and renamed into place, which is atomic.

Verified using the same approach as @flip111; all ~3600 responses are 200s without errors, the cache file looks correct, and no temp files are left behind.

Every request that renders a cacheable page writes the response body to the
same cache file, so under concurrent requests for one page the losers fail
with "resource busy (file is locked)" and are served a 500 - the behaviour
reported in #482, where a homepage flood turned a third of responses into
errors. A half-written file was also briefly visible to nginx, which serves
the cache directory directly.

The response is now written to a temporary file (suffixed with the writing
thread's id, so concurrent writers cannot collide on that name either) and
renamed into place, which is atomic. Under the same 400-connection homepage
flood, every response is now a 200.
@thomashoneyman thomashoneyman merged commit f35f239 into master Jul 6, 2026
2 checks passed
@thomashoneyman thomashoneyman deleted the atomic-cache-writes branch July 6, 2026 20:54
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.

1 participant