AWS
SNS
Lambda
SQS
Cloud Architecture

SNS to Lambda vs SNS to SQS to Lambda

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Introduction

In the realm of serverless architecture, AWS offers powerful tools for building event-driven applications. Two common patterns involve Amazon Simple Notification Service (SNS), Amazon Simple Queue Service (SQS), and AWS Lambda, each having its own strengths and potential use cases. This article delves into the distinctions between the SNS to Lambda pattern and the SNS to SQS to Lambda pattern, providing technical insight and examples to clarify each approach's merits.

SNS to Lambda

Overview

Amazon SNS to AWS Lambda is a widely used pattern that leverages SNS's pub/sub capabilities to trigger Lambda functions directly. This setup is suitable for real-time event processing, where the immediate execution of the Lambda function is necessary upon message publication to the SNS topic.

How It Works

  1. SNS Topic Creation: An SNS topic is created, serving as a conduit for messages.
  2. Lambda Subscription: The Lambda function subscribes directly to the SNS topic.
  3. Message Publication: A message is published to the SNS topic.
  4. Direct Invocation: SNS directly invokes the Lambda function with the message payload.

Technical Considerations

  • Latency: Direct invocation results in minimal latency, making it ideal for time-sensitive operations.
  • Scalability: SNS can handle a vast number of messages, but combining it directly with Lambda necessitates careful management of Lambda's concurrent executions.
  • Error Handling: If the Lambda function encounters an error, retry behavior is managed through the function's error-handling configurations.

Use Cases

  • Real-time notifications (e.g., alerts, updates)
  • Stateless real-time data processing
  • Triggering notification-based workflows

SNS to SQS to Lambda

Overview

An alternative pattern involves adding an SQS queue between SNS and Lambda. This approach can enhance reliability and fault tolerance by decoupling the message sources from the Lambda consumers.

How It Works

  1. SNS Topic Creation: As before, an SNS topic is established for message dissemination.
  2. SQS Queue Subscription: Instead of a direct connection, an SQS queue subscribes to the SNS topic.
  3. Message Publication: A message is published to the SNS topic and subsequently delivered to the SQS queue.
  4. Batch Processing by Lambda: The Lambda function polls the SQS queue and processes messages in batches.

Technical Considerations

  • Decoupling: SQS acts as a buffering layer, making the system more resilient to transient failures.
  • Batch Processing: Reduces the load on Lambda by allowing batch message processing. This can reduce costs and optimize throughput.
  • Guarantees: SQS offers more control over message delivery guarantees, such as FIFO (First-In-First-Out) and exactly-once processing with Lambda.

Use Cases

  • Processing high-volume, bursty workloads
  • Workflows requiring higher message durability and reliability
  • Scenarios needing message ordering or duplicate message handling

Comparison Table

FeatureSNS to LambdaSNS to SQS to Lambda
LatencyLowModerate (additional queuing delay)
Fault ToleranceModerate (dependent on retry settings)High (SQS provides buffering and retries)
CostPotentially higher with high concurrencyCost-effective with batch processing
ScalabilityHigh, but tightly linked to Lambda limitsVery high, with independent scaling
DurabilityLower, as messages aren't stored after Lambda receives themEnhanced with SQS's persistence
Message OrderingNot guaranteedGuaranteed with FIFO SQS
ComplexitySimpler setupMore components and configuration

Conclusion

Selecting between the SNS to Lambda and the SNS to SQS to Lambda patterns depends largely on your application's specific requirements. If your use case demands minimal latency and involves straightforward message processing, SNS to Lambda might suffice. However, if you require enhanced durability, message ordering, or are working with a system that can experience traffic spikes, incorporating SQS in the middle can provide significant advantages in stability and cost optimization. As with any architectural decision, careful consideration of the trade-offs involved will guide you to the right pattern for your application.


Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.