Repository navigation
Conversation
… connection hyper-util's HttpConnector leaves TCP_NODELAY off. With Nagle's algorithm on, the kernel holds the small tail of a streamed response until the Runtime API sends its delayed ACK. This adds about 40 ms to a small streamed response. Enable TCP_NODELAY on the default connector of ClientBuilder and PooledClientBuilder. A connector passed to with_connector is used as-is. Also add an integration test for response streaming through API Gateway.
bnusunny
left a comment
There was a problem hiding this comment.
Looks good. I reproduced the stall and the fix removes it.
Results. On a local mock Runtime API, streamed invokes on main took about 50 ms, and about 0.04 ms with this PR. A packet capture shows the client holding the rest of the response until a delayed ACK arrives, as described. On AWS (us-west-2, this PR's template, main client vs this PR):
main client |
This PR | |
|---|---|---|
| First invoke in a new sandbox (11 each) | 41.7–45.9 ms | 1.4–2.5 ms |
| Warm invokes over 20 ms (615 each) | 1 | 0 |
Question. I couldn't reproduce the 61–70% warm stalls through API Gateway, at 128 or 1024 MB. How did you measure them? The first-invoke case reproduces every time, so it may be worth leading with it in the description.
Suggestion (non-blocking). Instead of the wildcard API ID in ALLOWED_RESOURCE, build the policy from method_arn (<apiId>/<stage>/*/*). That keeps it scoped to the calling API and drops the new env var. (lambda-integration-tests/src/authorizer.rs)
CI note. run-integration-test.yml doesn't run on PRs, so the new route first runs after merge. Running it with workflow_dispatch beforehand would catch a missing role permission.
|
No action needed on the The job fails with |
|
Thanks for digging in and reproducing it! Warm stalls: It comes down to idle time between invokes. My script sent 10 back-to-back requests per target per round, so each function saw one invoke after ~1 s idle, then 9 in quick succession. The first one in each round never stalled (0/20), and the back-to-back ones stalled 61/90 and 70/90. Here's a small script that shows it through API Gateway (this PR's template, us-west-2, 128 MB, 50 invokes per gap, two runs per stack):
|
…odArn Allow every route in the stage of the calling API (<api-id>/<stage>/*/*) instead of reading a wildcard resource from ALLOWED_RESOURCE. This keeps the policy scoped to the calling API and removes the environment variable.
|
Updated based on your feedback @bnusunny, thanks for the review! |
📬 Issue #, if available: N/A
✍️ Description of changes:
On
main, a small streamed response can take 40 to 60 ms longer than a buffered response, even when the handler returns in microseconds. This occurs on the first invoke in each new execution environment. It also occurs on a warm invoke that arrives less than about 45 ms after the previous invoke finished. Warm invokes with more idle time between them do not stall.lambda_runtime_api_clientbuilds its connector withHttpConnector::new(), and hyper-util leavesTCP_NODELAYoff by default. A streaming response goes out as several small writes: the request head and metadata prelude, the 8-NUL delimiter, and the body chunks. With Nagle's algorithm on, the kernel holds a small write until the Runtime API acknowledges the earlier data. On a reused keep-alive connection, the Runtime API delays that ACK. Thus a small streamed response often waits for the delayed-ACK timer.This change enables
TCP_NODELAYon the default connector ofClientBuilderandPooledClientBuilder. The fix applies tolambda_runtime,lambda_http,lambda-extension, and the SnapStart pool reset. A connector passed towith_connectoris used as-is.The other Lambda runtime clients already send to the Runtime API with Nagle off:
CURLOPT_TCP_NODELAY. The Node.js and Python runtime interface clients use a bundled copy of it. The Node.js client streams responses through Node'shttpagent, which enablesnoDelayby default since Node.js 18.net/http. Go'snetpackage enablesTCP_NODELAYon every connection.The AWS SDK for Rust made the same change in
aws-smithy-http-client1.1.13 (changelog). Before that release, the SDK's default HTTPS client leftTCP_NODELAYoff, and the changelog gives the same reason for the change: "this can add roughly one RTT plus delayed-ACK time". reqwest enablesTCP_NODELAYby default since 0.10.5.Results for a small
StreamResponse(maincompared with this branch). The AWS Lambda rows are two runs of 100 sequential invokes for each target onprovided.al2023, and show theREPORTDuration. The Function URL rows and the API Gateway rows come from different deployments. For API Gateway, the requests tomainand to this branch are interleaved. The other rows are back-to-back invokes on loopback with a 28-byte body:mainp50In review, the first invoke in a new execution environment took 41.7 to 45.9 ms on
mainand 1.4 to 2.5 ms with this change (11 invokes each, us-west-2).Not every warm streamed invoke on
mainstalls. The API Gateway benchmark sent 10 rounds, and each round sent 10 requests back-to-back to each target. The first request of a round came after about 1 s of idle time, and it never stalled (0 of 20). Of the back-to-back requests, 61 and 70 of 90 stalled for 41 to 49 ms, and the others took about 1 ms. On the local mock, an invoke stalls when the idle time before it is less than about 45 ms, and it does not stall when the idle time is 60 ms or more. A second API Gateway test confirms this: onmain, back-to-back requests stalled 32 and 30 of 50 invokes, and requests 1 s apart stalled 0 of 50. Over all 100 invokes, the mean Duration dropped from 26.8 to 31.0 ms to 1.05 to 1.32 ms, a saving of 25 to 30 ms for each invoke. The client time to last byte (p50) of the streamed response dropped from 75.6 to 75.9 ms to 39.0 to 40.9 ms. That is the same as for the buffered route, within 2 ms. The buffered differences are in the run-to-run noise of a shared host. Onmain, a packet capture shows that the client holds the 52-byte tail (delimiter, body, terminator) until a pure ACK arrives 49 ms later. With this change, the segments go out about 15 µs apart.This PR also adds an integration test for response streaming. Before, the integration tests covered only buffered responses. The test calls a new
/streamroute (ResponseTransferMode: RESPONSE_STREAM) on the existing test API through the existing token authorizer. It makes sure that API Gateway returns the status, the headers, and the body that the function streams. The test checks behavior, not latency, so it also passes onmain. The unit tests guardTCP_NODELAY.<api-id>/<stage>/*/*, frommethodArn). Before, the policy allowed only the called method, and API Gateway caches the policy by token for 300 seconds. Thus the/streamrequest reused the/hellopolicy and got403.run-integration-test.ymlpasses the newStreamApiEndpointstack output to the test step. The test needs no region and no new secrets.Testing:
TcpStream::nodelay(). The default-connector test fails without the fix.cargo +nightly fmt --all -- --checkandcargo clippy --workspace --all-features -- -D warningspass.cargo test --all-featurespasses forlambda_runtime_api_client,lambda_runtime,lambda_http, andlambda-extensionon stable and on MSRV 1.84.0.cargo semver-checksreports that no semver update is required.make test-rie,make test-dockerized,make test-dockerized-concurrent, andexamples/check-examples.shpass.sam build --beta-featuresandsam deploy, then rancargo testas the workflow does: 3 passed, 0 failed, in two runs in a row. The authorizer cache had the default TTL, and the cache was empty before the first run. After the change to the authorizer (methodArn), I deployed again: 3 passed, 0 failed, on a stack with the client frommainand on a stack with this branch.401on both routes.methodArnsplit in the authorizer.sam validate --lintandcargo clippy -p lambda-integration-tests --all-targets -- -D warnings(with and without--all-features) pass.Note: on Rust 1.100.0, the
Cargo clippyjob fails withunreachable_codeatlambda-runtime/src/diagnostic.rs:89. The same failure occurs onmain, and this change does not cause it.🔏 By submitting this pull request
cargo +nightly fmt.cargo clippy --fix.