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.
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
- SNS Topic Creation: An SNS topic is created, serving as a conduit for messages.
- Lambda Subscription: The Lambda function subscribes directly to the SNS topic.
- Message Publication: A message is published to the SNS topic.
- 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
- SNS Topic Creation: As before, an SNS topic is established for message dissemination.
- SQS Queue Subscription: Instead of a direct connection, an SQS queue subscribes to the SNS topic.
- Message Publication: A message is published to the SNS topic and subsequently delivered to the SQS queue.
- 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
| Feature | SNS to Lambda | SNS to SQS to Lambda |
| Latency | Low | Moderate (additional queuing delay) |
| Fault Tolerance | Moderate (dependent on retry settings) | High (SQS provides buffering and retries) |
| Cost | Potentially higher with high concurrency | Cost-effective with batch processing |
| Scalability | High, but tightly linked to Lambda limits | Very high, with independent scaling |
| Durability | Lower, as messages aren't stored after Lambda receives them | Enhanced with SQS's persistence |
| Message Ordering | Not guaranteed | Guaranteed with FIFO SQS |
| Complexity | Simpler setup | More 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
- SNS topic not publishing to SQS
- Source Control and deployment for AWS Lambda
- Spark on Kubernetes Executor pods silently get killed
- Specify log group for an AWS lambda?
- Specifying a custom role for lambda with the AWS CDK
- Spring boot startup error for AWS application There is not EC2 meta data available
- Spring boot startup error for AWS application There is not EC2 meta data available
- Spring Cloud - SQS - The specified queue does not exist for this wsdl version

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack 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.