Skip to content

Stream large S3 downloads while preserving overwrite consistency - #14605

Merged
loks0n merged 8 commits into
mainfrom
codex/stream-s3-downloads
Oct 16, 2026
Merged

loks0n merged 8 commits into
mainfrom
codex/stream-s3-downloads

Conversation

@loks0n

@loks0n loks0n commented Oct 16, 2026 •

Copy link
Copy Markdown
Member

Large S3 GETs currently cast the storage stream to a PHP string and split the complete body again before sending it. A 100 MiB GET exhausts a 192 MiB PHP process.

Capture the storage stream with its metadata under the existing object lock, then release the lock before forwarding bounded chunks. Local storage returns an open file handle; the S3 adapter returns a private temporary-file snapshot that consumes disk proportional to the object/range until delivery finishes. S3 overwrite uses a fresh version path, so unlinking the previous path cannot change the captured stream. Slow downloads no longer hold the object lock or its Redis connection during client delivery.

This removes whole-object PHP string buffering. The S3 adapter still fetches the object into its private temporary file before returning; this change does not provide end-to-end backend streaming or promise a lower backend first-byte time. The NYC benchmark will measure the effect after deployment.

Small downloads and HEAD keep their existing framing. S3 object GETs consistently return the stored representation regardless of Accept-Encoding, while preserving stored Content-Encoding metadata; both full and range responses are covered through raw, non-decoded clients. This intentionally removes automatic HTTP compression of small S3 object responses too.

Also count streamed HTTP bytes for usage, abort incomplete streams without a terminal chunk, stop when a transport write is rejected (with a red/green regression), and map distributed lock contention to S3 SlowDown/503 centrally.

Validation:

  • Live Swoole S3 API: 11 tests / 196 assertions, including overwrite completing during the first 10 MiB of a throttled download, full original SHA-256, subsequent replacement GET, empty objects, raw gzip-request full/range downloads, HEAD, preconditions, bad signatures and multipart.
  • Observed overwrite test red with the client-duration lock (replacement completed after ~33 MiB), then green with the captured stream. Encoding test red with automatic compression, then green with byte-preserving responses.
  • 100 MiB download under a 192 MiB PHP limit succeeds with original SHA-256 while overwriting after first bytes. The buffered baseline fails under the same limit.
  • HTTP FPM suite: 15 tests / 29 assertions; accounting test observed red before fix.
  • Swoole abort/empty-final E2E: 2 tests / 6 assertions; observed red with a normal terminal chunk, green with abort.
  • Targeted PHPStan, Pint and diff checks pass. The standalone HTTP package Rector/Pint/PHPStan checks pass after adding the header array assertion required by its stricter PHPStan configuration. Automated S3 regressions are in the existing Cloud S3 suite: https://github.com/appwrite-labs/cloud/pull/6611.

Tracking: https://github.com/appwrite-labs/cloud/issues/6599. The reviewed candidate is deployed in NYC via application-configuration#623. Production HEAD, multipart, large range and 100 MiB concurrent-overwrite probes pass; the full benchmark is running.

@loks0n loks0n self-assigned this Oct 16, 2026
@luxor-code-review

luxor-code-review Bot commented Oct 16, 2026 •

Copy link
Copy Markdown

Review complete. 🟡 3 medium

💬 Inline comments (3)

  • 🟡 Streaming download holds object lock for full duration — Get.php:126
  • 🟡 Terminal end() fired after close() in stream abort — Get.php:292
  • 🟡 Lock contention surfaces as generic 500, not SlowDown — Get.php:298

This PR extends packages/http/src/Response.php with chunked response support (chunk send path, per-chunk size accounting, terminal end() handling) and adds a new S3-compatible GET object endpoint under src/Appwrite/Platform/Modules/S3/Http/S3/Get.php that acquires a distributed object lock, streams the body in bounded chunks, and aborts with a retry hint on contention. Tests cover the FPM response chunking behavior.

Files Change
packages/http/src/Response.php Adds chunked streaming path to the response with header/body size accounting.
packages/http/tests/FPMResponseTest.php Adds unit tests for FPM response chunking.
src/Appwrite/Platform/Modules/S3/Http/S3/Get.php New S3 GET object endpoint streaming bytes under a distributed lock.

Reviewed commit: d806169

@github-actions

Copy link
Copy Markdown

Security rules

No new WARNING or ERROR findings from security rules.

19 existing findings tracked in .semgrep/baseline.json
  • php.appwrite.guest-write-without-abuse-limit (12)
  • php.appwrite.secret-compare-timing (5)
  • php.appwrite.weak-secret-env-default (2)

Posted by Checks / Rules. Re-runs update this comment in place. Rule details and baseline: .semgrep/README.md.

@luxor-code-review luxor-code-review 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.

Adds chunked streaming support to the Utopia HTTP response and a lock-guarded S3 GET object endpoint that streams downloads with per-chunk size accounting.

Key findings

  • 🟡 Streaming download holds object lock for full duration — Get.php:126
  • 🟡 Terminal end() fired after close() in stream abort — Get.php:292
  • 🟡 Lock contention surfaces as generic 500, not SlowDown — Get.php:298

@ghost

ghost commented Oct 16, 2026 •

Copy link
Copy Markdown

🟢 Tier S · Ready to merge

S3 GET and range responses capture the object stream and metadata while holding the object lock, then forward bounded chunks after releasing it. The HTTP response layer accounts for streamed bytes and handles rejected writes and aborted Swoole streams, with regression coverage for streaming and completion behavior.

Latest changes: The latest commit adds an explicit assertion that the empty-final-chunk E2E response includes parsed headers before its status is checked.

Verdict New comments Fixed Still open
✅ Approved 0 0 0
📂 Walkthrough · 7
File Change
src/Appwrite/Platform/Modules/S3/Http/S3/Get.php Captures full and range object streams under lock, then sends bounded chunks after releasing the lock.
src/Appwrite/Platform/Modules/S3/Http/S3/Base.php Maps distributed object-lock contention to the S3 SlowDown response.
packages/http/src/Response.php Accounts for bytes written through streams and detects rejected chunk writes without marking incomplete responses complete.
packages/http/src/Adapter/Swoole/Response.php Adds support for aborting an incomplete Swoole response.
packages/http/tests/FPMResponseTest.php Covers streamed-byte accounting and failed final chunk writes.
packages/http/tests/E2E/ResponseSwooleTest.php Tests aborted and empty-final-chunk streams, including the latest assertion for parsed response headers.
packages/http/tests/E2E/server-swoole.php Adds Swoole endpoints to exercise aborted streams and empty final chunks.

Reviewed the commits since 4cab140 · Details · Comment @hansi-codes review to re-run, or mention @hansi-codes with a question.

@ghost ghost 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.

🔵 Tier A · Looks good to merge. Summary

@github-actions

github-actions Bot commented Oct 16, 2026 •

Copy link
Copy Markdown

✨ Benchmark results

Comparing main (before) → codex/stream-s3-downloads (after).

Metric Before After Change
🚀 Requests/sec 214.95 215.63 ⚪ +0.3%
⏱️ Latency P50 79.68 ms 79.12 ms ⚪ -0.7%
⏱️ Latency P95 190.04 ms 190.76 ms ⚪ +0.4%
Per-scenario breakdown & investigation details

Metrics below reflect the current branch (after). Δ P95 compares against the base.

Scenario P50 (ms) P95 (ms) Requests RPS Δ P95 (ms)
API total 79.12 190.76 13,680 215.63 +0.72
Account 155.75 314.94 720 11.96 +37.33
TablesDB 76.58 148.02 7,440 119.64 +13.03
Storage 71.58 161.95 3,600 59.69 -23.71
Functions 117.78 229.31 1,920 32.49 +2.84

Top API waits (after)

API request Max wait (ms)
account.name.update 521.32
account.prefs.update 447.23
tablesdb.rows.create 436.27
functions.variables.update 435.57
functions.variables.create 412.78

@ghost ghost 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.

🟡 Tier B · See the inline comments. Summary

@ghost ghost 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.

🔵 Tier A · Looks good to merge. Summary

@ghost ghost 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.

🟢 Tier S · Looks good to merge. Summary

@loks0n
loks0n merged commit 8853d83 into main Oct 16, 2026
64 checks passed
@ChiragAgg5k ChiragAgg5k mentioned this pull request Oct 21, 2026
6 tasks
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