Skip to content

Commit 5207953

Browse files
committed
Enhancement: Adding AdaptiveRetry Samples for Java SDK v2, DynamoDB, #7554
1 parent 708c376 commit 5207953

File tree

9 files changed

+2096
-1
lines changed

9 files changed

+2096
-1
lines changed

javav2/example_code/dynamodb/README.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,18 @@ Code excerpts that show you how to call individual service functions.
5858
- [Scan](src/main/java/com/example/dynamodb/DynamoDBScanItems.java#L6)
5959
- [UpdateItem](src/main/java/com/example/dynamodb/UpdateItem.java#L6)
6060

61+
### Retry Strategies
62+
63+
Code examples that show you how to configure and use different retry strategies for improved resilience.
64+
65+
- [Basic Adaptive Retry Implementation](src/main/java/com/example/dynamodb/BasicAdaptiveRetryImplementation.java) - Shows how to configure DynamoDB clients with AdaptiveRetryStrategy for better performance and resilience
66+
- [Migration Examples](src/main/java/com/example/dynamodb/MigrationExamples.java) - Demonstrates migrating from StandardRetryStrategy to AdaptiveRetryStrategy with before/after comparisons
67+
68+
For comprehensive documentation on adaptive retry strategies, see the [adaptive_retry_docs](adaptive_retry_docs/) directory which includes:
69+
- [Adaptive Retry Concepts](adaptive_retry_docs/concepts.md)
70+
- [Migration Guide](adaptive_retry_docs/migration-guide.md)
71+
- [Configuration Reference](adaptive_retry_docs/configuration-reference.md)
72+
6173
### Scenarios
6274

6375
Code examples that show you how to accomplish a specific task by calling multiple
@@ -160,4 +172,4 @@ in the `javav2` folder.
160172

161173
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
162174

163-
SPDX-License-Identifier: Apache-2.0
175+
SPDX-License-Identifier: Apache-2.0
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# AWS Java SDK AdaptiveRetryStrategy Guide
2+
3+
This guide provides comprehensive documentation and examples for implementing AWS Java SDK's `AdaptiveRetryStrategy` with AWS service clients.
4+
5+
> **⚠️ Important**: AdaptiveRetryStrategy is designed for specialized use cases with high resource constraints and single-resource clients. AWS recommends StandardRetryStrategy for most applications.
6+
7+
## Documentation Structure
8+
9+
| Document | Purpose | When to Use |
10+
|----------|---------|-------------|
11+
| **[Concepts](concepts.md)** | Conceptual foundation and theory | Understanding when/why to use AdaptiveRetryStrategy |
12+
| **[Migration Guide](migration-guide.md)** | Step-by-step migration instructions | Converting from older retry policies |
13+
| **[Configuration Reference](configuration-reference.md)** | Complete parameter reference | Fine-tuning and optimization |
14+
15+
## Getting Started
16+
17+
1. **Quick Start**: Check the `examples/` directory for ready-to-use code
18+
2. **Learn Concepts**: Read [Concepts](concepts.md) to understand when AdaptiveRetryStrategy is appropriate
19+
3. **Migration**: Use [Migration Guide](migration-guide.md) if converting existing code
20+
4. **Fine-tuning**: Consult [Configuration Reference](configuration-reference.md) for parameter optimization
21+
22+
## Documentation Sources
23+
24+
This guide is based on official AWS SDK for Java 2.x documentation and follows AWS best practices as documented in:
25+
26+
- [AWS SDK for Java 2.x Developer Guide - Configure retry behavior](https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/retry-strategy.html)
27+
- [AWS SDK for Java 2.x API Reference](https://sdk.amazonaws.com/java/api/latest/software/amazon/awssdk/retries/package-summary.html)
28+
29+
All examples and recommendations align with AWS's official guidance on retry strategy implementation.
Lines changed: 232 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,232 @@
1+
# Adaptive Retry Strategy Concepts
2+
3+
## Overview
4+
5+
AWS Java SDK's `AdaptiveRetryStrategy` is a specialized retry strategy designed for use cases with high resource constraints. It includes all features of the standard retry strategy plus a client-side rate limiter that measures throttled vs non-throttled requests to minimize throttling errors.
6+
7+
> **⚠️ Important**: AdaptiveRetryStrategy assumes the client works against a single resource (e.g., one DynamoDB table or one S3 bucket). AWS recommends StandardRetryStrategy for most use cases.
8+
9+
## Standard Retry vs Adaptive Retry
10+
11+
### Standard Retry Policies
12+
13+
Traditional retry policies use fixed algorithms with predetermined backoff strategies:
14+
15+
- **Fixed Backoff**: Same delay between all retry attempts
16+
- **Exponential Backoff**: Exponentially increasing delays (2^attempt * base_delay)
17+
- **Linear Backoff**: Linearly increasing delays (attempt * base_delay)
18+
19+
**Limitations:**
20+
- Cannot adapt to real-time service conditions
21+
- May be too aggressive during service degradation
22+
- May be too conservative during normal operations
23+
- Fixed retry counts regardless of error type
24+
25+
### Adaptive Retry Strategy
26+
27+
Adaptive retry includes all features of the standard strategy and adds:
28+
29+
- **Client-Side Rate Limiter**: Measures the rate of throttled requests compared to non-throttled requests
30+
- **Dynamic Request Rate**: Slows down requests to stay within safe bandwidth
31+
- **Load-Based Adaptation**: Uses dynamic backoff delay based on current load against downstream resources
32+
- **Circuit Breaking**: May prevent second attempts in outage scenarios to protect downstream services
33+
34+
**Benefits:**
35+
- **Throttling Prevention**: Attempts to cause zero throttling errors through rate limiting
36+
- **Resource Protection**: Protects downstream services from retry storms
37+
- **Load Adaptation**: Adjusts to real-time service conditions and traffic patterns
38+
- **Bandwidth Optimization**: Adjusts request rate to minimize throttling
39+
40+
## How Adaptive Retry Works
41+
42+
### 1. Client-Side Rate Limiting
43+
- Implements a token bucket mechanism to control request rates
44+
- Measures throttled vs non-throttled request ratios
45+
- Reduces request rate when throttling is detected
46+
- May delay initial attempts in high-traffic scenarios
47+
48+
### 2. Dynamic Backoff Strategy
49+
- Uses dynamic backoff delay based on current load against downstream resources
50+
- Adapts in real-time to changing service conditions and traffic patterns
51+
- Different from standard exponential backoff - timing adjusts based on service load
52+
53+
### 3. Circuit Breaking Protection
54+
- Performs circuit breaking when high downstream failures are detected
55+
- May prevent second attempts during outage scenarios
56+
- Designed to protect downstream services from retry storms
57+
- First attempt is always executed, only retries may be disabled
58+
59+
## When to Use Adaptive Retry
60+
61+
### Appropriate Use Cases
62+
- **High resource constraint environments** where minimizing throttling is critical
63+
- **Single-resource applications** (one DynamoDB table, one S3 bucket per client)
64+
- **Applications experiencing frequent throttling** with standard retry strategies
65+
- **Environments where all clients use adaptive retry** against the same resource
66+
67+
### Use Standard Retry Instead When
68+
- **Multi-resource clients** (one client accessing multiple tables/buckets)
69+
- **General use cases** - AWS recommends StandardRetryStrategy for most applications
70+
- **Mixed client environments** where not all clients use adaptive retry
71+
- **Applications requiring predictable retry timing**
72+
73+
### Critical Limitations
74+
⚠️ **Single Resource Assumption**: If you use a single client for multiple resources, throttling or outages with one resource will cause increased latency and failures for all other resources accessed by that client.
75+
76+
## Key Concepts
77+
78+
### Client-Side Rate Limiting
79+
Adaptive retry includes built-in client-side rate limiting to prevent overwhelming services during degradation.
80+
81+
### Token Bucket Mechanism
82+
Each client maintains a token bucket that provides a mechanism to stop retries when a large percentage of requests are failing and retries are unsuccessful.
83+
84+
### Rate Measurement
85+
The strategy measures the rate of throttled requests compared to non-throttled requests to determine when to slow down request rates.
86+
87+
### Error Classification
88+
Different retry strategies for different error types:
89+
- **Throttling errors**: Aggressive backoff with rate limiting
90+
- **Server errors**: Standard exponential backoff
91+
- **Client errors**: Minimal or no retries
92+
93+
## Performance Implications
94+
95+
### Latency
96+
- **Improved**: Enhanced recovery from transient issues
97+
- **Adaptive**: Longer delays during service degradation (by design)
98+
99+
### Throughput
100+
- **Higher**: Better success rates through intelligent retry timing
101+
- **Stable**: Maintains throughput during service stress
102+
103+
### Resource Usage
104+
- **Optimized**: Reduces unnecessary retry attempts
105+
- **Efficient**: Better CPU and network utilization
106+
107+
## Testing and Validation
108+
109+
### Compilation Testing
110+
Always test that your AdaptiveRetryStrategy configuration compiles correctly:
111+
112+
```java
113+
@Test
114+
public void testAdaptiveRetryStrategyCompilation() {
115+
// Test basic configuration
116+
AdaptiveRetryStrategy strategy = AdaptiveRetryStrategy.builder()
117+
.maxAttempts(3)
118+
.build();
119+
120+
DynamoDbClient client = DynamoDbClient.builder()
121+
.overrideConfiguration(ClientOverrideConfiguration.builder()
122+
.retryStrategy(strategy)
123+
.build())
124+
.build();
125+
126+
assertNotNull(client);
127+
client.close();
128+
}
129+
130+
@Test
131+
public void testRetryModeConfiguration() {
132+
// Test RetryMode approach
133+
DynamoDbClient client = DynamoDbClient.builder()
134+
.overrideConfiguration(ClientOverrideConfiguration.builder()
135+
.retryStrategy(software.amazon.awssdk.core.retry.RetryMode.ADAPTIVE)
136+
.build())
137+
.build();
138+
139+
assertNotNull(client);
140+
client.close();
141+
}
142+
```
143+
144+
### Runtime Validation
145+
Monitor retry behavior in your application logs to ensure the adaptive strategy is working as expected:
146+
147+
- Watch for adaptive backoff patterns
148+
- Monitor throttling error rates
149+
- Observe request rate adjustments during load spikes
150+
151+
## Implementation Details
152+
153+
### Correct API Usage in AWS SDK v2
154+
155+
When implementing AdaptiveRetryStrategy, use the correct AWS SDK v2 API:
156+
157+
```java
158+
// CORRECT - Basic AdaptiveRetryStrategy configuration
159+
AdaptiveRetryStrategy adaptiveRetryStrategy = AdaptiveRetryStrategy.builder()
160+
.maxAttempts(3)
161+
.backoffStrategy(BackoffStrategy.exponentialDelay(
162+
Duration.ofMillis(100), // base delay
163+
Duration.ofSeconds(20) // max delay
164+
))
165+
.throttlingBackoffStrategy(BackoffStrategy.exponentialDelay(
166+
Duration.ofSeconds(1), // base delay for throttling
167+
Duration.ofSeconds(20) // max delay for throttling
168+
))
169+
// Note: circuitBreakerEnabled() method doesn't exist - circuit breaking is built-in
170+
.build();
171+
172+
DynamoDbClient client = DynamoDbClient.builder()
173+
.overrideConfiguration(ClientOverrideConfiguration.builder()
174+
.retryStrategy(adaptiveRetryStrategy) // Use retryStrategy(), not retryPolicy()
175+
.build())
176+
.build();
177+
```
178+
179+
### Simplest Configuration Approach
180+
181+
For basic adaptive retry behavior, use RetryMode:
182+
183+
```java
184+
// CORRECT - Simplest adaptive retry configuration
185+
DynamoDbClient client = DynamoDbClient.builder()
186+
.overrideConfiguration(ClientOverrideConfiguration.builder()
187+
.retryStrategy(software.amazon.awssdk.core.retry.RetryMode.ADAPTIVE)
188+
.build())
189+
.build();
190+
```
191+
192+
### Key API Differences from Standard Retry
193+
194+
- **Package**: `software.amazon.awssdk.retries.AdaptiveRetryStrategy` (not `core.retry`)
195+
- **Builder**: `AdaptiveRetryStrategy.builder()` (not `RetryPolicy.builder()`)
196+
- **Configuration**: `.retryStrategy()` (not `.retryPolicy()`)
197+
- **Circuit Breaking**: Built-in (no `.circuitBreakerEnabled()` method)
198+
199+
## Next Steps
200+
201+
Now that you understand the concepts, proceed to:
202+
1. [Migration Guide](migration-guide.md) - Convert existing retry policies with correct API usage
203+
2. [Configuration Reference](configuration-reference.md) - Detailed parameter documentation
204+
3. [Examples](../examples/) - Working, tested code implementations
205+
206+
## Sources and References
207+
208+
This conceptual guide is based on official AWS SDK for Java 2.x documentation:
209+
210+
### Primary Sources
211+
212+
1. **AWS SDK for Java 2.x Developer Guide - Configure retry behavior**
213+
*AWS Documentation*
214+
https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/retry-strategy.html
215+
Retrieved: August 18, 2025
216+
217+
2. **AWS SDK for Java 2.x API Reference - AdaptiveRetryStrategy**
218+
*AWS SDK API Documentation*
219+
https://sdk.amazonaws.com/java/api/latest/software/amazon/awssdk/retries/AdaptiveRetryStrategy.html
220+
Retrieved: August 18, 2025
221+
222+
### Key Technical Details
223+
224+
- **Retry Strategy Introduction**: Retry strategies were introduced in AWS SDK for Java 2.x version 2.26.0 as part of the AWS-wide effort to unify interfaces and behavior¹
225+
- **Adaptive Strategy Purpose**: Designed specifically for "use cases with a high level of resource constraints"¹
226+
- **Rate Limiting**: "Includes all the features of the standard strategy and adds a client-side rate limiter that measures the rate of throttled requests compared to non-throttled requests"¹
227+
- **Single Resource Assumption**: "The adaptive retry strategy assumes that the client works against a single resource (for example, one DynamoDB table or one Amazon S3 bucket)"¹
228+
- **AWS Recommendation**: StandardRetryStrategy is "the recommended RetryStrategy implementation for normal use cases" and "generally useful across all retry use cases" unlike AdaptiveRetryStrategy¹
229+
230+
---
231+
**Citations:**
232+
1. AWS SDK for Java 2.x Developer Guide. "Configure retry behavior in the AWS SDK for Java 2.x." Amazon Web Services. https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/retry-strategy.html

0 commit comments

Comments
 (0)