AWS practice 14_advanced_patterns

Chapter 14: Advanced Architecture Patterns

     _       _                               _
    /    __| |_   ____ _ _ __   ___ ___  __| |
   / _  / _`   / / _` | '_  / __/ _ / _` |
  / ___  (_| | V / (_| | | | | (_|  __/ (_| |
 /_/   ___,_| _/ __,_|_| |_|______|__,_|
   ____       _   _
  |  _  __ _| |_| |_ ___ _ __ _ __  ___
  | |_) / _` | __| __/ _  '__| '_ / __|
  |  __/ (_| | |_| ||  __/ |  | | | __ 
  |_|   __,_|__|_____|_|  |_| |_|___/

14.1 Microservices Architecture

Microservices on AWS

┌──────────────────────────────────────────────────────────────────────┐
│                    Microservices Architecture                        │
├──────────────────────────────────────────────────────────────────────┤
│                                                                      │
│                         API Gateway                                  │
│                             │                                        │
│              ┌──────────────┼──────────────┐                        │
│              │              │              │                         │
│              ▼              ▼              ▼                         │
│         ┌────────┐    ┌────────┐    ┌────────┐                      │
│         │  User  │    │ Order  │    │Product │                      │
│         │Service │    │Service │    │Service │                      │
│         └────┬───┘    └────┬───┘    └────┬───┘                      │
│              │             │             │                           │
│              ▼             ▼             ▼                           │
│         ┌────────┐    ┌────────┐    ┌────────┐                      │
│         │DynamoDB│    │  RDS   │    │DynamoDB│                      │
│         └────────┘    └────────┘    └────────┘                      │
│                                                                      │
│  Compute Options:                                                    │
│  ├── ECS / Fargate: Container-based                                │
│  ├── EKS: Kubernetes orchestration                                 │
│  ├── Lambda: Serverless functions                                  │
│  └── App Runner: Simplified container deployment                   │
│                                                                      │
│  Service Communication:                                              │
│  ├── Synchronous: API Gateway, ALB, App Mesh                      │
│  └── Asynchronous: SQS, SNS, EventBridge                          │
│                                                                      │
└──────────────────────────────────────────────────────────────────────┘

Service Mesh (AWS App Mesh)

┌──────────────────────────────────────────────────────────────────────┐
│                      AWS App Mesh                                    │
├──────────────────────────────────────────────────────────────────────┤
│                                                                      │
│  ┌────────────────────────────────────────────────────────────────┐ │
│  │                        Service Mesh                             │ │
│  │                                                                 │ │
│  │  ┌──────────────────┐      ┌──────────────────┐               │ │
│  │  │   Service A      │      │   Service B      │               │ │
│  │  │  ┌────────────┐  │      │  ┌────────────┐  │               │ │
│  │  │  │    App     │  │      │  │    App     │  │               │ │
│  │  │  └──────┬─────┘  │      │  └──────┬─────┘  │               │ │
│  │  │         │        │      │         │        │               │ │
│  │  │  ┌──────▼─────┐  │      │  ┌──────▼─────┐  │               │ │
│  │  │  │Envoy Proxy │◀─┼──────┼─▶│Envoy Proxy │  │               │ │
│  │  │  └────────────┘  │      │  └────────────┘  │               │ │
│  │  └──────────────────┘      └──────────────────┘               │ │
│  │                                                                 │ │
│  │  Capabilities:                                                  │ │
│  │  ├── Traffic management (routing, retries)                    │ │
│  │  ├── Observability (metrics, traces, logs)                    │ │
│  │  ├── Security (mTLS)                                          │ │
│  │  └── Works with ECS, EKS, EC2                                 │ │
│  └────────────────────────────────────────────────────────────────┘ │
│                                                                      │
└──────────────────────────────────────────────────────────────────────┘

14.2 Event-Driven Architecture

Event-Driven Patterns

┌──────────────────────────────────────────────────────────────────────┐
│                  Event-Driven Architecture                           │
├──────────────────────────────────────────────────────────────────────┤
│                                                                      │
│  Pattern 1: Event Notification                                       │
│  ┌───────────┐    ┌───────────┐    ┌───────────┐                   │
│  │  Producer │───▶│EventBridge│───▶│  Consumer │                   │
│  │           │    │    Bus    │    │           │                   │
│  └───────────┘    └───────────┘    └───────────┘                   │
│                                                                      │
│  Pattern 2: Event Sourcing                                           │
│  ┌───────────┐    ┌───────────┐    ┌───────────┐                   │
│  │  Command  │───▶│  Event    │───▶│  Event    │                   │
│  │  Handler  │    │  Store    │    │  Processor│                   │
│  └───────────┘    │ (Kinesis) │    └───────────┘                   │
│                   └───────────┘                                     │
│                        │                                             │
│                        ▼                                             │
│                   ┌───────────┐                                     │
│                   │Read Model │                                     │
│                   │(DynamoDB) │                                     │
│                   └───────────┘                                     │
│                                                                      │
│  Pattern 3: CQRS (Command Query Responsibility Segregation)         │
│  ┌───────────┐         ┌───────────┐         ┌───────────┐        │
│  │  Command  │────────▶│  Write    │────────▶│  Event    │        │
│  │    API    │         │  Store    │         │  Stream   │        │
│  └───────────┘         │  (RDS)    │         │ (Kinesis) │        │
│                        └───────────┘         └─────┬─────┘        │
│                                                     │               │
│  ┌───────────┐         ┌───────────┐◀──────────────┘               │
│  │  Query    │◀────────│  Read     │                               │
│  │   API     │         │  Store    │                               │
│  └───────────┘         │(DynamoDB) │                               │
│                        └───────────┘                               │
│                                                                      │
└──────────────────────────────────────────────────────────────────────┘

Saga Pattern for Distributed Transactions

┌──────────────────────────────────────────────────────────────────────┐
│                       Saga Pattern                                   │
├──────────────────────────────────────────────────────────────────────┤
│                                                                      │
│  Orchestration-based (Step Functions)                               │
│  ┌────────────────────────────────────────────────────────────────┐ │
│  │                    Step Functions                               │ │
│  │                                                                 │ │
│  │  ┌────────┐   ┌────────┐   ┌────────┐   ┌────────┐            │ │
│  │  │Reserve │──▶│ Charge │──▶│Fulfill │──▶│Complete│            │ │
│  │  │Inventory│  │Payment │   │ Order  │   │ Order  │            │ │
│  │  └────┬───┘   └────┬───┘   └────┬───┘   └────────┘            │ │
│  │       │            │            │                               │ │
│  │       │ Failure    │ Failure    │ Failure                      │ │
│  │       ▼            ▼            ▼                               │ │
│  │  ┌────────┐   ┌────────┐   ┌────────┐                         │ │
│  │  │Release │   │ Refund │   │ Cancel │   (Compensating         │ │
│  │  │Inventory│   │Payment │   │Fulfill │    Transactions)       │ │
│  │  └────────┘   └────────┘   └────────┘                         │ │
│  └────────────────────────────────────────────────────────────────┘ │
│                                                                      │
│  Choreography-based (EventBridge/SNS)                               │
│  ┌────────────────────────────────────────────────────────────────┐ │
│  │                                                                 │ │
│  │  Service A ───event───▶ Service B ───event───▶ Service C      │ │
│  │      ▲                      │                      │           │ │
│  │      │                      │                      │           │ │
│  │      └───compensate─────────┴───────compensate─────┘           │ │
│  │                                                                 │ │
│  │  Each service listens for events and publishes results         │ │
│  │  Compensating actions triggered by failure events              │ │
│  └────────────────────────────────────────────────────────────────┘ │
│                                                                      │
└──────────────────────────────────────────────────────────────────────┘

14.3 Serverless Patterns

Serverless Web Application

┌──────────────────────────────────────────────────────────────────────┐
│                 Serverless Web Application                           │
├──────────────────────────────────────────────────────────────────────┤
│                                                                      │
│  ┌─────────────────────────────────────────────────────────────────┐│
│  │                                                                 ││
│  │  Users                                                          ││
│  │    │                                                            ││
│  │    ▼                                                            ││
│  │  ┌───────────────┐                                              ││
│  │  │  CloudFront   │  CDN                                         ││
│  │  └───────┬───────┘                                              ││
│  │          │                                                       ││
│  │    ┌─────┴─────┐                                                ││
│  │    │           │                                                 ││
│  │    ▼           ▼                                                 ││
│  │  ┌─────┐    ┌──────────┐    ┌────────┐    ┌──────────┐         ││
│  │  │ S3  │    │   API    │───▶│ Lambda │───▶│ DynamoDB │         ││
│  │  │(SPA)│    │ Gateway  │    │        │    │          │         ││
│  │  └─────┘    └──────────┘    └────────┘    └──────────┘         ││
│  │                  │                                               ││
│  │                  │                                               ││
│  │             ┌────▼────┐                                         ││
│  │             │ Cognito │  Authentication                         ││
│  │             └─────────┘                                         ││
│  │                                                                 ││
│  └─────────────────────────────────────────────────────────────────┘│
│                                                                      │
│  Benefits:                                                           │
│  ├── No server management                                          │
│  ├── Auto-scaling                                                  │
│  ├── Pay per use                                                   │
│  └── High availability built-in                                    │
│                                                                      │
└──────────────────────────────────────────────────────────────────────┘

Serverless Data Processing

┌──────────────────────────────────────────────────────────────────────┐
│               Serverless Data Processing                             │
├──────────────────────────────────────────────────────────────────────┤
│                                                                      │
│  Real-time Processing:                                               │
│  ┌────────────────────────────────────────────────────────────────┐ │
│  │  Data Sources                                                   │ │
│  │       │                                                         │ │
│  │       ▼                                                         │ │
│  │  ┌────────────┐    ┌────────────┐    ┌────────────┐            │ │
│  │  │ Kinesis    │───▶│  Lambda    │───▶│   S3 /     │            │ │
│  │  │ Data Stream│    │ (process)  │    │ DynamoDB   │            │ │
│  │  └────────────┘    └────────────┘    └────────────┘            │ │
│  └────────────────────────────────────────────────────────────────┘ │
│                                                                      │
│  Batch Processing:                                                   │
│  ┌────────────────────────────────────────────────────────────────┐ │
│  │                                                                 │ │
│  │  ┌────────┐    ┌────────────┐    ┌────────────┐    ┌────────┐ │ │
│  │  │   S3   │───▶│  Lambda    │───▶│   S3       │───▶│ Athena │ │ │
│  │  │ (raw)  │    │ (transform)│    │ (processed)│    │(query) │ │ │
│  │  └────────┘    └────────────┘    └────────────┘    └────────┘ │ │
│  │      │              │                                          │ │
│  │      └──────────────┘                                          │ │
│  │         S3 Event                                                │ │
│  └────────────────────────────────────────────────────────────────┘ │
│                                                                      │
│  ETL with Step Functions:                                            │
│  ┌────────────────────────────────────────────────────────────────┐ │
│  │                                                                 │ │
│  │  S3 ──▶ Lambda ──▶ Glue Job ──▶ Lambda ──▶ Redshift           │ │
│  │         (validate)  (transform)  (verify)   (load)             │ │
│  │                                                                 │ │
│  │  Orchestrated by Step Functions                                │ │
│  └────────────────────────────────────────────────────────────────┘ │
│                                                                      │
└──────────────────────────────────────────────────────────────────────┘

14.4 Data Lake Architecture

┌──────────────────────────────────────────────────────────────────────┐
│                    Data Lake Architecture                            │
├──────────────────────────────────────────────────────────────────────┤
│                                                                      │
│  ┌─────────────────────────────────────────────────────────────────┐│
│  │                        Data Lake                                ││
│  │                                                                 ││
│  │  Ingest          Store              Process          Consume   ││
│  │  ┌─────┐        ┌─────────────────────────┐        ┌─────────┐││
│  │  │Kinesis│──────▶│           S3            │◀──────│ Athena  │││
│  │  │Data   │      │                         │       │         │││
│  │  │Firehose│     │  ┌─────┐ ┌─────┐ ┌─────┐│       │Redshift │││
│  │  └─────┘        │  │Raw  │ │Stage│ │Curated││      │Spectrum │││
│  │                 │  │Zone │ │Zone │ │ Zone ││       │         │││
│  │  ┌─────┐        │  └─────┘ └─────┘ └─────┘│       │QuickSight│││
│  │  │ DMS │───────▶│                         │       └─────────┘││
│  │  │     │        │  Glue Data Catalog      │                  ││
│  │  └─────┘        │  (metadata/schema)      │                  ││
│  │                 └─────────────────────────┘                  ││
│  │  ┌─────┐                    │                                ││
│  │  │AWS   │                   ▼                                ││
│  │  │Glue  │           ┌─────────────┐                         ││
│  │  │      │◀──────────│  Glue ETL   │                         ││
│  │  └─────┘            │   Jobs      │                         ││
│  │                     └─────────────┘                         ││
│  │                                                              ││
│  │  Governance:                                                 ││
│  │  ├── Lake Formation (permissions, encryption)               ││
│  │  ├── Glue Data Catalog (metadata)                          ││
│  │  └── AWS DataBrew (data preparation)                       ││
│  └─────────────────────────────────────────────────────────────────┘│
│                                                                      │
└──────────────────────────────────────────────────────────────────────┘

14.5 Multi-Account Architecture

┌──────────────────────────────────────────────────────────────────────┐
│                Multi-Account Architecture                            │
├──────────────────────────────────────────────────────────────────────┤
│                                                                      │
│                    ┌─────────────────────────┐                      │
│                    │  Management Account     │                      │
│                    │  (Organizations)        │                      │
│                    └───────────┬─────────────┘                      │
│                                │                                     │
│        ┌───────────────────────┼───────────────────────┐            │
│        │                       │                       │            │
│   ┌────▼────┐            ┌─────▼────┐           ┌─────▼────┐       │
│   │Security │            │Shared    │           │ Workloads│       │
│   │   OU    │            │Services  │           │    OU    │       │
│   └────┬────┘            │   OU     │           └────┬─────┘       │
│        │                 └────┬─────┘                │             │
│   ┌────▼────┐           ┌─────▼─────┐          ┌─────▼─────┐       │
│   │ Log     │           │  Network  │          │   Prod    │       │
│   │ Archive │           │  (Transit │          │   OU      │       │
│   │ Account │           │  Gateway) │          │           │       │
│   └─────────┘           │           │          │ ┌───────┐ │       │
│   ┌─────────┐           │  Shared   │          │ │App A  │ │       │
│   │ Audit   │           │  DNS      │          │ │Account│ │       │
│   │ Account │           │           │          │ └───────┘ │       │
│   └─────────┘           │  Shared   │          │ ┌───────┐ │       │
│   ┌─────────┐           │  VPC      │          │ │App B  │ │       │
│   │ Security│           └───────────┘          │ │Account│ │       │
│   │ Tooling │                                  │ └───────┘ │       │
│   └─────────┘                                  └───────────┘       │
│                                                                      │
│  Key Components:                                                     │
│  ├── AWS Control Tower for landing zone                            │
│  ├── SCPs for guardrails                                           │
│  ├── Transit Gateway for networking                                │
│  ├── Centralized logging (CloudTrail, VPC Flow Logs)              │
│  └── IAM Identity Center for SSO                                   │
│                                                                      │
└──────────────────────────────────────────────────────────────────────┘

14.6 Resilient Architecture Patterns

Circuit Breaker Pattern

┌──────────────────────────────────────────────────────────────────────┐
│                    Circuit Breaker Pattern                           │
├──────────────────────────────────────────────────────────────────────┤
│                                                                      │
│  States:                                                             │
│  ┌────────────────────────────────────────────────────────────────┐ │
│  │                                                                 │ │
│  │   ┌──────────┐         ┌──────────┐         ┌──────────┐      │ │
│  │   │  Closed  │────────▶│   Open   │────────▶│Half-Open │      │ │
│  │   │(healthy) │ Failures│ (failing)│ Timeout │ (testing)│      │ │
│  │   └────▲─────┘         └──────────┘         └────┬─────┘      │ │
│  │        │                                          │            │ │
│  │        └──────────────────────────────────────────┘            │ │
│  │                        Success                                  │ │
│  └────────────────────────────────────────────────────────────────┘ │
│                                                                      │
│  Implementation with Lambda + Step Functions:                       │
│  ┌────────────────────────────────────────────────────────────────┐ │
│  │                                                                 │ │
│  │  Request ──▶ Check Circuit ──▶ If Open  ──▶ Return Fallback   │ │
│  │                   │                                             │ │
│  │                   ▼ If Closed                                   │ │
│  │              Call Service                                       │ │
│  │                   │                                             │ │
│  │              ┌────┴────┐                                        │ │
│  │              ▼         ▼                                        │ │
│  │          Success    Failure                                     │ │
│  │              │         │                                        │ │
│  │              ▼         ▼                                        │ │
│  │          Return    Increment Counter                           │ │
│  │          Result    (DynamoDB)                                  │ │
│  │                        │                                        │ │
│  │                        ▼                                        │ │
│  │                   If Threshold ──▶ Open Circuit                │ │
│  │                                                                 │ │
│  └────────────────────────────────────────────────────────────────┘ │
│                                                                      │
└──────────────────────────────────────────────────────────────────────┘

Bulkhead Pattern

┌──────────────────────────────────────────────────────────────────────┐
│                     Bulkhead Pattern                                 │
├──────────────────────────────────────────────────────────────────────┤
│                                                                      │
│  Isolate resources to prevent cascading failures                    │
│                                                                      │
│  ┌────────────────────────────────────────────────────────────────┐ │
│  │                                                                 │ │
│  │  ┌─────────────────┐   ┌─────────────────┐                     │ │
│  │  │  Service Pool A │   │  Service Pool B │                     │ │
│  │  │  (Critical)     │   │  (Normal)       │                     │ │
│  │  │                 │   │                 │                     │ │
│  │  │  ┌───┐ ┌───┐    │   │  ┌───┐ ┌───┐    │                     │ │
│  │  │  │ λ │ │ λ │    │   │  │ λ │ │ λ │    │                     │ │
│  │  │  └───┘ └───┘    │   │  └───┘ └───┘    │                     │ │
│  │  │                 │   │                 │                     │ │
│  │  │  Reserved       │   │  On-Demand      │                     │ │
│  │  │  Concurrency:50 │   │  Concurrency:   │                     │ │
│  │  │                 │   │  Unreserved     │                     │ │
│  │  └─────────────────┘   └─────────────────┘                     │ │
│  │                                                                 │ │
│  │  If Pool B overwhelmed, Pool A unaffected                      │ │
│  │                                                                 │ │
│  └────────────────────────────────────────────────────────────────┘ │
│                                                                      │
│  AWS Implementation:                                                 │
│  ├── Lambda Reserved Concurrency                                   │
│  ├── Separate Auto Scaling Groups                                  │
│  ├── Separate SQS queues per priority                             │
│  └── Separate databases per service                                │
│                                                                      │
└──────────────────────────────────────────────────────────────────────┘

14.7 Caching Strategies

┌──────────────────────────────────────────────────────────────────────┐
│                    Caching Strategies                                │
├──────────────────────────────────────────────────────────────────────┤
│                                                                      │
│  Cache-Aside (Lazy Loading):                                        │
│  ┌─────────────────────────────────────────────────────────────┐   │
│  │  1. Check cache                                              │   │
│  │  2. If miss, query database                                  │   │
│  │  3. Store in cache                                           │   │
│  │  4. Return data                                              │   │
│  │                                                              │   │
│  │  App ──▶ Cache (miss) ──▶ Database                          │   │
│  │    ◀─────────────────────────┘                               │   │
│  │           + Write to cache                                   │   │
│  └─────────────────────────────────────────────────────────────┘   │
│                                                                      │
│  Write-Through:                                                      │
│  ┌─────────────────────────────────────────────────────────────┐   │
│  │  1. Write to cache and database simultaneously              │   │
│  │  2. Always consistent                                        │   │
│  │                                                              │   │
│  │  App ──▶ Cache ──▶ Database                                 │   │
│  │         (write)   (write)                                    │   │
│  └─────────────────────────────────────────────────────────────┘   │
│                                                                      │
│  Write-Behind (Write-Back):                                         │
│  ┌─────────────────────────────────────────────────────────────┐   │
│  │  1. Write to cache                                          │   │
│  │  2. Async write to database                                  │   │
│  │                                                              │   │
│  │  App ──▶ Cache ──async──▶ Database                          │   │
│  │         (write)   (batch)                                    │   │
│  └─────────────────────────────────────────────────────────────┘   │
│                                                                      │
│  AWS Cache Options:                                                  │
│  ├── CloudFront (edge caching)                                     │
│  ├── ElastiCache Redis/Memcached (in-memory)                      │
│  ├── DAX (DynamoDB caching)                                        │
│  └── API Gateway caching                                           │
│                                                                      │
└──────────────────────────────────────────────────────────────────────┘

14.8 Pattern Selection Guide

┌────────────────────────────────────────────────────────────────────────┐
│                    Pattern Selection Guide                             │
├────────────────────────────────────────────────────────────────────────┤
│                                                                        │
│  Requirement                    │  Pattern / Approach                  │
│  ──────────────────────────────│─────────────────────────────────────│
│  Distributed transactions       │  Saga (Step Functions)               │
│  Service-to-service resilience │  Circuit Breaker                     │
│  Resource isolation             │  Bulkhead                            │
│  Read scaling                   │  CQRS + Read Replicas               │
│  Audit trail / Time travel     │  Event Sourcing                      │
│  Loose coupling                 │  Event-Driven + SQS/SNS             │
│  Complex workflows              │  Step Functions                      │
│  Service communication          │  App Mesh / API Gateway             │
│  Data analytics                 │  Data Lake on S3                    │
│  Multi-account governance       │  Control Tower + Organizations      │
│                                                                        │
└────────────────────────────────────────────────────────────────────────┘

Key Takeaways

┌─────────────────────────────────────────────────────────────────────┐
│  1. Microservices: ECS/EKS + API Gateway + App Mesh               │
│  2. Event-driven: EventBridge for routing, Kinesis for streaming  │
│  3. Saga pattern with Step Functions for distributed transactions │
│  4. Serverless: Lambda + API Gateway + DynamoDB                   │
│  5. Data Lake: S3 + Glue + Lake Formation + Athena               │
│  6. Multi-account: Control Tower + Organizations + Transit GW    │
│  7. Circuit breaker and bulkhead for resilience                  │
│  8. Cache-aside for read-heavy workloads                         │
│  9. CQRS for separate read/write scaling                         │
│  10. Choose patterns based on requirements, not trends           │
└─────────────────────────────────────────────────────────────────────┘

Congratulations! You’ve completed the AWS SA Professional study materials.

Back to README