What is @smithy/middleware-retry?
@smithy/middleware-retry is a middleware package for the AWS SDK for JavaScript (v3) that provides retry functionality. It allows you to automatically retry failed requests based on customizable retry strategies, which can help improve the reliability and resilience of your applications.
What are @smithy/middleware-retry's main functionalities?
Standard Retry Strategy
The StandardRetryStrategy provides a default retry strategy that retries failed requests with exponential backoff and jitter. This is useful for handling transient errors and improving the reliability of your application.
const { StandardRetryStrategy } = require('@smithy/middleware-retry');
const retryStrategy = new StandardRetryStrategy();
// Use the retry strategy in a client configuration
const client = new SomeAWSClient({
region: 'us-west-2',
retryStrategy
});
Custom Retry Strategy
You can create a custom retry strategy by extending the RetryStrategy class and implementing your own logic for determining when to retry and how many attempts to make. This allows you to tailor the retry behavior to your specific needs.
const { RetryStrategy } = require('@smithy/middleware-retry');
class CustomRetryStrategy extends RetryStrategy {
shouldRetry(error) {
// Custom logic to determine if the request should be retried
return error.retryable;
}
getMaxAttempts() {
// Custom logic to determine the maximum number of retry attempts
return 5;
}
}
const customRetryStrategy = new CustomRetryStrategy();
// Use the custom retry strategy in a client configuration
const client = new SomeAWSClient({
region: 'us-west-2',
retryStrategy: customRetryStrategy
});
Adaptive Retry Strategy
The AdaptiveRetryStrategy dynamically adjusts the retry behavior based on the observed success and failure rates of requests. This can help optimize the retry behavior for different network conditions and improve overall performance.
const { AdaptiveRetryStrategy } = require('@smithy/middleware-retry');
const adaptiveRetryStrategy = new AdaptiveRetryStrategy();
// Use the adaptive retry strategy in a client configuration
const client = new SomeAWSClient({
region: 'us-west-2',
retryStrategy: adaptiveRetryStrategy
});
Other packages similar to @smithy/middleware-retry
axios-retry
axios-retry is a plugin for the Axios HTTP client that adds automatic retry functionality. It allows you to configure retry strategies, including exponential backoff and custom retry logic. Compared to @smithy/middleware-retry, axios-retry is designed specifically for use with Axios and may not offer the same level of integration with AWS SDK for JavaScript (v3).
retry
retry is a general-purpose retry library for JavaScript that provides a flexible API for implementing retry logic. It supports various retry strategies, including exponential backoff and custom retry logic. While it is not specifically designed for use with the AWS SDK, it can be used to implement retry functionality in a wide range of applications.
promise-retry
promise-retry is a library that provides retry functionality for promise-based operations. It allows you to configure retry strategies and handle transient errors in asynchronous code. Like retry, it is a general-purpose library and can be used in various contexts, but it does not offer the same level of integration with the AWS SDK as @smithy/middleware-retry.