maxNetworkRetries setting does not work
Problem
environment: node v10.x on AWS Lambda package version `"stripe": "^8.84.0"` we have the following code in our checkout lambda function, as suggested in official docs https://www.npmjs.com/package/stripe#network-retries [code block] in one of executions it thrown exception [code block] we expected request would be retried at least 3 times, but it did not, cause Lambda function duration was ~5000ms <img width="969" alt="_scr_ 2020-10-28 at 14 34 44" src="https://user-images.githubusercontent.com/155563/97489568-15d2d880-1936-11eb-95f3-344a0db83723.png">
Unverified for your environment
Select your OS to check compatibility.
1 Fix
Increase Lambda Timeout for Stripe Network Retries
The maxNetworkRetries setting in the Stripe SDK is designed to retry failed requests based on network issues. However, if the Lambda function's execution time exceeds the timeout limit (default is 3 seconds), the function will terminate before the retries can occur. In this case, the Lambda function duration was ~5000ms, which indicates that the retries did not have enough time to execute before the function timed out.
Awaiting Verification
Be the first to verify this fix
- 1
Increase Lambda Timeout
Modify the AWS Lambda function configuration to increase the timeout setting to allow for sufficient time for network retries to occur. Set the timeout to a value greater than the expected maximum execution time, such as 10 seconds.
bashaws lambda update-function-configuration --function-name YourFunctionName --timeout 10 - 2
Verify maxNetworkRetries Setting
Ensure that the maxNetworkRetries setting is correctly configured in your Stripe client initialization. This setting should be set to at least 3 to allow for multiple retries on network errors.
typescriptconst stripe = require('stripe')('your_secret_key', { maxNetworkRetries: 3 }); - 3
Implement Error Logging
Add error logging to capture and log any errors that occur during the Stripe API calls. This will help in diagnosing whether the retries are being attempted and if they are failing.
typescripttry { await stripe.charges.create({ ... }); } catch (error) { console.error('Stripe API error:', error); } - 4
Test the Configuration
Deploy the updated Lambda function and run tests to confirm that the function can handle network errors and that retries are occurring as expected. Monitor the logs for any errors and ensure that the function completes successfully.
Validation
To confirm the fix worked, invoke the Lambda function and simulate a network error (e.g., by temporarily blocking internet access). Check the logs to ensure that the function retries the request at least 3 times before failing. Verify that the execution time does not exceed the new timeout setting.
Sign in to verify this fix
Environment
Submitted by
Alex Chen
2450 rep