1.1.4 Synchronous vs Asynchronous

Synchronous vs Asynchronous Patterns

Synchronous

  • Caller chờ response trước khi tiếp tục
  • API Gateway → Lambda (sync invocation)
  • Ưu: Đơn giản, immediate response
  • Nhược: Blocking, timeout issues

Asynchronous

  • Caller gửi request và tiếp tục, không chờ response
  • S3 event → Lambda (async invocation)
  • SQS → Lambda (event source mapping)
  • Ưu: Decoupled, resilient, scalable
  • Nhược: Phức tạp hơn, eventual consistency

Lambda Invocation Types

TypeTrigger ExamplesRetryError Handling
SynchronousAPI Gateway, ALB, CloudFrontCaller retryCaller nhận error
AsynchronousS3, SNS, EventBridge2 retries (tổng 3)DLQ / Destinations
Event Source MappingSQS, Kinesis, DynamoDB StreamsUntil success hoặc expiryBisect batch, DLQ

Quan trọng: SQS + Lambda: Visibility timeout phải > 6x function timeout. Nếu không, message sẽ bị xử lý lại trước khi Lambda hoàn thành.