4.9 KiB
Task 3.1 Implementation: DynamoDB Storage Service Wrapper
Overview
Enhanced the existing DynamoDB client from Task 2.1 with retry logic, connection pooling, transaction support, and batch operations.
Implementation Details
1. Enhanced Client Configuration
- Retry Strategy: Configured with exponential backoff and jitter
- Max attempts: 5
- Max backoff: 20 seconds
- Prevents thundering herd with jitter
- Connection Pooling: Uses AWS SDK's default HTTP client with built-in connection pooling
2. Transaction Support
Implemented TransactWriteItems method for ACID transactions:
- Supports multiple write operations in a single atomic transaction
- Automatic retry on transient failures
- Proper error handling and wrapping
3. Batch Operations
Implemented two batch operation methods:
BatchGetItems
- Retrieves multiple items in a single request
- Automatically retries unprocessed keys with exponential backoff
- Merges results from retry attempts
- Max 5 retry attempts with increasing backoff (100ms → 20s)
BatchWriteItems
- Writes multiple items in a single request
- Automatically retries unprocessed items with exponential backoff
- Max 5 retry attempts with increasing backoff (100ms → 20s)
4. Standard Operations
Wrapped standard DynamoDB operations with automatic retry:
PutItem- Put a single itemGetItem- Get a single itemUpdateItem- Update a single itemDeleteItem- Delete a single itemQuery- Query items
All operations include proper error handling and wrapping.
5. Comprehensive Testing
Created dynamodb_test.go with tests for:
- Client initialization
- Transaction operations
- Batch get operations
- Batch write operations
- Put and get operations
- Update operations
- Delete operations
- Query operations
Tests include:
- Automatic skip when DynamoDB is not available
- Helper function for test setup with dummy AWS credentials
- Table creation and cleanup helpers
- Verification of all operations
Files Modified/Created
Modified
internal/storage/dynamodb.go- Enhanced with retry logic, transactions, and batch operations
Created
internal/storage/dynamodb_test.go- Comprehensive test suiteinternal/storage/README.md- Documentation for the storage servicedocs/task-3.1-implementation.md- This implementation document
Requirements Addressed
✅ Requirement 8.1: Immediate persistence with reliable operations ✅ Requirement 8.8: Efficient scaling through batch operations and connection pooling ✅ Design: Retry logic with exponential backoff ✅ Design: Transaction support (TransactWrite) ✅ Design: Batch operations (BatchGet, BatchWrite) ✅ Design: Connection pooling
Testing
Running Tests
# Start DynamoDB Local
docker-compose up -d
# Run all storage tests
go test -v ./internal/storage
# Run specific tests
go test -v ./internal/storage -run TestTransactWriteItems
Test Coverage
- ✅ Client initialization with retry configuration
- ✅ Transaction writes with multiple items
- ✅ Batch get with multiple items
- ✅ Batch write with multiple items
- ✅ Single item operations (Put, Get, Update, Delete)
- ✅ Query operations
- ✅ Graceful skip when DynamoDB unavailable
Usage Example
// Create client
ctx := context.Background()
client, err := storage.NewDynamoDBClient(ctx, "http://localhost:8000")
if err != nil {
log.Fatal(err)
}
// Transaction example
err = client.TransactWriteItems(ctx, &dynamodb.TransactWriteItemsInput{
TransactItems: []types.TransactWriteItem{
{
Put: &types.Put{
TableName: aws.String("MyTable"),
Item: map[string]types.AttributeValue{
"id": &types.AttributeValueMemberS{Value: "item1"},
},
},
},
},
})
// Batch write example
err = client.BatchWriteItems(ctx, &dynamodb.BatchWriteItemInput{
RequestItems: map[string][]types.WriteRequest{
"MyTable": {
{
PutRequest: &types.PutRequest{
Item: map[string]types.AttributeValue{
"id": &types.AttributeValueMemberS{Value: "item1"},
},
},
},
},
},
})
// Batch get example
output, err := client.BatchGetItems(ctx, &dynamodb.BatchGetItemInput{
RequestItems: map[string]types.KeysAndAttributes{
"MyTable": {
Keys: []map[string]types.AttributeValue{
{"id": &types.AttributeValueMemberS{Value: "item1"}},
},
},
},
})
Next Steps
This enhanced storage service is now ready to be used by:
- Task 3.2: Data model implementations
- Task 3.3: Table schema creation
- All future tasks requiring DynamoDB operations
The retry logic, transaction support, and batch operations provide a solid foundation for building scalable, reliable data access patterns.