Design an active-active architecture to handle concurrent DynamoDB Global Table transactions with conflict resolution and idempotency.
- Objective
- Architecture Overview
- Class Definitions
- Interfaces
- AWS Integration
- Telecom Standards & Justifications
- Unit Tests
- Running the Tests
-
AWS Serverless Components:
- DynamoDB Global Tables (Regions 1 and 2) for cross-region replication.
- Lambda Functions (Python/Node.js) in each region to process events.
- Amazon SQS (Dead Letter Queue) for failed event handling.
- Step Functions for idempotent workflow orchestration.
-
Conflict Resolution Strategy:
- Use conditional writes and last-write-wins with client-side timestamps.
- AWS AppSync (optional) for real-time conflict detection.
-
Diagram
class EventProcessor:
def __init__(self, dynamo_table, region):
self.dynamo = dynamo_table
self.region = region
def process_event(self, event):
# Apply idempotency key check
if self._is_duplicate(event.idempotency_key):
return {"status": "duplicate"}
# Conditional update with versioning
return self.dynamo.update_item(...)
class ConflictResolver:
def resolve(self, item_region1, item_region2):
# Compare timestamps and merge data
return merged_item- DynamoDB Table Schema:
{ "Id": "string", "Data": "string", "Version": "number", "LastUpdatedRegion": "string", "Timestamp": "number", "IdempotencyKey": "string" } - Lambda Handler:
def lambda_handler(event, context): processor = EventProcessor(dynamo_table, current_region) return processor.process_event(event)
-
Serverless Framework Template:
resources: DynamoTable: Type: AWS::DynamoDB::Table Properties: TableName: GlobalEventTable Replicas: - Region: us-east-1 - Region: eu-west-1
CloudFormation Template Excerpt:
GlobalEventTable: Type: AWS::DynamoDB::GlobalTable Properties: TableName: GlobalEventTable BillingMode: PAY_PER_REQUEST AttributeDefinitions: - AttributeName: Id AttributeType: S - AttributeName: IdempotencyKey AttributeType: S KeySchema: - AttributeName: Id KeyType: HASH GlobalSecondaryIndexes: - IndexName: IdempotencyIndex KeySchema: - AttributeName: IdempotencyKey KeyType: HASH Projection: ProjectionType: ALL Replicas: - Region: us-east-1 PointInTimeRecoverySpecification: PointInTimeRecoveryEnabled: true - Region: eu-west-1 PointInTimeRecoverySpecification: PointInTimeRecoveryEnabled: true EventProcessorLambda: Type: AWS::Serverless::Function Properties: CodeUri: ../src/task1/ Handler: event_processor.lambda_handler Runtime: python3.9 Timeout: 30 Policies: - DynamoDBCrudPolicy: TableName: !Ref GlobalEventTable
- TM Forum Open API Standards: Used for event schema design.
- AWS Well-Architected Framework: Ensures scalability and cost optimization.
- Test Case 1: Simulate concurrent updates in two regions and validate conflict resolution.
- Test Case 2: Validate idempotency using repeated idempotency keys.
To see the geo-distributed data architecture in action, follow these steps:
# Install required dependencies
pip install diagrams pytestThe visualizer demonstrates a complete flow of events across regions, including conflict resolution and idempotency:
# From the project root directory
python src/task1/visualize_flow.pyThis will output a step-by-step simulation showing:
- Events being processed in different regions
- Global table replication
- Conflict resolution in action
- Idempotency key handling
Unit tests verify the core functionality:
# From the project root directory
pytest src/task1/tests/test_event_processor.py -vTo generate the architecture diagrams:
# From the project root directory
python src/task1/diagrams/sequence_diagram.pyThis will create a diagram file in the current directory showing the sequence of operations across regions.
